资讯专栏INFORMATION COLUMN

python3 logging 'ascii' codec can't

txgcwm / 3201人阅读

摘要:环境问题发现最近刚从换到下搬砖,发现在跑的好好的代码,在下终端老是报错还是编码错误注册失败坦白说,看到这个错误好无奈。既然能在跑,换到就出错,那多半是环境问题了,然后我就开始了我的调试追踪之旅了跟踪调试先前调试都是一晃而过,只看结果。

环境:mac 10.12  python3  django 1.10
问题发现

最近刚从arch 换到 mac下搬砖, 发现在arch跑的好好的代码,在mac下 终端老是报错 .... 还是编码错误...

code:

try:
    int("sdfasdf")
except Exception as e:
    logger.error( (u" 注册失败 " + str(e)) )
    return render(request, "user/register.html")
        

error:

--- Logging error ---
Traceback (most recent call last):
  File "../repository/apps/user/views.py", line 104, in post
    int("sdfasdf")
ValueError: invalid literal for int() with base 10: "sdfasdf"

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/logging/__init__.py", line 983, in emit
    stream.write(msg)
UnicodeEncodeError: "ascii" codec can"t encode characters in position 91-94: ordinal not in range(128)   

坦白说,看到这个错误好无奈。既然能在Linux跑,换到mac就出错,那多半是环境问题了,然后我就开始了我的调试追踪之旅了

跟踪调试

先前调试都是一晃而过,只看结果。 然并软,并没有什么用,后面耐着性子多带带调试

首先从错误点开始:

/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/logging/__init__.py line: 982

发现这里出现ASCII编码,感觉拿到钥匙了~~

然后顺藤摸瓜,去看了 这个 stream 的初始化,发现标准的stderr 读写都是 encoding="utf-8",

但是读写日志文件就指定了 ASCII编码。

解决

知道了原因,就好解决了,google 一圈未果,马丹,竟然百度一下就知道怎么写(应该是歪果仁都不会有这个问题吧)

在 LOGGING 的 handlers 中配置编码就行了

    "debug": {
        "level": "DEBUG",
        "class": "logging.handlers.RotatingFileHandler",
        "filename": "log/debug.log",
        "maxBytes": 1024 * 1024 * 5,
        "backupCount": 5,
        "formatter": "standard",
        "encoding": "utf8"
    },

参考链接:每个 Python 程序员都要知道的日志实践

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/44319.html

相关文章

  • 【python】[转载]UnicodeEncodeError: 'ascii'

    摘要:原因在安装时,默认的编码是,当程序中出现非编码时,的处理常常会报这样的错,不过在就不会有这样的问题。 1、原因 python2.7在安装时,默认的编码是ascii,当程序中出现非ascii编码时,python的处理常常会报这样的错,不过在python3就不会有这样的问题。 2、解决办法 临时解决方法: 代码中加入如下三行import sys reload(sys) sys.setd...

    Airy 评论0 收藏0
  • 通过 Certbot 安装 Let's Encrypt 证书,来实现全站的 HTTPS 访

    摘要:甚至和百度的搜索结果也正在给予的网站更高的排名和优先收录权。由于预设的解码器是,所以就不能识别中文。那理解了这个错误原因后,我这边首先想到的就是网站的配置文件中是否含有中文。打开一看,确实存在中文注释。 相关知识 HTTP/HTTPS 是什么? 简单来说,HTTP 是一个传输网页内容的协议,比如我们浏览一个网页,网页上的文字、图片、 CSS 、 JS 等文件都是通过 HTTP 协议传输...

    Lsnsh 评论0 收藏0
  • 【Copy攻城狮日志】踩坑小程序之can't read property 'of

    摘要:根据当时的情境,是在微信开发者工具中删掉该小程序然后重新载入就解决了,大家给出的结论是微信小程序开发者工具的。 Created 2019-4-2 22:17:34 by huqiUpdated 2019-4-2 23:17:34 by huqishowImg(https://segmentfault.com/img/bVbqOLH?w=1526&h=818); ↑开局一张图,故事全靠编...

    hsluoyz 评论0 收藏0
  • Useful APIs that you probably don't notice

    摘要:结果 Date Get the number of days in a month The 0th day of next month is the last day of the current month. function daysInMonth(year, month) { let date = new Date(year, month + 1, 0); ret...

    崔晓明 评论0 收藏0

发表评论

0条评论

txgcwm

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<