python logging模塊寫入中文,文件亂碼的解決方法

一下文件執行將產生亂碼,切.log文件顯示問好,打不開spa

import logging def shop_logging(name): name = name+"登陸成功!" logger = logging.getLogger() fh = logging.FileHandler("test.log") formatter = logging.Formatter("%(asctime)s - %(name)s-%(levelname)s %(message)s") fh.setFormatter(formatter) logger.addHandler(fh) logger.setLevel(logging.DEBUG) logger.info(name) shop_logging("auxc")

經發現,是沒有定義文件格式,修復後code

import logging def shop_logging(name): name = name+"登陸成功!" logger = logging.getLogger() fh = logging.FileHandler("test.log",encoding="utf-8",mode="a") formatter = logging.Formatter("%(asctime)s - %(name)s-%(levelname)s %(message)s") fh.setFormatter(formatter) logger.addHandler(fh) logger.setLevel(logging.DEBUG) logger.info(name) shop_logging("auxc")

加上 encoding="utf-8",mode="a" 完美解決中文亂碼的問題orm

相關文章
相關標籤/搜索