简单日志按天滚动,加入apsheduler,用crontab模式按小时运行测试:
import logging
from logging.handlers import TimedRotatingFileHandler
from apscheduler.schedulers.blocking import BlockingScheduler
logFilePath = 'spark_streaming_opinion.log' logger = logging.getLogger()
logger.setLevel(logging.INFO)
handler = TimedRotatingFileHandler(logFilePath,
when = 'd',
interval = 1,
backupCount=7)
formatter = logging.Formatter('[%(asctime)s-%(levelname)s - %(message)s]')
handler.setFormatter(formatter)
logger.addHandler(handler)
def testLogRun():
for i in range(2):
logger.debug('This is debug message')
logger.info('This is info message')
logger.warning('This is warning message') if __name__ == '__main__':
shed = BlockingScheduler()
shed.add_job(testLogRun, 'cron', hour = '*')
try:
shed.start()
except SystemExit:
shed.shutdown(wait=False)
作 者:小闪电
出处:http://www.cnblogs.com/yueyanyu/
本文版权归作者和博客园共有,欢迎转载、交流,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。如果觉得本文对您有益,欢迎点赞、欢迎探讨。本博客来源于互联网的资源,若侵犯到您的权利,请联系博主予以删除。