logging.conf:---------------------------# logging.conf[loggers]keys=root,test,test2[handlers]keys=consoleHandler,rotateFileHandler[formatters]keys=simpleFormatter[formatter_simpleFormatter]format=[%(asctime)s](%(levelname)s)%(name)s : %(message)s[logger_root]level=DEBUGhandlers=consoleHandler,rotateFileHandler[logger_test]level=DEBUGhandlers=consoleHandler,rotateFileHandlerqualname=testpropagate=0[logger_test2]level=DEBUGhandlers=consoleHandler,rotateFileHandlerqualname=test2propagate=0[handler_consoleHandler]class=StreamHandlerlevel=DEBUGformatter=simpleFormatterargs=(sys.stdout,)[handler_rotateFileHandler]class=handlers.RotatingFileHandlerlevel=DEBUGformatter=simpleFormatterargs=('test.log', 'a', 20000, 9)----------------------------------------------my_test:----------------------------------------------#python33from tkinter import *import logging import logging.configdef Log(text):logging.config.fileConfig("logging.conf")logger = loggong.getLogger("test")logger.info(text)def Test():Log("Test")if __name__=="__main__":root = Tk()btn1 = Button(root, text='test', command=Test)btn1.grid()root.mainloop()
1 回答

交互式愛(ài)情
TA貢獻(xiàn)1712條經(jīng)驗(yàn) 獲得超3個(gè)贊
原生loggging類 + TimedRotatingFileHandler類 實(shí)現(xiàn)按day hour second 切分 import logging from logging.handlers import TimedRotatingFileHandler log = logging.getLogger(loggerName) formatter = logging.Formatter( '%(name)-12s %(asctime)s level-%(levelname)-8s thread-%(thread)-8d %(message)s' ) # 每行日志的前綴設(shè)置 fileTimeHandler = TimedRotatingFileHandler(BASIC_LOG_PATH + filename, "S" , 1 , 10 ) fileTimeHandler.suffix = "%Y%m%d.log" #設(shè)置 切分后日志文件名的時(shí)間格式 默認(rèn) filename+"." + suffix 如果需要更改需要改logging 源碼 fileTimeHandler.setFormatter(formatter) logging.basicConfig(level = logging.INFO) fileTimeHandler.setFormatter(formatter) log.addHandler(fileTimeHandler) try : log.error(msg) except Exception, e: print "writeLog error" finally : log.removeHandler(fileTimeHandler) |
值 interval的類型
S 秒
M 分鐘
H 小時(shí)
D 天
W 周
midnight 在午夜
添加回答
舉報(bào)
0/150
提交
取消