Skip to content

Commit 926a3f9

Browse files
authored
add different log files (#77)
1 parent 4e4c6fb commit 926a3f9

File tree

3 files changed

+33
-17
lines changed

3 files changed

+33
-17
lines changed

backend/app/common/exception/exception_handler.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from uvicorn.protocols.http.h11_impl import STATUS_PHRASES
1010

1111
from backend.app.common.exception.errors import BaseExceptionMixin
12+
from backend.app.common.log import log
1213
from backend.app.common.response.response_schema import response_base
1314
from backend.app.core.conf import settings
1415

@@ -106,9 +107,11 @@ def all_exception_handler(request: Request, exc: Exception):
106107
)
107108

108109
else:
110+
import traceback
111+
log.exception(traceback.format_exc())
109112
return JSONResponse(
110113
status_code=500,
111-
content=response_base.fail(code=500, msg=str(exc))
112-
if settings.ENVIRONMENT != 'pro'
114+
content=response_base.fail(code=500, msg=traceback.format_exc())
115+
if settings.ENVIRONMENT == 'dev'
113116
else response_base.fail(code=500, msg='Internal Server Error'),
114117
)

backend/app/common/log.py

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,36 @@
1515

1616

1717
class Logger:
18-
@staticmethod
19-
def log() -> loguru.Logger:
20-
if not os.path.exists(path_conf.LogPath):
21-
os.mkdir(path_conf.LogPath)
18+
19+
def __init__(self):
20+
self.log_path = path_conf.LogPath
21+
22+
def log(self) -> loguru.Logger:
23+
if not os.path.exists(self.log_path):
24+
os.mkdir(self.log_path)
2225

2326
# 日志文件
24-
log_file = os.path.join(path_conf.LogPath, settings.LOG_FILE_NAME)
27+
log_stdout_file = os.path.join(self.log_path, settings.LOG_STDOUT_FILENAME)
28+
log_stderr_file = os.path.join(self.log_path, settings.LOG_STDERR_FILENAME)
2529

26-
# loguru日志
27-
# more: https://github.com/Delgan/loguru#ready-to-use-out-of-the-box-without-boilerplate
30+
# loguru 日志: https://loguru.readthedocs.io/en/stable/api/logger.html#loguru._logger.Logger.add
31+
log_config = dict(rotation='10 MB', retention='15 days', compression='tar.gz', enqueue=True)
32+
# stdout
33+
logger.add(
34+
log_stdout_file,
35+
level='INFO',
36+
filter=lambda record: record['level'].name == 'INFO' or record['level'].no <= 25,
37+
**log_config,
38+
backtrace=False,
39+
diagnose=False,
40+
)
41+
# stderr
2842
logger.add(
29-
log_file,
30-
encoding='utf-8',
31-
level='DEBUG',
32-
rotation='00:00', # 每天 0 点创建一个新日志文件
33-
retention='7 days', # 定时自动清理文件
34-
enqueue=True, # 异步安全
35-
backtrace=True, # 错误跟踪
43+
log_stderr_file,
44+
level='ERROR',
45+
filter=lambda record: record['level'].name == 'ERROR' or record['level'].no >= 30,
46+
**log_config,
47+
backtrace=True,
3648
diagnose=True,
3749
)
3850

backend/app/core/conf.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ def validator_api_url(cls, values):
8080
TOKEN_REDIS_PREFIX: str = 'fba_token'
8181

8282
# Log
83-
LOG_FILE_NAME: str = 'fba.log'
83+
LOG_STDOUT_FILENAME: str = 'fba_access.log'
84+
LOG_STDERR_FILENAME: str = 'fba_error.log'
8485

8586
# Middleware
8687
MIDDLEWARE_CORS: bool = True

0 commit comments

Comments
 (0)