feat: implement better logging using structlog

This commit is contained in:
ramvignesh-b
2026-04-26 13:05:50 +05:30
parent 97e4d0be98
commit 2896c60c5f
6 changed files with 191 additions and 9 deletions
+89
View File
@@ -0,0 +1,89 @@
import structlog
structlog.configure(
processors=[
structlog.contextvars.merge_contextvars,
structlog.stdlib.filter_by_level,
structlog.processors.TimeStamper(fmt="iso"),
structlog.stdlib.add_logger_name,
structlog.stdlib.add_log_level,
structlog.stdlib.PositionalArgumentsFormatter(),
structlog.processors.StackInfoRenderer(),
structlog.processors.format_exc_info,
structlog.processors.UnicodeDecoder(),
structlog.stdlib.ProcessorFormatter.wrap_for_formatter,
],
logger_factory=structlog.stdlib.LoggerFactory(),
cache_logger_on_first_use=True,
)
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"json_formatter": {
"()": structlog.stdlib.ProcessorFormatter,
"processor": structlog.processors.JSONRenderer(),
},
"plain_console": {
"()": structlog.stdlib.ProcessorFormatter,
"processor": structlog.dev.ConsoleRenderer(colors=True),
},
"key_value": {
"()": structlog.stdlib.ProcessorFormatter,
"processor": structlog.processors.KeyValueRenderer(key_order=["timestamp", "level", "event", "logger"]),
},
},
"handlers": {
"console": {
"class": "logging.StreamHandler",
"formatter": "plain_console",
},
"json_file": {
"class": "logging.handlers.WatchedFileHandler",
"filename": "logs/json.log",
"formatter": "json_formatter",
},
"flat_line_file": {
"class": "logging.handlers.WatchedFileHandler",
"filename": "logs/flat_line.log",
"formatter": "key_value",
},
"letters_log": {
"class": "logging.handlers.WatchedFileHandler",
"filename": "logs/letters.log",
"formatter": "key_value",
},
"scheduler_log": {
"class": "logging.handlers.WatchedFileHandler",
"filename": "logs/scheduler.log",
"formatter": "key_value",
},
},
"loggers": {
"django_structlog": {
"handlers": ["console", "flat_line_file", "json_file"],
"level": "INFO",
"propagate": False,
},
"django.core.mail": {
"handlers": ["console", "flat_line_file", "json_file"],
"level": "DEBUG",
"propagate": False,
},
"letters": {
"handlers": ["console", "flat_line_file", "json_file", "letters_log"],
"level": "INFO",
"propagate": False,
},
"scheduler": {
"handlers": ["console", "scheduler_log"],
"level": "INFO",
"propagate": False,
},
"": {
"handlers": ["console", "flat_line_file", "json_file"],
"level": "INFO",
},
},
}
+2 -6
View File
@@ -58,6 +58,7 @@ INSTALLED_APPS = [
"django.contrib.sessions",
"django.contrib.staticfiles",
"django_extensions",
"django_structlog",
"rest_framework",
"corsheaders",
"users",
@@ -74,6 +75,7 @@ MIDDLEWARE = [
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"django_structlog.middlewares.RequestMiddleware",
]
ROOT_URLCONF = "config.urls"
@@ -169,9 +171,3 @@ USE_TZ = True
STATIC_URL = "static/"
MEDIA_URL = "/media/"
MEDIA_ROOT = BASE_DIR / "media"
LOGGING = {
"version": 1,
"handlers": {"console": {"class": "logging.StreamHandler"}},
"loggers": {"letters": {"handlers": ["console"], "level": "INFO"}},
}