Skip to content

Commit

Permalink
Chore/configure logging (#216)
Browse files Browse the repository at this point in the history
* 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
3 people authored Mar 12, 2024
1 parent c31e3b6 commit df7cc1d
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/datadoc/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@
from datadoc.frontend.components.control_bars import progress_bar
from datadoc.frontend.components.dataset_tab import build_dataset_tab
from datadoc.frontend.components.variables_tab import build_variables_tab
from datadoc.logging.logging_config import configure_logging
from datadoc.utils import get_app_version
from datadoc.utils import pick_random_port
from datadoc.utils import running_in_notebook

logging.basicConfig(level=config.get_log_level(), force=True)
configure_logging()
logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -146,7 +147,9 @@ def main(dataset_path: str | None = None) -> None:
)
else:
if dev_mode := config.get_dash_development_mode():
logger.warning("Starting in Development Mode. NOT SUITABLE FOR PRODUCTION.")
logger.warning(
"Starting in Development Mode. NOT SUITABLE FOR PRODUCTION.",
)
app.run(debug=dev_mode, port=port)


Expand Down
6 changes: 5 additions & 1 deletion src/datadoc/backend/dapla_dataset_path_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import logging
import pathlib
import re
from abc import ABC
Expand All @@ -21,6 +22,8 @@
import os
from datetime import date

logger = logging.getLogger(__name__)


@dataclass
class DateFormat(ABC):
Expand Down Expand Up @@ -110,7 +113,7 @@ class SsbDateFormat(DateFormat):
def get_floor(self, period_string: str) -> date | None:
"""Convert SSB format to date-string and return first date.
If not excisting SSB format, return None
If not existing SSB format, return None
>>> SSB_BIMESTER.get_floor("2003B8")
None
Expand All @@ -122,6 +125,7 @@ def get_floor(self, period_string: str) -> date | None:
period = year + month
return arrow.get(period, self.arrow_pattern).floor(self.timeframe).date()
except KeyError:
logger.exception("Error while converting to SSB date format")
return None

def get_ceil(self, period_string: str) -> date | None:
Expand Down
1 change: 1 addition & 0 deletions src/datadoc/frontend/callbacks/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def open_dataset_handling(
try:
state.metadata = open_file(file_path)
except FileNotFoundError:
logger.exception("File %s not found", str(file_path))
return (
False,
True,
Expand Down
1 change: 1 addition & 0 deletions src/datadoc/frontend/fields/display_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ def get_comma_separated_string(metadata: BaseModel, identifier: str) -> str:
try:
return ", ".join(value)
except TypeError:
logger.exception("Type error")
return ""


Expand Down
1 change: 1 addition & 0 deletions src/datadoc/logging/__init__.py
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."""
50 changes: 50 additions & 0 deletions src/datadoc/logging/json_formatter.py
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
53 changes: 53 additions & 0 deletions src/datadoc/logging/logging_config.py
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)

0 comments on commit df7cc1d

Please sign in to comment.