首页 > 编程语言 > 详细

python logging模块日志回滚RotatingFileHandler

时间:2019-03-16 00:35:42      阅读:29      评论:0      收藏:0      [点我收藏+]

标签:ict   admin   etc   turn   icc   for   stdout   date   nco   

# coding=utf-8
import logging
import time
import os
import logging.handlers

def logger(appname,rootstdout=True):

    log_fmt= "%(asctime)s --%(name)s [%(levelname)s]:\n%(message)s"
    c_fmt="%(asctime)s --%(name)s [%(levelname)s]:\n%(message)s"
    date_format = "%Y-%m-%d %H:%M:%S %a"
    #设置Console输出level
    logging.basicConfig(level=logging.DEBUG,
                            format=c_fmt,
                            datefmt=date_format,

                            )

    list_level=["Error","Info","Warning","Debug"]
    stamp = time.strftime("%Y%m%d", time.localtime())+".log"
    logsdir=os.path.join(os.getcwd(),"logs")
    if os.path.exists(logsdir):
        for p in list_level:
            if os.path.exists(os.path.join(logsdir,p)):
                pass
            else:
                os.mkdir(os.path.join(logsdir,p))
    else:
        os.mkdir(logsdir)
        list_level=["Error","Info","Warning","Debug"]
        for p in list_level:
             print(os.path.join(logsdir,p))
             os.mkdir(os.path.join(logsdir,p))

    f_dict={}
    for i in list_level:
        filename=os.path.join(logsdir,i,stamp)
        f_dict[i]=filename
    logger= logging.getLogger(SshTest.class)

    for k,v in f_dict.items():
            handler=logging.handlers.RotatingFileHandler(filename=v, maxBytes=1024*1024*50, backupCount=5,encoding="utf-8",delay=False)
            h_fmt=logging.Formatter(log_fmt)
            handler.setFormatter(h_fmt)
            if k==list_level[0]:
                handler.setLevel(logging.ERROR)
            elif k==list_level[1]:
                handler.setLevel(logging.INFO)
            elif k== list_level[2]:
                 handler.setLevel(logging.WARNING)
            else :
                 handler.setLevel(logging.DEBUG)
            logger.addHandler(handler)
    logger.propagate = rootstdout
    return  logger

if __name__ == "__main__":
        logger=logger("root",rootstdout=True)
        while True:
            time.sleep(0.01)
            logger.info("file test",exc_info=True)
            logger.debug("bebug test")
            logger.error("error test")
            logger.warning("warning test")

C:\Python37\python.exe C:/Users/Administrator/PycharmProjects/Supro/src/rotating_rewrite.py
2019-03-21 23:32:40 Thu --SshTest.class [INFO]:
file test
NoneType: None
2019-03-21 23:32:40 Thu --SshTest.class [DEBUG]:
bebug test
2019-03-21 23:32:40 Thu --SshTest.class [ERROR]:
error test
2019-03-21 23:32:40 Thu --SshTest.class [WARNING]:
warning test
2019-03-21 23:32:40 Thu --SshTest.class [INFO]:
file test
NoneType: None
2019-03-21 23:32:40 Thu --SshTest.class [DEBUG]:

 

python logging模块日志回滚RotatingFileHandler

标签:ict   admin   etc   turn   icc   for   stdout   date   nco   

原文:https://www.cnblogs.com/SunshineKimi/p/10540178.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号