zoukankan      html  css  js  c++  java
  • python logging system

    官方教程:https://docs.python.org/2/library/logging.html 

    1.  用法1

    import logging
    import logging.handlers
    
    LOG_FILE = 'tst.log'
    
    handler = logging.handlers.RotatingFileHandler(LOG_FILE, maxBytes = 1024*1024, backupCount = 5) # 实例化handler 
    fmt = '%(asctime)s - %(filename)s:%(lineno)s - %(name)s - %(message)s'
    
    formatter = logging.Formatter(fmt)   # 实例化formatter
    handler.setFormatter(formatter)      # 为handler添加formatter
    
    logger = logging.getLogger('tst')    # 获取名为tst的logger
    logger.addHandler(handler)           # 为logger添加handler
    logger.setLevel(logging.DEBUG)
    
    logger.info('first info message')
    logger.debug('first debug message')
    

    formatter

      采用的是%(<dict key>)s的形式,就是字典的关键字替换。提供的关键字包括:

    FormatDescription
    %(name)s Name of the logger (logging channel).
    %(levelno)s Numeric logging level for the message (DEBUGINFOWARNINGERRORCRITICAL).
    %(levelname)s Text logging level for the message ('DEBUG''INFO''WARNING''ERROR''CRITICAL').
    %(pathname)s Full pathname of the source file where the logging call was issued (if available).
    %(filename)s Filename portion of pathname.
    %(module)s Module (name portion of filename).
    %(funcName)s Name of function containing the logging call.
    %(lineno)d Source line number where the logging call was issued (if available).
    %(created)f Time when the LogRecord was created (as returned by time.time()).
    %(relativeCreated)d Time in milliseconds when the LogRecord was created, relative to the time the logging module was loaded.
    %(asctime)s Human-readable time when the LogRecord was created. By default this is of the form “2003-07-08 16:49:45,896” (the numbers after the comma are millisecond portion of the time).
    %(msecs)d Millisecond portion of the time when the LogRecord was created.
    %(thread)d Thread ID (if available).
    %(threadName)s Thread name (if available).
    %(process)d Process ID (if available).
    %(message)s The logged message, computed as msg args.

     level

    The numeric values of logging levels are given in the following table. These are primarily of interest if you want to define your own levels, and need them to have specific values relative to the predefined levels. If you define a level with the same numeric value, it overwrites the predefined value; the predefined name is lost.

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

     如果指定level 为INFO, 则小于它的所有message都不会打印出来。

    2. 用法2

      采用配置文件的方式去配置logging system, 这种方法简单方便,可以在多模块间调用而不需要传入logger对象。

    现在我准备一个配置文件logging.conf如下

    [loggers]
    keys=root,uploader,msgChanger   #
    
    [handlers]
    keys=consoleHandler,uploaderHandler,msgChangerHandler
    
    [formatters]
    keys=fmt
    
    [logger_root]
    level=INFO
    handlers=consoleHandler
    qualname=root
    
    [logger_uploader]
    level=INFO
    qualname=uploader
    handlers=uploaderHandler
    
    [logger_msgChanger]
    level=INFO
    qualname=msgChanger
    handlers=msgChangerHandler
    
    [handler_consoleHandler]
    class=StreamHandler
    level=INFO
    formatter=fmt
    args=(sys.stdout,)
    
    [handler_uploaderHandler]
    class=logging.handlers.RotatingFileHandler
    level=INFO
    formatter=fmt
    args=('log/uploader.log','w',1024*1024*1024,5,)
    
    [handler_msgChangerHandler]
    class=logging.handlers.RotatingFileHandler
    level=INFO
    formatter=fmt
    args=('log/msg_changer.log','w',1024*1024*1024,5,)
    
    [formatter_fmt]
    format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
    datefmt=
    
    

     called in code:

    '''init log module'''
        logging.config.fileConfig('config/logging.conf')   # parse config file as above   
        msgChanger_logger = logging.getLogger('msgChanger.main') # get a logger instance, msgChanger must be included 
      msgChanger_logger.info('test log system in main.')

     可以在任何module下写入上述代码调用logger, 只需要传入一个名字就好了,名字必须在config文件里配置了的,否则实例失败。

  • 相关阅读:
    js相关小实例——大图轮播
    js相关小实例——div实现下拉菜单
    js相关小实例——二级菜单
    html5部分相关
    CSS3常用属性(边框、背景、文本效果、2D转换、3D转换、过渡、有过渡效果大图轮播、动画)
    数据访问
    php测试
    单例模式
    Doc
    横竖列表 下拉隐藏显示
  • 原文地址:https://www.cnblogs.com/lovemo1314/p/3812572.html
Copyright © 2011-2022 走看看