python logging模塊按天滾動簡單程序


簡單日志按天滾動,加入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/ 

本文版權歸作者和博客園共有,歡迎轉載、交流,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接。如果覺得本文對您有益,歡迎點贊、歡迎探討。本博客來源於互聯網的資源,若侵犯到您的權利,請聯系博主予以刪除。


 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM