-
Notifications
You must be signed in to change notification settings - Fork 61
198 add structlog #208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
198 add structlog #208
Changes from all commits
3f09b57
c09c338
9716a0b
7e00248
d0d2668
1098e39
8e7692b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,24 +1,65 @@ | ||||||
import logging | ||||||
import os | ||||||
from logging.handlers import RotatingFileHandler | ||||||
from pathlib import Path | ||||||
|
||||||
from rich.console import Console | ||||||
from rich.logging import RichHandler | ||||||
import orjson | ||||||
import structlog | ||||||
from attrs import define, field | ||||||
from whenever._whenever import Instant | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] Importing from a private module (
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
|
||||||
from app.utils.singleton import SingletonMeta | ||||||
from app.utils.singleton import SingletonMetaNoArgs | ||||||
|
||||||
|
||||||
class AppLogger(metaclass=SingletonMeta): | ||||||
_logger = None | ||||||
# TODO: merge this wrapper with the one in structlog under one hood of AppLogger | ||||||
class BytesToTextIOWrapper: | ||||||
def __init__(self, handler, encoding="utf-8"): | ||||||
self.handler = handler | ||||||
self.encoding = encoding | ||||||
|
||||||
def __init__(self): | ||||||
self._logger = logging.getLogger(__name__) | ||||||
def write(self, b): | ||||||
if isinstance(b, bytes): | ||||||
self.handler.stream.write(b.decode(self.encoding)) | ||||||
else: | ||||||
self.handler.stream.write(b) | ||||||
self.handler.flush() | ||||||
|
||||||
def flush(self): | ||||||
self.handler.flush() | ||||||
|
||||||
def close(self): | ||||||
self.handler.close() | ||||||
|
||||||
def get_logger(self): | ||||||
return self._logger | ||||||
|
||||||
@define(slots=True) | ||||||
class AppStructLogger(metaclass=SingletonMetaNoArgs): | ||||||
_logger: structlog.BoundLogger = field(init=False) | ||||||
|
||||||
class RichConsoleHandler(RichHandler): | ||||||
def __init__(self, width=200, style=None, **kwargs): | ||||||
super().__init__( | ||||||
console=Console(color_system="256", width=width, style=style, stderr=True), | ||||||
**kwargs, | ||||||
def __attrs_post_init__(self): | ||||||
_log_date = Instant.now().py_datetime().strftime("%Y%m%d") | ||||||
_log_path = Path(f"{_log_date}_{os.getpid()}.log") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] Logs are written to the current working directory; consider writing to a dedicated Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
_handler = RotatingFileHandler( | ||||||
filename=_log_path, | ||||||
mode="a", | ||||||
maxBytes=10 * 1024 * 1024, | ||||||
backupCount=5, | ||||||
encoding="utf-8" | ||||||
) | ||||||
structlog.configure( | ||||||
cache_logger_on_first_use=True, | ||||||
wrapper_class=structlog.make_filtering_bound_logger(logging.INFO), | ||||||
processors=[ | ||||||
structlog.contextvars.merge_contextvars, | ||||||
structlog.processors.add_log_level, | ||||||
structlog.processors.format_exc_info, | ||||||
structlog.processors.TimeStamper(fmt="iso", utc=True), | ||||||
structlog.processors.JSONRenderer(serializer=orjson.dumps), | ||||||
], | ||||||
logger_factory=structlog.BytesLoggerFactory( | ||||||
file=BytesToTextIOWrapper(_handler) | ||||||
) | ||||||
) | ||||||
self._logger = structlog.get_logger() | ||||||
|
||||||
def get_logger(self) -> structlog.BoundLogger: | ||||||
return self._logger |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] There are multiple blocks of commented-out experimental code and TODOs; consider removing or relocating them to keep the codebase clean and focused.
Copilot uses AI. Check for mistakes.