TOC

logging 不输出中文日志的问题

一个小问题。

import logging
logging.basicConfig(level=logging.DEBUG)
LOG = logging.getLogger(__name__)
name = u'中文'

LOG.debug('你好')
LOG.debug('你好, %s', '世界')
LOG.debug('你好, %s', name)  # <====
LOG.debug('Helle, %s', name)

发现只有箭头指向的那一行不打印。

改成 %r 之后发现 name 是 Unicode 类型, 然后 Python 会尝试将 '你好, %s' 转换成 Unicode,默认使用 ASCII 编码,就会报错:UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)

凡人的 Unicode / Str 问题。

编码问题会永远存在,但是 Python3 中默认使用 Unicode,如果遇到 Str(Unicode) + Bytes 会将 Bytes repr 成 Str,遇到编码问题的概率就会下降好多。

对了,忘了说,服务打印的日志会输出到标准错误,然后进了 supervisor 的日志(redirect_stderr = true),上面说的异常我就在 supervisor 日志中找到了。

如果你有魔法,你可以看到一个评论框~