乱码是因为没有设置文件格式,存在esc是因为颜色问题,阅读器无法渲染。解决配置如下
import re
# 自定义formatter,匹配常见的ANSI转义序列,处理日志中的esc字符
class NoColorFormatter(logging.Formatter):
def format(self, record):
ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
record.msg = ansi_escape.sub('', record.msg)
return super().format(record)
# 创建一个带有UTF-8编码的RotatingFileHandler
handler = RotatingFileHandler('logfile.log', maxBytes=1*1024*1024, backupCount=3, encoding='utf-8')
handler.setLevel(logging.INFO)
# 创建一个格式器并将其添加到处理器
formatter = NoColorFormatter('%(asctime)s - %(levelname)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
handler.setFormatter(formatter)
# 获取根日志记录器并添加处理器
logger = logging.getLogger()
logger.addHandler(handler)
文章评论
Hi! Do you know if they make any plugins to help with SEO?
I'm trying to get my website to rank for some targeted keywords but I'm not seeing
very good success. If you know of any please share. Thanks!
You can read similar article here: <a href="https://www.ecobij.nl/">Eco blankets</a>