Python logging模塊寫(xiě)入中文出現(xiàn)亂碼
一下文件執(zhí)行將產(chǎn)生亂碼,切.log文件顯示問(wèn)好,打不開(kāi)
import loggingdef 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')
經(jīng)發(fā)現(xiàn),是沒(méi)有定義文件格式,修復(fù)后
import loggingdef 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' 完美解決中文亂碼的問(wèn)題
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Vue數(shù)組響應(yīng)式操作及高階函數(shù)使用代碼詳解2. IntelliJ IDEA設(shè)置默認(rèn)瀏覽器的方法3. IntelliJ IDEA導(dǎo)入項(xiàng)目的方法4. idea設(shè)置自動(dòng)導(dǎo)入依賴(lài)的方法步驟5. phpstudy apache開(kāi)啟ssi使用詳解6. 刪除docker里建立容器的操作方法7. idea自定義快捷鍵的方法步驟8. idea刪除項(xiàng)目的操作方法9. Intellij Idea 多模塊Maven工程中模塊之間無(wú)法相互引用問(wèn)題10. IntelliJ IDEA配置Tomcat服務(wù)器的方法

網(wǎng)公網(wǎng)安備