Skip to content

add error log to file #3431

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

Merged
merged 8 commits into from
Aug 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ dmypy.json
FETCH_HEAD

#log
log*/
log/

checkpoints/
checkpoints_origin/
Expand Down
Empty file added fastdeploy/logger/__init__.py
Empty file.
55 changes: 55 additions & 0 deletions fastdeploy/logger/formatters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""

"""
自定义日志格式化器模块
该模块定义了 ColoredFormatter 类,用于在控制台输出带颜色的日志信息,
便于开发者在终端中快速识别不同级别的日志。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

增加开源协议

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好滴

"""

import logging


class ColoredFormatter(logging.Formatter):
"""
自定义日志格式器,用于控制台输出带颜色的日志。
支持的颜色:
- WARNING: 黄色
- ERROR: 红色
- CRITICAL: 红色
- 其他等级: 默认终端颜色
"""

COLOR_CODES = {
logging.WARNING: 33, # 黄色
logging.ERROR: 31, # 红色
logging.CRITICAL: 31, # 红色
}

def format(self, record):
"""
格式化日志记录,并根据日志等级添加 ANSI 颜色前缀和后缀。
Args:
record (LogRecord): 日志记录对象。
Returns:
str: 带有颜色的日志消息字符串。
"""
color_code = self.COLOR_CODES.get(record.levelno, 0)
prefix = f"\033[{color_code}m"
suffix = "\033[0m"
message = super().format(record)
if color_code:
message = f"{prefix}{message}{suffix}"
return message
Loading
Loading