學(xué)習(xí)
Python
不久碰到過(guò)這個(gè)問(wèn)題, 記得當(dāng)時(shí)沒(méi)查出是什么問(wèn)題. 剛剛無(wú)意中發(fā)現(xiàn)了這個(gè)問(wèn)題的原因及解決方案, 記錄一下.
參考:
https://juejin.im/post/5bc2bd3a5188255c94465d31
第一種情況
參考文章中介紹說(shuō)產(chǎn)生這個(gè)問(wèn)題的原因是因?yàn)閯?chuàng)建了自定義的 Logger 對(duì)象后, 又使用了
logging
中的日志輸出方法,這些方法使用的是默認(rèn)配置的 Logger 對(duì)象,導(dǎo)致之后輸出的日志信息會(huì)重復(fù)。
示例代碼:
import logging
# 日志管理
logger = logging.getLogger('logger')
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s')
# 輸出到控制臺(tái)的log
sh = logging.StreamHandler()
sh.setLevel(logging.DEBUG)
sh.setFormatter(formatter)
logger.addHandler(sh)
logger.debug('a debug message')
logger.info('a info message')
logger.warning('a warning message')
logging.error('a error message')
logger.critical('a critical message')
打印結(jié)果如下:
2019-07-17 09:42:29,336 spider.py[line:92] DEBUG a debug message
2019-07-17 09:42:29,336 spider.py[line:93] INFO a info message
2019-07-17 09:42:29,336 spider.py[line:94] WARNING a warning message
ERROR:root:a error message
2019-07-17 09:42:29,336 spider.py[line:96] CRITICAL a critical message
CRITICAL:logger:a critical message
[Finished in 0.6s]
可以看到從
error message
開始出現(xiàn)格式異常, 之后下一條
message
開始重復(fù)打印. 之后每一條
log
都會(huì)打印出兩次. 去掉使用
logging
的日志輸出方法后則打印正常.
第二種情況
封裝了
logger
到文件后在其他文件
import
引用這個(gè)
logger
. 卻又在文件中創(chuàng)建了新的
logger
. 如下:
封裝了
logger
的
base_utils
:
import configparser
import logging
import time
import os
# 配置文件管理
confLoad = configparser.ConfigParser()
confLoad.read('./spider.conf')
# 日志管理
logger = logging.getLogger('logger')
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s')
# 輸出到log目錄下的當(dāng)日l(shuí)og文件
log_dir = confLoad.get('logger','LOG_DIR')
if not os.path.exists(log_dir):
os.makedirs(log_dir)
logFile = log_dir + time.strftime("%Y-%m-%d", time.localtime()) + ".log"
fh = logging.FileHandler(logFile, encoding='utf-8')
fh.setLevel(int(confLoad.get('logger','LOGFILE_LEVEL')))
fh.setFormatter(formatter)
# 輸出到控制臺(tái)的log
sh = logging.StreamHandler()
sh.setLevel(int(confLoad.get('logger','CONSOLE_LEVEL')))
sh.setFormatter(formatter)
logger.addHandler(fh)
logger.addHandler(sh)
配置文件
spider.conf
:
# mongodb
[mongodb]
DB_MONGO_HOST=192.168.0.201
DB_MONGO_PORT=27017
DB_MONGO_DATABASE=foxsaas_spider
DB_MONGO_USERNAME=
DB_MONGO_PASSWORD=
[logger]
LOG_DIR = ./log/
# CRITICAL = 50
# FATAL = CRITICAL
# ERROR = 40
# WARNING = 30
# WARN = WARNING
# INFO = 20
# DEBUG = 10
# NOTSET = 0
CONSOLE_LEVEL = 10
LOGFILE_LEVEL = 10
在文件
spider.py
中引用:
from base_utils import logger
if __name__ == "__main__":
# 如果在此文件中再次創(chuàng)建 logger, 會(huì)產(chǎn)生兩個(gè) logger 對(duì)象, 使 log 打印 2 次
logger = logging.getLogger('logger')
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s')
sh = logging.StreamHandler()
sh.setLevel(logging.DEBUG)
sh.setFormatter(formatter)
logger.addHandler(sh)
logger.debug('a debug message')
logger.info('a info message')
logger.warning('a warning message')
logger.error('a error message')
logger.critical('a critical message')
打印結(jié)果如下:
2019-07-17 10:01:21,969 spider.py[line:93] DEBUG a debug message
2019-07-17 10:01:21,969 spider.py[line:93] DEBUG a debug message
2019-07-17 10:01:21,970 spider.py[line:94] INFO a info message
2019-07-17 10:01:21,970 spider.py[line:94] INFO a info message
2019-07-17 10:01:21,970 spider.py[line:95] WARNING a warning message
2019-07-17 10:01:21,970 spider.py[line:95] WARNING a warning message
2019-07-17 10:01:21,971 spider.py[line:96] ERROR a error message
2019-07-17 10:01:21,971 spider.py[line:96] ERROR a error message
2019-07-17 10:01:21,971 spider.py[line:97] CRITICAL a critical message
2019-07-17 10:01:21,971 spider.py[line:97] CRITICAL a critical message
[Finished in 0.6s]
可以看到
log
打印了 2 次, 同樣的要避免這樣子使用.
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
