Created
February 14, 2021 11:02
-
-
Save SiddharthPant/435ebf435674df534e6dc2b67dc97326 to your computer and use it in GitHub Desktop.
Django logging done right
This file contains hidden or 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 | |
# ... other settings | |
LOGGING_CONFIG = None # This empties out Django's logging config | |
LOGGING = { | |
"version": 1, | |
"disable_existing_loggers": False, | |
"formatters": { | |
"verbose": { | |
"()": "colorlog.ColoredFormatter", | |
"format": "%(log_color)s %(levelname)-8s %(asctime)s %(request_id)s %(process)s --- " | |
"%(lineno)-8s [%(name)s] %(funcName)-24s : %(message)s", | |
"log_colors": { | |
"DEBUG": "blue", | |
"INFO": "white", | |
"WARNING": "yellow", | |
"ERROR": "red", | |
"CRITICAL": "bold_red", | |
}, | |
}, | |
"aws": { | |
"format": "%(asctime)s - %(name)s - %(levelname)s - %(funcName)s:%(lineno)d - %(message)s", | |
"datefmt": "%Y-%m-%d %H:%M:%S", | |
}, | |
}, | |
"filters": { | |
"request_id": {"()": "log_request_id.filters.RequestIDFilter"}, | |
}, | |
"handlers": { | |
"console": { | |
"class": "logging.StreamHandler", | |
"formatter": "verbose", | |
"filters": ["request_id"], | |
}, | |
}, | |
"loggers": { | |
# Default logger for any logger name | |
"": { | |
"level": "INFO", | |
"handlers": ["console", ], | |
"propagate": False, | |
}, | |
# Logger for django server logs with django.server logger name | |
"django.server": { | |
"level": "DEBUG", | |
"handlers": ["console", ], | |
"propagate": False, | |
}, | |
# Logger for 3rd party library to restrict unnecessary log statments by the library | |
"azure": {"level": "ERROR", "handlers": ["console"], "propogate": False}, | |
}, | |
} | |
logging.config.dictConfig(LOGGING) # Finally replace our config in python logging | |
# ... other settings |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment