python3记录器-UnicodeEncodeError

时间:2018-10-19 16:28:57

标签: python python-3.x

我有一个这样的记录器设置:

import logging
from logging.handlers import RotatingFileHandler
import sys

# root logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

# create a file logger
handler = RotatingFileHandler('log/core-application.log', maxBytes=1024*1024*1, backupCount=3)
handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(funcName)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)


# create stdout logger
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(funcName)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)

logger.info('Logging all setup')

在我的开发系统上,此日志记录条目运行正常。它记录您在这里看到的内容:

logger.info('message key: {}'.format('2018-10-19_00:20:56_\xd8\xa7\xd9\x84\xd8\xb5\xd9\x88\xd8\xa7\xd9\x81/'))

我的ubuntu AWS EC2服务器上的同一行显示了一个错误:

  

---记录错误---追溯(最近一次调用最后一次):文件“ /usr/lib/python3.5/logging/init.py”,发射行982       stream.write(msg)UnicodeEncodeError:'ascii'编解码器无法对位置108-113中的字符进行编码:序数不在范围(128)中

有人能想象为什么吗?

顺便说一句: EC2服务器的语言环境为:

LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8

谢谢。

3 个答案:

答案 0 :(得分:2)

对我有用的是在处理程序中添加encoding ='utf8'arg:

 formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
 log_path = '/var/log/app'
 log_file = '%s/%s.log' % (log_path, name)
 create_log_file(log_file)
 handler = logging.FileHandler(log_file, encoding='utf8')
 handler.setFormatter(formatter)
 logger.addHandler(handler)

答案 1 :(得分:1)

我刚刚找到了解决方案,并希望分享答案:

它已连接到管理服务器上python应用程序的主管。 Supervisor配置需要以某种方式包含下面的环境变量,以便能够将UTF-8打印到stdout。

MFR_DRIVER_CLASS: Mfr
MFR_BATCH_CLASS: TestScript
MFR_CMD_LINE_ARGS:
INFO   - Executing script: TestScript.groovy, null
INFO   - STARTED AT: 10/19/2018 14:05:16.484
Exception in thread "main" org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed, script1539972317344.groovy: 1: expecting '!', found 'U' @ line 1, column 2.
1 error

    at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:296)
    at org.codehaus.groovy.control.ErrorCollector.addFatalError(ErrorCollector.java:143)
    at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:113)
    at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:125)
    at org.codehaus.groovy.control.SourceUnit.addError(SourceUnit.java:352)
    at org.codehaus.groovy.antlr.AntlrParserPlugin.parseCST(AntlrParserPlugin.java:80)
    at org.codehaus.groovy.control.SourceUnit.parse(SourceUnit.java:248)
    at org.codehaus.groovy.control.CompilationUnit$1.call(CompilationUnit.java:143)
    at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:772)
    at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:438)
    at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:277)
    at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:248)
    at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:243)
    at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:205)
    at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:215)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:86)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:230)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:912)
    at groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:946)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:756)
    at org.codehaus.groovy.runtime.InvokerHelper.invokePojoMethod(InvokerHelper.java:766)
    at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:754)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:170)
    at groovy.util.ConfigSlurper.parse(ConfigSlurper.groovy:151)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:86)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:230)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:912)
    at groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:946)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:756)
    at org.codehaus.groovy.runtime.InvokerHelper.invokePogoMethod(InvokerHelper.java:778)
    at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:758)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:170)
    at DBConnectionUtil.readBatchDataSourceConfig(DBConnectionUtil.groovy:37)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:86)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:230)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:912)
    at groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:946)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:756)
    at org.codehaus.groovy.runtime.InvokerHelper.invokePogoMethod(InvokerHelper.java:778)
    at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:758)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:170)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethod0(ScriptBytecodeAdapter.java:198)
    at GormHelper.loadProperties(GormHelper.groovy:122)
    at GormHelper.this$2$loadProperties(GormHelper.groovy)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:86)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:230)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:912)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnCurrentN(ScriptBytecodeAdapter.java:78)
    at GormHelper.buildConfiguration(GormHelper.groovy:109)
    at GormHelper.this$2$buildConfiguration(GormHelper.groovy)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:86)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:230)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:912)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnCurrentN(ScriptBytecodeAdapter.java:78)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnCurrent0(ScriptBytecodeAdapter.java:112)
    at GormHelper.doInitialize(GormHelper.groovy:67)
    at GormHelper.this$2$doInitialize(GormHelper.groovy)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:86)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:230)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:912)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:756)
    at org.codehaus.groovy.runtime.InvokerHelper.invokePogoMethod(InvokerHelper.java:778)
    at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:758)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:170)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethod0(ScriptBytecodeAdapter.java:198)
    at GormHelper.initialize(GormHelper.groovy:57)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:86)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:230)
    at groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1105)
    at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:749)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:170)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethod0(ScriptBytecodeAdapter.java:198)
    at Mfr.main(Mfr.groovy:35)
Finished executing TestScript. Please check for errors.

This is the TestScript groovy script

class TestScript {
    static void main(String[] args) {
        println "Hello World"
    }

}

编辑:

https://docs.python.org/3.7/whatsnew/3.7.html#whatsnew37-pep538

Python 3.7似乎旨在自动解决此问题。

答案 2 :(得分:1)

搜索了很长一段时间后,此解决方案在Windows 10 x64和Python 3.7.6下为我工作,每当`logger.error('message',exc_info = True)因为回溯包含无效字符而失败时,该解决方案便对我有效。

在格式化程序字符串之前添加一个u以强制unicode消息:

#formatter = logging.Formatter('%(asctime)s - %(funcName)s - %(levelname)s - %(message)s'
formatter = logging.Formatter(u'%(asctime)s - %(funcName)s - %(levelname)s - %(message)s'

希望对任何阅读本书的人都有帮助。