|
| 1 | +""" |
| 2 | +A sample logger (requires structlog, tested with structlog==25.1.0) |
| 3 | +
|
| 4 | +Usage |
| 5 | +----- |
| 6 | +
|
| 7 | +>>> from $package_name.log import configure_file_and_print_logger, get_logger |
| 8 | +>>> configure_file_and_print_logger("app.log") |
| 9 | +>>> # OR |
| 10 | +>>> configure_print_logger() |
| 11 | +>>> logger = get_logger() |
| 12 | +>>> logger.info("Hello, world!") |
| 13 | +""" |
| 14 | + |
| 15 | +import logging |
| 16 | +import structlog |
| 17 | +from typing import Any, TextIO |
| 18 | +import os |
| 19 | + |
| 20 | +import structlog |
| 21 | +from structlog import WriteLogger, PrintLogger |
| 22 | + |
| 23 | + |
| 24 | +class CustomLogger: |
| 25 | + """ |
| 26 | + A custom logger that writes to a file and prints to the console |
| 27 | + """ |
| 28 | + |
| 29 | + def __init__(self, file: TextIO | None = None): |
| 30 | + self._file = file |
| 31 | + self._write_logger = WriteLogger(self._file) |
| 32 | + self._print_logger = PrintLogger() |
| 33 | + |
| 34 | + def msg(self, message: str) -> None: |
| 35 | + self._write_logger.msg(message) |
| 36 | + self._print_logger.msg(message) |
| 37 | + |
| 38 | + log = debug = info = warn = warning = msg |
| 39 | + fatal = failure = err = error = critical = exception = msg |
| 40 | + |
| 41 | + |
| 42 | +class CustomLoggerFactory: |
| 43 | + def __init__(self, file: TextIO | None = None): |
| 44 | + self._file = file |
| 45 | + |
| 46 | + def __call__(self, *args: Any) -> CustomLogger: |
| 47 | + return CustomLogger(self._file) |
| 48 | + |
| 49 | + |
| 50 | +def configure_file_and_print_logger(file_path: str = "app.log") -> None: |
| 51 | + structlog.configure( |
| 52 | + processors=[ |
| 53 | + structlog.contextvars.merge_contextvars, |
| 54 | + structlog.processors.add_log_level, |
| 55 | + structlog.processors.StackInfoRenderer(), |
| 56 | + structlog.dev.set_exc_info, |
| 57 | + structlog.processors.TimeStamper(fmt="%Y-%m-%d %H:%M:%S", utc=False), |
| 58 | + structlog.processors.JSONRenderer(), |
| 59 | + ], |
| 60 | + wrapper_class=structlog.make_filtering_bound_logger(logging.NOTSET), |
| 61 | + context_class=dict, |
| 62 | + logger_factory=CustomLoggerFactory(open(file_path, "at")), |
| 63 | + cache_logger_on_first_use=False, |
| 64 | + ) |
| 65 | + |
| 66 | + |
| 67 | +def configure_print_logger() -> None: |
| 68 | + structlog.configure( |
| 69 | + processors=[ |
| 70 | + structlog.contextvars.merge_contextvars, |
| 71 | + structlog.processors.add_log_level, |
| 72 | + structlog.processors.StackInfoRenderer(), |
| 73 | + structlog.dev.set_exc_info, |
| 74 | + structlog.processors.TimeStamper(fmt="%Y-%m-%d %H:%M:%S", utc=False), |
| 75 | + structlog.dev.ConsoleRenderer(), |
| 76 | + ], |
| 77 | + wrapper_class=structlog.make_filtering_bound_logger(logging.NOTSET), |
| 78 | + context_class=dict, |
| 79 | + logger_factory=structlog.PrintLoggerFactory(), |
| 80 | + cache_logger_on_first_use=False, |
| 81 | + ) |
| 82 | + |
| 83 | + |
| 84 | +def configure_no_logging() -> None: |
| 85 | + structlog.configure( |
| 86 | + logger_factory=structlog.PrintLoggerFactory(open(os.devnull, "w")), |
| 87 | + ) |
| 88 | + |
| 89 | + |
| 90 | +def get_logger(): |
| 91 | + return structlog.get_logger() |
0 commit comments