paip 日誌中文編碼原理問題本質解決python
阿新 • • 發佈:2018-11-12
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
paip.日誌中文編碼原理問題本質解決python
預設的python日誌編碼僅僅gbk...儲存utf8字元錯誤..輸出到個eric5的控制檯十預設好像十unicode的,要是有沒顯示出來的字元,大概十字型問題..調整eric5的字型走ok蘭.
#呼叫封裝
from log import *
iniLog (r"c:\enPn2atiEnPh.log")
logx("xxx")
#主要的程式碼
設定檔案編碼為utf8
handler = logging.FileHandler(logfile, "a",
encoding = "UTF-8")
設定預設日誌檔案
iniLog(r"c:\pylog.log")
作者 老哇的爪子 Attilax 艾龍, EMAIL:
轉載請註明來源: http://blog.csdn.net/attilax
#所有的程式碼
import logging
def iniLog(logfile):
log_format = '%(filename)s [%(asctime)s] [%(levelname)s] %(message)s'
#logfile="c:\pythonlog_en2ati.log"
#jeig bsin cant suport utf encode
#logging.basicConfig(encode="utf-8" , encoding = "UTF-8", filename=logfile, format=log_format,datefmt='%Y-%m-%d %H:%M:%S %p',level=logging.DEBUG)
#todox jeig support utf8 encode
handler = logging.FileHandler(logfile, "a",
encoding = "UTF-8")
formatter = logging.Formatter(log_format)
handler.setFormatter(formatter)
root_logger = logging.getLogger()
root_logger.addHandler(handler)
root_logger.setLevel(logging.INFO)
#encoding = "UTF-8"
# 定義一個Handler列印INFO及以上級別的日誌到sys.stderr
console = logging.StreamHandler()
console.setLevel(logging.INFO)
# 設定日誌列印格式
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
console.setFormatter(formatter)
# 將定義好的console日誌handler新增到root logger
logging.getLogger('').addHandler(console)
logging.info("--start loging")
def xx():
print("")
def logx(msg):
logging.info("\n---"+msg)
iniLog(r"c:\pylog.log")