【问题标题】:Logentries - Python example return UnicodeDecodeErrorLogentries - Python 示例返回 UnicodeDecodeError
【发布时间】:2015-12-08 05:30:31
【问题描述】:

我正在尝试设置一个“logentries”帐户来记录我的 python 开发事件。 但即使使用文档中最简单的测试,我也会收到以下错误。有什么想法吗?

Python 2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)] on wi
n32
Type "help", "copyright", "credits" or "license" for more information.
>>> from logentries import LogentriesHandler
>>> import logging
>>> log = logging.getLogger('logentries')
>>> log.setLevel(logging.INFO)
>>> log.addHandler(LogentriesHandler('xxxx-xxxx-xxxx-xxxx-xxxx'))
>>> log.info('teste')
LE: Starting Logentries Asynchronous Socket Appender
>>> Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
    self.run()
  File "C:\Python27\lib\site-packages\logentries\utils.py", line 96, in run
    multiline = le_helpers.create_unicode(data).replace(
  File "C:\Python27\lib\site-packages\logentries\helpers.py", line 31, in create
_unicode
    return unicode(ch, 'utf-8')
UnicodeDecodeError: 'utf8' codec can't decode byte 0xe1 in position 59: invalid
continuation byte

【问题讨论】:

  • 似乎 ch 不包含有效的 utf-8 字节字符串。异常时 ch 的值是多少?至少 ch 如何获得它的价值? // 您可以尝试使用函数的第三个参数,即 unicode(ch, 'utf-8', 'replace'),但这会将所有无效字节替换为“?”。默认为“严格”,这就是它引发异常的原因。

标签: python python-2.7 logging logentries


【解决方案1】:

感谢 Nikita 的评论,我找到了问题所在。

logentries 中的 Helpers.py 库不适合拉丁语言操作系统,其时间戳标签具有特殊字母“á”。

替换

unicode(ch,'utf8')

unicode(ch,'utf8','replace')

成功了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-26
    • 1970-01-01
    • 2016-04-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-15
    • 1970-01-01
    • 2017-03-22
    相关资源
    最近更新 更多