美文网首页
2019-05-28

2019-05-28

作者: 数据小黑升值记 | 来源:发表于2019-05-28 21:17 被阅读0次

    Python中的日志模块

    logging使用

    import logging
    logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    logger = logging.getLogger(__name__)
    
    logger.info("Start print log")
    logger.debug("Do something")
    logger.warning("Something maybe fail.")
    logger.info("Finish")
    

    参数:作用
    %(levelno)s:打印日志级别的数值
    %(levelname)s:打印日志级别的名称
    %(pathname)s:打印当前执行程序的路径,其实就是sys.argv[0]
    %(filename)s:打印当前执行程序名
    %(funcName)s:打印日志的当前函数
    %(lineno)d:打印日志的当前行号
    %(asctime)s:打印日志的时间
    %(thread)d:打印线程ID
    %(threadName)s:打印线程名称
    %(process)d:打印进程ID
    %(message)s:打印日志信息

    日志回滚

    import logging
    from logging.handlers import RotatingFileHandler
    logger = logging.getLogger(__name__)
    logger.setLevel(level = logging.INFO)
    rHandler = RotatingFileHandler("log.txt",maxBytes = 1*1024,backupCount = 3)
    rHandler.setLevel(logging.INFO)
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    rHandler.setFormatter(formatter)
    
    console = logging.StreamHandler()
    console.setLevel(logging.INFO)
    console.setFormatter(formatter)
    
    logger.addHandler(rHandler)
    logger.addHandler(console)
    
    logger.info("Start print log")
    logger.debug("Do something")
    logger.warning("Something maybe fail.")
    logger.info("Finish")
    

    相关文章

      网友评论

          本文标题:2019-05-28

          本文链接:https://www.haomeiwen.com/subject/axijtctx.html