我有一个设置格式的日志记录脚本。它使用 utf-8 符号以提高可读性,编码设置为保存文件,如下所示。
log = logging.getLogger('general')
fmt = logging.Formatter('%(asctime)s.%(msecs)03d │ %(levelname)-10s│ %(lineno)4s %(funcName)-20s│ %(message)s',
datefmt='%Y/%m/%d │ %H:%M:%S')
handler_error = logging.handlers.RotatingFileHandler("error.log", 'a', encoding="utf-8")
handler_error.setLevel(logging.INFO)
handler_error.setFormatter(fmt)
log.addHandler(handler_error)
理论上,所有的分隔符都应该是 │ 的,但是datefmt中的分隔符被改成了 ¦。因此,生成的日志如下所示 - 注意所有正确添加的垂直线,除了日期和时间之间的一组。
我试过使用 unicode \u2502而不是 │,但没有帮助。有想法该怎么解决这个吗?
日志截图
编辑 - 来自另一个用户执行的错误日志。
File "C:\Users\xxx\AppData\Local\Programs\Python\Python37\lib\logging\__init__.py", line 1025, in emit
msg = self.format(record)
File "C:\Users\xxx\AppData\Local\Programs\Python\Python37\lib\logging\__init__.py", line 869, in format
return fmt.format(record)
File "C:\Users\xxx\AppData\Local\Programs\Python\Python37\lib\logging\__init__.py", line 610, in format
record.asctime = self.formatTime(record, self.datefmt)
File "C:\Users\xxx\AppData\Local\Programs\Python\Python37\lib\logging\__init__.py", line 548, in formatTime
s = time.strftime(datefmt, ct)
UnicodeEncodeError: 'locale' codec can't encode character '\u2502' in position 9: encoding error
弑天下
相关分类