File size: 1,904 Bytes
355ff2b
 
 
 
 
 
efc36ce
 
355ff2b
ab1bbb0
355ff2b
efc36ce
355ff2b
 
 
efc36ce
 
 
355ff2b
 
efc36ce
 
 
ed1c494
355ff2b
 
 
efc36ce
355ff2b
efc36ce
ed1c494
355ff2b
 
efc36ce
355ff2b
 
 
efc36ce
355ff2b
 
 
efc36ce
355ff2b
 
 
 
 
 
 
 
efc36ce
355ff2b
efc36ce
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import structlog
from config.settings import AppConfig
from domain.logger import Logger
from pathlib import Path
import logging


# Don't forget to keep logs.json file meaningful.
def setup_logging(logs_path: Path | str):
    # log_file = open(logs_path, "a", encoding="utf-8")
    structlog.configure(
        processors=[
            structlog.processors.StackInfoRenderer(),  # Stack strace, showing the exact source of errors.
            structlog.processors.format_exc_info,  # for Exceptions in JSON
            structlog.processors.add_log_level,  # Adding log level (info, warning, error)
            structlog.processors.TimeStamper(
                fmt="iso", utc=True
            ),  # Adding ISO timestamp
            structlog.processors.JSONRenderer(),  # Makes JSON outputs
        ],
        wrapper_class=structlog.make_filtering_bound_logger(
            logging.INFO
        ),  # Profiling info and higher.
        # logger_factory = structlog.WriteLoggerFactory(file=log_file),    # Save in file instead of terminal
        cache_logger_on_first_use=True,  # Caching being used for optimization
    )


class StructLogger(Logger):
    def __init__(self, settings: AppConfig):
        setup_logging(logs_path="")
        self._logger = structlog.get_logger()

    def info(self, message: str, **kwargs):
        print(message)
        self._logger.info(message, **kwargs)

    def debug(self, message: str, **kwargs):
        print(message)
        self._logger.debug(message, **kwargs)

    def error(self, message: str, **kwargs):
        print(message)
        self._logger.error(message, **kwargs)

    def warn(self, message: str, **kwargs):
        # return super().warn(msg, **kwargs)
        print(message)
        self._logger.warn(message, **kwargs)

    def exception(self, message: str, **kwargs):
        print(message)
        self._logger.exception(message, **kwargs)