Skip to content

Commit 29bb9b9

Browse files
committed
Update loguru config
1 parent 9602e28 commit 29bb9b9

File tree

3 files changed

+21
-38
lines changed

3 files changed

+21
-38
lines changed

backend/common/log.py

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
import inspect
44
import logging
55
import os
6-
7-
from sys import stderr, stdout
6+
import sys
87

98
from asgi_correlation_id import correlation_id
109
from loguru import logger
@@ -55,37 +54,11 @@ def setup_logging():
5554
# Debug log handlers
5655
# logging.debug(f'{logging.getLogger(name)}, {logging.getLogger(name).propagate}')
5756

58-
# Remove every other logger's handlers
59-
logger.remove()
57+
# configure loguru
58+
logger.configure(handlers=[{"sink": sys.stdout, "serialize": False}])
6059

61-
# Define the correlation_id filter function
62-
# https://github.com/snok/asgi-correlation-id?tab=readme-ov-file#configure-logging
63-
# https://github.com/snok/asgi-correlation-id/issues/7
64-
def correlation_id_filter(record) -> bool:
65-
cid = correlation_id.get(settings.LOG_CID_DEFAULT_VALUE)
66-
record['correlation_id'] = cid[: settings.LOG_CID_UUID_LENGTH]
67-
return True
68-
69-
# Configure loguru logger before starts logging
70-
logger.configure(
71-
handlers=[
72-
{
73-
'sink': stdout,
74-
'level': settings.LOG_STDOUT_LEVEL,
75-
'filter': lambda record: correlation_id_filter(record) and record['level'].no <= 25,
76-
'format': settings.LOG_STD_FORMAT,
77-
},
78-
{
79-
'sink': stderr,
80-
'level': settings.LOG_STDERR_LEVEL,
81-
'filter': lambda record: correlation_id_filter(record) and record['level'].no >= 30,
82-
'format': settings.LOG_STD_FORMAT,
83-
},
84-
]
85-
)
8660

87-
88-
def set_customize_logfile():
61+
def set_custom_logfile():
8962
log_path = path_conf.LOG_DIR
9063
if not os.path.exists(log_path):
9164
os.mkdir(log_path)
@@ -94,10 +67,18 @@ def set_customize_logfile():
9467
log_stdout_file = os.path.join(log_path, settings.LOG_STDOUT_FILENAME)
9568
log_stderr_file = os.path.join(log_path, settings.LOG_STDERR_FILENAME)
9669

70+
# Define the correlation_id filter function
71+
# https://github.com/snok/asgi-correlation-id?tab=readme-ov-file#configure-logging
72+
# https://github.com/snok/asgi-correlation-id/issues/7
73+
def correlation_id_filter(record):
74+
cid = correlation_id.get(settings.LOG_CID_DEFAULT_VALUE)
75+
record['correlation_id'] = cid[: settings.LOG_CID_UUID_LENGTH]
76+
return record
77+
9778
# loguru logger: https://loguru.readthedocs.io/en/stable/api/logger.html#loguru._logger.Logger.add
9879
log_config = {
99-
'rotation': '10 MB',
100-
'retention': '15 days',
80+
'rotation': '5 MB',
81+
'retention': '7 days',
10182
'compression': 'tar.gz',
10283
'enqueue': True,
10384
'format': settings.LOG_FILE_FORMAT,
@@ -107,18 +88,20 @@ def set_customize_logfile():
10788
logger.add(
10889
str(log_stdout_file),
10990
level=settings.LOG_STDOUT_LEVEL,
110-
**log_config,
91+
filter=lambda record: correlation_id_filter(record) and record['level'].no <= 25,
11192
backtrace=False,
11293
diagnose=False,
94+
**log_config,
11395
)
11496

11597
# stderr file
11698
logger.add(
11799
str(log_stderr_file),
118100
level=settings.LOG_STDERR_LEVEL,
119-
**log_config,
101+
filter=lambda record: correlation_id_filter(record) and record['level'].no >= 30,
120102
backtrace=True,
121103
diagnose=True,
104+
**log_config,
122105
)
123106

124107

backend/core/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class Settings(BaseSettings):
9494
COOKIE_REFRESH_TOKEN_EXPIRE_SECONDS: int = TOKEN_REFRESH_EXPIRE_SECONDS
9595

9696
# Log
97-
LOG_ROOT_LEVEL: str = 'NOTSET'
97+
LOG_ROOT_LEVEL: str = 'DEBUG'
9898
LOG_STD_FORMAT: str = (
9999
'<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</> | <lvl>{level: <8}</> | '
100100
'<cyan> {correlation_id} </> | <lvl>{message}</>'

backend/core/registrar.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from starlette.staticfiles import StaticFiles
1515

1616
from backend.common.exception.exception_handler import register_exception
17-
from backend.common.log import set_customize_logfile, setup_logging
17+
from backend.common.log import set_custom_logfile, setup_logging
1818
from backend.core.conf import settings
1919
from backend.core.path_conf import STATIC_DIR, UPLOAD_DIR
2020
from backend.database.db import create_table
@@ -99,7 +99,7 @@ def register_logger() -> None:
9999
:return:
100100
"""
101101
setup_logging()
102-
set_customize_logfile()
102+
set_custom_logfile()
103103

104104

105105
def register_static_file(app: FastAPI):

0 commit comments

Comments
 (0)