-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unify the logging config so that JSON logging works
- Loading branch information
Showing
8 changed files
with
103 additions
and
121 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,9 @@ | ||
"""Configuration for the Gunicorn server.""" | ||
|
||
import sys | ||
from datadoc.logging_configuration.logging_config import get_log_config | ||
|
||
bind = "0.0.0.0:8050" | ||
workers = 1 | ||
loglevel = "info" | ||
preload = True | ||
|
||
logconfig_dict = GUNICORN_LOG_CONFIG = { | ||
"handlers": { | ||
"console_stdout": { | ||
"level": "DEBUG", | ||
"class": "logging.StreamHandler", | ||
"stream": sys.stdout, | ||
}, | ||
}, | ||
"loggers": { | ||
"": {"handlers": ["console_stdout"], "level": "INFO", "propagate": False}, | ||
"gunicorn": { | ||
"handlers": ["console_stdout"], | ||
"level": "INFO", | ||
"propagate": False, | ||
}, | ||
"gunicorn.access": { | ||
"handlers": ["console_stdout"], | ||
"level": "INFO", | ||
"propagate": False, | ||
}, | ||
"gunicorn.error": { | ||
"handlers": ["console_stdout"], | ||
"level": "INFO", | ||
"propagate": False, | ||
}, | ||
}, | ||
"root": { | ||
"level": "INFO", | ||
"handlers": ["console_stdout"], | ||
}, | ||
} | ||
logconfig_dict = get_log_config() |
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
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
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
11 changes: 11 additions & 0 deletions
11
src/datadoc/logging_configuration/gunicorn_access_log_filter.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import logging | ||
|
||
EXCLUDED_PATHS = ["/healthz", "/_dash-", "/assets"] | ||
|
||
|
||
class GunicornAccessLoggerHealthProbeFilter(logging.Filter): | ||
"""Filter out any Gunicorn access logs on Liveness or Readiness probes.""" | ||
|
||
def filter(self, record: logging.LogRecord) -> bool: | ||
"""Filter health probes on the /healthz endpoints.""" | ||
return all(path not in record.getMessage() for path in EXCLUDED_PATHS) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,69 @@ | ||
from __future__ import annotations | ||
|
||
import logging.config | ||
from typing import Any | ||
|
||
from datadoc.config import get_log_formatter | ||
from datadoc.config import get_log_level | ||
from datadoc.logging_configuration.gunicorn_access_log_filter import ( | ||
GunicornAccessLoggerHealthProbeFilter, | ||
) | ||
|
||
|
||
def configure_logging(config: dict[str, Any] | None = None) -> None: | ||
def get_log_config() -> dict[str, Any]: | ||
"""Configure logging for the application.""" | ||
if not config: | ||
config = { | ||
"version": 1, | ||
"disable_existing_loggers": False, | ||
"formatters": { | ||
"simple": { | ||
"format": "%(asctime)s.%(msecs)03d %(levelname)s %(name)s: %(message)s", | ||
return { | ||
"version": 1, | ||
"disable_existing_loggers": False, | ||
"formatters": { | ||
"simple": { | ||
"format": "%(asctime)s.%(msecs)03d %(levelname)s %(name)s: %(message)s", | ||
"logger": "name", | ||
"datefmt": "%Y-%m-%d %H:%M:%S", | ||
}, | ||
"json": { | ||
"()": "datadoc.logging_configuration.json_formatter.DatadocJSONFormatter", | ||
"fmt_keys": { | ||
"level": "levelname", | ||
"message": "message", | ||
"timestamp": "timestamp", | ||
"logger": "name", | ||
"datefmt": "%Y-%m-%d %H:%M:%S", | ||
}, | ||
"json": { | ||
"()": "datadoc.logging_configuration.json_formatter.DatadocJSONFormatter", | ||
"fmt_keys": { | ||
"level": "levelname", | ||
"message": "message", | ||
"timestamp": "timestamp", | ||
"logger": "name", | ||
"module": "module", | ||
"function": "funcName", | ||
"line": "lineno", | ||
"thread_name": "threadName", | ||
}, | ||
"module": "module", | ||
"function": "funcName", | ||
"line": "lineno", | ||
"thread_name": "threadName", | ||
}, | ||
}, | ||
"handlers": { | ||
"stdout": { | ||
"class": "logging.StreamHandler", | ||
"level": get_log_level(), | ||
"formatter": get_log_formatter(), | ||
"stream": "ext://sys.stdout", | ||
}, | ||
}, | ||
"handlers": { | ||
"stdout": { | ||
"class": "logging.StreamHandler", | ||
"level": get_log_level(), | ||
"formatter": get_log_formatter(), | ||
"stream": "ext://sys.stdout", | ||
}, | ||
"loggers": { | ||
"root": { | ||
"level": get_log_level(), | ||
"handlers": [ | ||
"stdout", | ||
], | ||
}, | ||
}, | ||
"loggers": { | ||
"gunicorn": { | ||
"handlers": ["stdout"], | ||
"level": "INFO", | ||
"propagate": False, | ||
}, | ||
} | ||
|
||
logging.config.dictConfig(config) | ||
logging.getLogger("faker").setLevel(logging.ERROR) | ||
"gunicorn.access": { | ||
"handlers": ["stdout"], | ||
"level": "INFO", | ||
"propagate": False, | ||
"filters": [GunicornAccessLoggerHealthProbeFilter()], | ||
}, | ||
"gunicorn.error": { | ||
"handlers": ["stdout"], | ||
"level": "INFO", | ||
"propagate": False, | ||
}, | ||
}, | ||
"root": { | ||
"level": get_log_level(), | ||
"handlers": [ | ||
"stdout", | ||
], | ||
}, | ||
} |
Oops, something went wrong.