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 回答
交互式爱情
TA贡献1712条经验 获得超3个赞
原生loggging类 + TimedRotatingFileHandler类 实现按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' ) # 每行日志的前缀设置 fileTimeHandler = TimedRotatingFileHandler(BASIC_LOG_PATH + filename, "S" , 1 , 10 ) fileTimeHandler.suffix = "%Y%m%d.log" #设置 切分后日志文件名的时间格式 默认 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 小时
D 天
W 周
midnight 在午夜
添加回答
举报
0/150
提交
取消