Python logging

日志等级

从高到低分别有以下几种:
CRITICAL : ‘CRITICAL’, 50
ERROR : ‘ERROR’, 40
WARNING : ‘WARNING’, 30
INFO : ‘INFO’, 20
DEBUG : ‘DEBUG’, 10
比所选等级低的日志记录不会输出,选择DEBUG将输出所有日志。

示例

import logging

def init_log(log_path, log_name="Spider.crawler", level=logging.INFO, when="D", backup=7,
             format="%(levelname)s: %(asctime)s: %(filename)s:%(lineno)d * %(thread)d %(message)s", 
             datefmt="%m-%d %H:%M:%S"):
    """
    init_log - initialize log module

    Args:
      log_path      - Log file path prefix.
                      Log data will go to two files: log_path.log and log_path.log.wf
                      Any non-exist parent directories will be created automatically
      level         - msg above the level will be displayed
                      DEBUG < INFO < WARNING < ERROR < CRITICAL
                      the default value is logging.INFO
      when          - how to split the log file by time interval
                      'S' : Seconds
                      'M' : Minutes
                      'H' : Hours
                      'D' : Days
                      'W' : Week day
                      default value: 'D'
      format        - format of the log
                      default format:
                      %(levelname)s: %(asctime)s: %(filename)s:%(lineno)d * %(thread)d %(message)s
                      INFO: 12-09 18:02:42: log.py:40 * 139814749787872 HELLO WORLD
      backup        - how many backup file to keep
                      default value: 7

    Raises:
        OSError: fail to create log directories
        IOError: fail to open log file
    """
    # log文件夹建立
    dir = os.path.dirname(log_path) # 去掉文件名返回路径
    if not os.path.isdir(dir):
        os.makedirs(dir)
        
    formatter = logging.Formatter(format, datefmt) # 输出格式和时间格式
    logger = logging.getLogger(log_name) # 模块初始化
    logger.setLevel(level)

	# info
    handler = logging.handlers.TimedRotatingFileHandler(log_path + ".log",
                                                        when=when,
                                                        backupCount=backup)
    handler.setLevel(level)
    handler.setFormatter(formatter)
    logger.addHandler(handler)
	
	# warn
    handler = logging.handlers.TimedRotatingFileHandler(log_path + ".log.wf",
                                                        when=when,
                                                        backupCount=backup)
    handler.setLevel(logging.WARNING)
    handler.setFormatter(formatter)
    logger.addHandler(handler)

    ch = logging.StreamHandler() # 日志信息会输出到指定的stream中,为空则默认输出到sys.stderr
    ch.setLevel(level)  
    ch.setFormatter(formatter)
    logger.addHandler(ch)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值