Last active
March 9, 2021 20:34
-
-
Save ipmb/c957fddf1e06bbf0fead35b917a90790 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging.config | |
from django.utils.log import DEFAULT_LOGGING | |
# ======= | |
# LOGGING | |
# ======= | |
LOGGING_CONFIG = None | |
LOGLEVEL = os.getenv("LOGLEVEL", "info").upper() | |
def _log_format(): | |
keys = [ | |
"asctime", | |
"created", | |
"levelname", | |
"levelno", | |
"filename", | |
"funcName", | |
"lineno", | |
"module", | |
"message", | |
"name", | |
"pathname", | |
"process", | |
"processName", | |
] | |
return " ".join(["%({0:s})".format(i) for i in keys]) | |
LOGGING = { | |
"version": 1, | |
"disable_existing_loggers": False, | |
"formatters": { | |
"default": { | |
"format": _log_format(), | |
"class": "pythonjsonlogger.jsonlogger.JsonFormatter", | |
} | |
}, | |
"handlers": { | |
# console logs to stderr | |
"console": {"class": "logging.StreamHandler", "formatter": "default"}, | |
}, | |
"loggers": { | |
# default for all undefined Python modules | |
"": {"level": "WARNING", "handlers": ["console"]}, | |
# Our application code | |
"myapp": { | |
"level": LOGLEVEL, | |
"handlers": ["console"], | |
# Avoid double logging because of root logger | |
"propagate": False, | |
}, | |
}, | |
} | |
# Add runserver request logging back in | |
for k in ["formatters", "handlers", "loggers"]: | |
LOGGING[k]["django.server"] = DEFAULT_LOGGING[k]["django.server"] | |
try: | |
# setup pretty logging for local dev | |
import readable_log_formatter | |
LOGGING["formatters"]["default"] = { | |
"()": "readable_log_formatter.ReadableFormatter" | |
} | |
except ImportError: | |
pass | |
logging.config.dictConfig(LOGGING) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ipmb thanks for gist
should I copy this in
setting.py
to use?