Skip to content

Instantly share code, notes, and snippets.

@ipmb ipmb/django_logging.py
Last active Sep 4, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.