代码
import logging
from logging import config
LOGGING_CONFIG = {
"version": 1,
"formatters": {
"default": {
'format': '%(asctime)19.19s %(levelname)1.1s %(message)s',
},
"file": {
'format': '%(asctime)19.19s %(filename)s %(lineno)s %(levelname)1.1s %(message)s',
},
"plain": {
"format": "%(message)s",
},
},
"handlers": {
"console": {
"class": "logging.StreamHandler",
"level": "INFO",
"formatter": "default",
},
"file": {
"class": "logging.FileHandler",
"level": 20,
"filename": "./log.txt",
"formatter": "default",
},
"rotate_file": {
"class": "logging.handlers.RotatingFileHandler",
"level": 20,
"filename": "./log.txt",
"formatter": "default",
"maxBytes": 52428800,
"backupCount": 7,
}
},
"loggers": {
"tmp": {
"handlers": ["console", "rotate_file"],
"level": "INFO",
"propagate": False,
},
},
"disable_existing_loggers": True,
}
config.dictConfig(LOGGING_CONFIG)
logger = logging.getLogger("tmp")
logger.info("吃\ud83d\udc3a")
报错信息
2021-02-28 22:35:55 I 吃\ud83d\udc3a
--- Logging error ---
Traceback (most recent call last):
File "/home/vagrant/.pyenv/versions/3.6.9/lib/python3.6/logging/__init__.py", line 996, in emit
stream.write(msg)
UnicodeEncodeError: 'utf-8' codec can't encode characters in position 23-24: surrogates not allowed
Call stack:
File "/home/vagrant/code/xxx/tmp.py", line 55, in <module>
logger.info("吃\ud83d\udc3a")
Message: '吃\ud83d\udc3a'
Arguments: ()
这种问题怎么 fix 呀?
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.