美文网首页
python_logging模块

python_logging模块

作者: Frank_8942 | 来源:发表于2019-06-21 01:56 被阅读0次
    def initLogging():
        logging.basicConfig(level=logging.INFO,
                            filename="{}_log.txt".format(__file__[:-3]),
                            filemode='a',
                            format='%(asctime)s-%(filename)s[line:%(lineno)d]-%(levelname)s: %(message)s')
    
    initLogging()
    logging.info('这是 loggging info message')
    logging.debug('这是 loggging debug message')
    logging.warning('这是 loggging a warning message')
    logging.error('这是 an loggging error message')
    logging.critical('这是 loggging critical message')
    logging.info("{}_log.txt".format(__file__[:-3]))
    
    
    #result
    2019-06-21 01:54:33,421-LoggingDemo.py[line:13]-INFO: 这是 loggging info message
    2019-06-21 01:54:33,421-LoggingDemo.py[line:15]-WARNING: 这是 loggging a warning message
    2019-06-21 01:54:33,421-LoggingDemo.py[line:16]-ERROR: 这是 an loggging error message
    2019-06-21 01:54:33,421-LoggingDemo.py[line:17]-CRITICAL: 这是 loggging critical message
    2019-06-21 01:54:33,421-LoggingDemo.py[line:19]-INFO: G:/MyCode/study_python/package/frank/LoggingDemo_log.txt
    
    
    1. 日志级别
    日志一共分成5个等级,从低到高分别是:
    DEBUG  INFO   WARNING   ERROR   CRITICAL
    这5个等级,也分别对应5种打日志的方法: debug 、info 、warning 、error 、critical。
    默认的是WARNING,当在WARNING或之上时才被跟踪。
    
    2. 日志格式说明
    logging.basicConfig函数中,可以指定日志的输出格式format,这个参数可以输出很多有用的信息,如下:
    %(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: 打印日志信息
    
    
    在工作中给的常用格式如下:
        format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s'
    这个格式可以输出日志的打印时间,是哪个模块输出的,输出的日志级别是什么,以及输入的日志内容。
    
    注意:
    只要用过一次log功能再次设置格式时将失效,
    实际开发中格式肯定不会经常变化,所以刚开始时需要设定好格式
    
    #源码
    def basicConfig(**kwargs):
    Do basic configuration for the logging system.
    
    This function does nothing if the root logger already has handlers
    configured. It is a convenience method intended for use by simple scripts
    to do one-shot configuration of the logging package.
    
    The default behaviour is to create a StreamHandler which writes to
    sys.stderr, set a formatter using the BASIC_FORMAT format string, and
    add the handler to the root logger.
    
    A number of optional keyword arguments may be specified, which can alter
    the default behaviour.
    
    filename  Specifies that a FileHandler be created, using the specified
              filename, rather than a StreamHandler.
    filemode  Specifies the mode to open the file, if filename is specified
              (if filemode is unspecified, it defaults to 'a').
    format    Use the specified format string for the handler.
    datefmt   Use the specified date/time format.
    style     If a format string is specified, use this to specify the
              type of format string (possible values '%', '{', '$', for
              %-formatting, :meth:`str.format` and :class:`string.Template`
              - defaults to '%').
    level     Set the root logger level to the specified level.
    stream    Use the specified stream to initialize the StreamHandler. Note
              that this argument is incompatible with 'filename' - if both
              are present, 'stream' is ignored.
    handlers  If specified, this should be an iterable of already created
              handlers, which will be added to the root handler. Any handler
              in the list which does not have a formatter assigned will be
              assigned the formatter created in this function.
    
    Note that you could specify a stream created using open(filename, mode)
    rather than passing the filename and mode in. However, it should be
    remembered that StreamHandler does not close its stream (since it may be
    using sys.stdout or sys.stderr), whereas FileHandler closes its stream
    when the handler is closed.
    

    相关文章

      网友评论

          本文标题:python_logging模块

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