Python3解決Nginx日志的中文亂碼問題


Nginx中文日志出現亂碼,如下所示:

{\x22code\x22: \x22000\x22, \x22msg\x22: \x22\x5Cu6210\x5Cu529f\x22, \x22data\x22: {\x22store_id\x22: 322589}, \x22subcode\x22: \x22100000\x22}

Python3進行解碼

import json

msg = """
{\x22code\x22: \x22000\x22, \x22msg\x22: \x22\x5Cu6210\x5Cu529f\x22, \x22data\x22: {\x22store_id\x22: 322589}, \x22subcode\x22: \x22100000\x22}
"""
res_obj = json.loads(msg.encode('raw_unicode_escape').decode('utf8'))
print(json.dumps(res_obj, ensure_ascii=False))

結果如下所示:

{"code": "000", "msg": "成功", "data": {"store_id": 322589}, "subcode": "100000"}

總結

  • Nginx默認不支持中文日志,會將中文轉成16進制存儲
  • 通過Python3先編碼在解碼:msg.encode('raw_unicode_escape').decode('utf8')即可完成相應的轉換
  • Nginx可以在配置中支持中文的json,在定義 access log 格式時,加上 escape=json,如下所示:
log_format  main escape=json '$remote_addr - $remote_user [$time_local] "$request" ' 
'$status $body_bytes_sent "$http_referer" ' 
'"$http_user_agent" "$http_x_forwarded_for"';  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM