python 日志记录logging

1.logging基本类说明

  • Loggers 类,提供实际应用中的直接调用接口
  • Handlers 类,将日志内容送到合适的地方,如控制台,文件等
  • Filters 类,提供过滤比日志级别更细致的过滤,如从内容字段等.
  • Formatters 类,提供输出的格式.

    import logging
    
    # 创建一个logger
    logger = logging.getLogger('simple_example')
    logger.setLevel(logging.DEBUG) #最低的日志级别,大于此级别的都记录,下同
    
    # 创建一个handler,用于写入日志文件
    fh = logging.FileHandler('spam.log')
    fh.setLevel(logging.DEBUG)
    
     # 再创建一个handler,用于输出到控制台
    ch = logging.StreamHandler()
    ch.setLevel(logging.ERROR)
    
    # 定义handler的输出格式
    formatter = logging.Formatter(
        '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    ch.setFormatter(formatter)
    fh.setFormatter(formatter)
    
    # 给logger添加handler
    logger.addHandler(ch)
    logger.addHandler(fh)
    
    # 记录日志
    logger.debug('debug message')
    logger.info('info message')
    logger.warn('warn message')
    logger.error('error message')
    logger.critical('critical message')
    

2.日志的级别

日志的级别:

Level   Numeric value
CRITICAL    50
ERROR       40
WARNING     30
INFO        20
DEBUG       10
NOTSET      0

3.日志格式

默认是:’%(message)s’

有两种格式化方式:

  • {}类型,三种特殊的标志: ‘!s’ 代表求值后调用 str(), ‘!r’ 调用 repr(),’!a’ 调用ascii().

    "First, thou shalt count to {0}"  # References first positional argument
    "Bring me a {}"                   # Implicitly references the first positional argument
    "From {} to {}"                   # Same as "From {0} to {1}"
    "My quest is {name}"              # References keyword argument 'name'
    "Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
    "Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
    
    "Harold's a clever {0!s}"        # Calls str() on the argument first
    "Bring out the holy {name!r}"    # Calls repr() on the argument first
    "More {!a}"                      # Calls ascii() on the argument first
    
    示例:
    >>"The sum of 1 + 2 is {0}".format(1+2)
    'The sum of 1 + 2 is 3'
    
  • 模板

    >>> from string import Template
    >>> s = Template('$who likes $what')
    >>> s.substitute(who='tim', what='kung pao')
    'tim likes kung pao'
    >>> d = dict(who='tim')
    >>> Template('Give $who $100').substitute(d)
    Traceback (most recent call last):
    ...
    ValueError: Invalid placeholder in string: line 1, col 11
    >>> Template('$who likes $what').substitute(d)
    Traceback (most recent call last):
    ...
    KeyError: 'what'
    >>> Template('$who likes $what').safe_substitute(d)
    'tim likes $what'
    

所有可用的属性字段:

参考:

  1. my coding.net
  2. python标准日志模块logging及日志系统设计
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

baibingql

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值