python寫入日誌檔案時日誌內容重複寫入

2022-06-15 19:48:13 字數 4042 閱讀 6860

python寫入日誌檔案時日誌內容重複寫入:原因handler未移除,導致重複寫入

問了度娘之後,大概搞明白了,就是你第二次呼叫log的時候,根據getlogger(name)裡的name獲取同乙個logger,而這個logger裡已經有了第一次你新增的handler,第二次呼叫又新增了乙個handler,所以,這個logger裡有了兩個同樣的handler,以此類推,呼叫幾次就會有幾個handler。。

修改前**:

1

#coding:utf-8

2import

logging

3from logging import

handlers45

6class

logger():

7 level_relations =

9 fmt_str = "

%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s:%(message)s"10

def__init__(self, filename, level="

info

", when="

d", backcount=2, fmt=fmt_str):

11 self.logger =logging.getlogger(filename)

12 format_str =logging.formatter(fmt)

13self.logger.setlevel(self.level_relations.get(level))

14 self.th = handlers.timedrotatingfilehandler(filename, when=when, backupcount=backcount, encoding="

utf-8")

15self.th.setformatter(format_str)

1617

def log_write(self, log_text, level="

info"):

18self.logger.addhandler(self.th)

19if level == "

debug

"or level == "

debug":

20self.logger.debug(log_text)

21elif level == "

info

"or level == "

info":

22self.logger.info(log_text)

23elif level == "

warning

"or level == "

warning":

24self.logger.warning(log_text)

25elif level == "

error

"or level == "

error":

26self.logger.error(log_text)

27elif level == "

critical

"or level == "

critical":

28self.logger.critical(log_text)

29else:30

raise ("

日誌級別錯誤")

3132

33if

__name__ == '

__main__':

34 log1 = logger("

text.log")

35 log1.log_write("

test4-0")

36 log2 = logger("

text.log")

37 log2.log_write("

test4-1")

38 log3 = logger("

text.log")

39 log3.log_write("

test4-2

")

執行結果:

檔案中日誌內容列印了多次

修改後的**:

1

#coding:utf-8

2import

logging

3from logging import

handlers45

6class

logger():

7 level_relations =

9 fmt_str = "

%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s:%(message)s"10

def__init__(self, filename, log_text, level="

info

", when="

d", backcount=2, fmt=fmt_str):

11 self.logger =logging.getlogger(filename)

12 format_str =logging.formatter(fmt)

13self.logger.setlevel(self.level_relations.get(level))

14 self.th = handlers.timedrotatingfilehandler(filename, when=when, backupcount=backcount, encoding="

utf-8")

15self.th.setformatter(format_str)

1617

def log_write(self, log_text, level="

info"):

18self.logger.addhandler(self.th)

19if level == "

debug

"or level == "

debug":

20self.logger.debug(log_text)

21elif level == "

info

"or level == "

info":

22self.logger.info(log_text)

23elif level == "

warning

"or level == "

warning":

24self.logger.warning(log_text)

25elif level == "

error

"or level == "

error":

26self.logger.error(log_text)

27elif level == "

critical

"or level == "

critical":

28self.logger.critical(log_text)

29else:30

raise ("

日誌級別錯誤")

31 self.logger.removehandler(self.th) #

日誌寫入完成後移除handler

3233

34if

__name__ == '

__main__':

35 log = logger("

text.log

", "

test4-1")

36 log = logger("

text.log

", "

test4-2")

37 log = logger("

text.log

", "

test4-3

")

flask將日誌寫入日誌檔案

import logging logging.basicconfig level logging.debug,控制台列印的日誌級別 filename log new.log 將日誌寫入log new.log檔案中 filemode a 模式,有w和a,w就是寫模式,每次都會重新寫日誌,覆蓋之前的日誌...

SYLogManager實時日誌

sylogmanager log日誌檢視工具。特點 檢視日誌按鈕可拖動的任意位置 使用介紹 示例 匯入標頭檔案 import sylogmanager.h 初始化配置 sylogmanager.sharelog config sylogmanager.sharelog.email 151311301...

java寫入日誌檔案

首先要定義日誌的四個等級 info,debug,error,fatal,還要建日誌輸出方式,如console,file,html,database 以上資訊可以放到.properties檔案中。建類,來實現各種方式的輸出。如 private static log log new log privat...