栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

每次启动应用程序时旋转日志文件(Python)

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

每次启动应用程序时旋转日志文件(Python)

如果

RotatingFileHandler
没有
maxBytes
,我可能足够使用,然后调用
doRollover()
应用程序启动。

是的,似乎工作正常。下面的代码将在每次运行应用程序时创建一个新的日志文件,并为日志的开始和关闭时间添加时间戳。运行它将打印可用日志文件的列表。您可以检查它们以检查正确的行为。改编自Python
docs示例:

import osimport globimport loggingimport logging.handlersimport timeLOG_FILENAME = 'logging_rotatingfile_example.out'# Set up a specific logger with our desired output levelmy_logger = logging.getLogger('MyLogger')my_logger.setLevel(logging.DEBUG)# Check if log exists and should therefore be rolledneedRoll = os.path.isfile(LOG_FILENAME)# Add the log message handler to the loggerhandler = logging.handlers.RotatingFileHandler(LOG_FILENAME, backupCount=50)my_logger.addHandler(handler)# This is a stale log, so roll itif needRoll:        # Add timestamp    my_logger.debug('n---------nLog closed on %s.n---------n' % time.asctime())    # Roll over on application start    my_logger.handlers[0].doRollover()# Add timestampmy_logger.debug('n---------nLog started on %s.n---------n' % time.asctime())# Log some messagesfor i in xrange(20):    my_logger.debug('i = %d' % i)# See what files are createdlogfiles = glob.glob('%s*' % LOG_FILENAME)print 'n'.join(logfiles)


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/516861.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号