-
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.
* Configure logging * Added new logging config making it possible to log both json and text, also added timestamp to the log * Removed unused logger --------- Co-authored-by: Miles Mason Winther <[email protected]> Co-authored-by: rlj <[email protected]>
- Loading branch information
1 parent
c31e3b6
commit df7cc1d
Showing
7 changed files
with
116 additions
and
3 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
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
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 @@ | ||
"""Code that is used for setting up the logging for the Datadoc app.""" |
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,50 @@ | ||
from __future__ import annotations | ||
|
||
import datetime as dt | ||
import json | ||
import logging | ||
from typing import Any | ||
|
||
|
||
class DatadocJSONFormatter(logging.Formatter): | ||
"""Class for formatting json for log files.""" | ||
|
||
def __init__( | ||
self, | ||
*, | ||
fmt_keys: dict[str, str] | None = None, | ||
) -> None: | ||
"""Initializer for the json formatter.""" | ||
super().__init__() | ||
self.fmt_keys = fmt_keys if fmt_keys is not None else {} | ||
|
||
def format(self, record: logging.LogRecord) -> str: | ||
"""Method that creates the json structure from a message created by the _prepare_log_dict method.""" | ||
message = self._prepare_log_dict(record) | ||
return json.dumps(message, default=str) | ||
|
||
def _prepare_log_dict(self, record: logging.LogRecord) -> dict[str, str | Any]: | ||
always_fields = { | ||
"message": record.getMessage(), | ||
"timestamp": dt.datetime.fromtimestamp( | ||
record.created, | ||
tz=dt.timezone.utc, | ||
).isoformat(), | ||
} | ||
if record.exc_info is not None: | ||
always_fields["exc_info"] = self.formatException(record.exc_info) | ||
|
||
if record.stack_info is not None: | ||
always_fields["stack_info"] = self.formatStack(record.stack_info) | ||
|
||
message = { | ||
key: ( | ||
msg_val | ||
if (msg_val := always_fields.pop(val, None)) is not None | ||
else getattr(record, val) | ||
) | ||
for key, val in self.fmt_keys.items() | ||
} | ||
message.update(always_fields) | ||
|
||
return message |
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,53 @@ | ||
from __future__ import annotations | ||
|
||
import logging.config | ||
from typing import Any | ||
|
||
from datadoc.config import get_log_level | ||
|
||
|
||
def configure_logging(config: dict[str, Any] | None = None) -> None: | ||
"""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", | ||
"logger": "name", | ||
"datefmt": "%Y-%m-%d %H:%M:%S", | ||
}, | ||
"json": { | ||
"()": "datadoc.logging.json_formatter.DatadocJSONFormatter", | ||
"fmt_keys": { | ||
"level": "levelname", | ||
"message": "message", | ||
"timestamp": "timestamp", | ||
"logger": "name", | ||
"module": "module", | ||
"function": "funcName", | ||
"line": "lineno", | ||
"thread_name": "threadName", | ||
}, | ||
}, | ||
}, | ||
"handlers": { | ||
"stdout": { | ||
"class": "logging.StreamHandler", | ||
"level": get_log_level(), | ||
"formatter": "simple", | ||
"stream": "ext://sys.stdout", | ||
}, | ||
}, | ||
"loggers": { | ||
"root": { | ||
"level": get_log_level(), | ||
"handlers": [ | ||
"stdout", | ||
], | ||
}, | ||
}, | ||
} | ||
|
||
logging.config.dictConfig(config) |