首页 > 解决方案 > 记录日期格式误传 │as ¦

问题描述

我有一个设置格式的日志记录脚本。它使用 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

标签: pythonloggingutf-8

解决方案



推荐阅读