python - 记录日期格式误传 │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
解决方案
推荐阅读
- database - 检查 sysdate 是否等于 Oracle 中的事务日期
- php - 从 Stripe 不正确的层次结构中的 invoice.payment_failed webhook 获取订阅 ID
- python - 为什么我的代码会出现内存错误?
- php - 通过 wordpress db 的 PHP 查询不起作用
- python - SqlAlchemy + Firebird + FDB 的 UnicodeError
- ruby-on-rails - Rails 5 从外部 API 设置 current_user
- python - 如何合并共享相同指定索引条目的熊猫数据框中的行?
- android - 在 Android Studio 中使用 FAB 时出现 style.xml 错误
- sql - 下面的代码如何被SQL注入攻击绕过登录机制
- javascript - 在反应中动态加载菜单