美文网首页
Python日志系统的使用(基础版)

Python日志系统的使用(基础版)

作者: 啊阿伟啊 | 来源:发表于2017-07-05 11:27 被阅读0次

    Python的日志是通过logging模块实现的

    一、日志的基本使用

    1. 日志输出到控制台

    import logging
    
    logging.warning('This is a warning message')  # will print a message to the console
    logging.info('This is a nomal message')  # will not print anything
    

    运行结果:

    WARNING:root:This is a warning message
    

    日志的默认级别是warning,所以上例中,只有warning的信息打印出来。

    1. 日志输出到文件

    import logging
    
    logging.basicConfig(filename='example.log',level=logging.DEBUG)
    logging.debug('This is a debug message')
    logging.info('This is a nomal message')
    logging.warning('This is a warning message')
    

    运行代码后,会在根目录生成一个example.log,打开该日志文件,可以看到如下结果:

    DEBUG:root:This is a debug message
    INFO:root:This is a nomal message
    WARNING:root:This is a warning message
    

    注意:basicConfig()调用前不能进行任何日志的输出,否则basicConfig()的配置信息全部失效。

    二、多模块使用日志

    1. 日志输出到一个终端

    # myapp.py
    import logging
    import mylib
    
    def main():
        logging.basicConfig(filename='myapp.log', level=logging.INFO)
        logging.info('In myapp.py, Started')
        mylib.do_something()
        logging.info('In myapp.py, Finished')
    
    if __name__ == '__main__':
        main()
    
    # mylib.py
    import logging
    
    def do_something():
        logging.info('In mylib.py, Doing something')
    

    运行myapp.py后,打开生成的日志文件myapp.log,可以看到如下结果:

    INFO:root:In myapp.py, Started
    INFO:root:In mylib.py, Doing something
    INFO:root:In myapp.py, Finished
    

    从运行结果可以看出,如上例实现的多模块日志,会根据方法调用的先后顺序打印日志信息。

    2. 日志输出到一个终端

    上面的例子都只能输出到一个终端,要么是控制台,要么是日志文件。
    下面的例子将会展示如何将日志信既输出到控制台,又输出到日志文件中:

    # myapp.py
    import logging
    import mylib
    
    # create logger with 'myapp'
    logger = logging.getLogger('myapp')
    logger.setLevel(logging.DEBUG)
    # create file handler which logs even debug messages
    # file mode set write. myapp.log will be rewrite when every time myapp.py is runned.
    fh = logging.FileHandler('myapp.log', mode='w')
    fh.setLevel(logging.DEBUG)
    # create console handler with a higher log level
    ch = logging.StreamHandler()
    ch.setLevel(logging.ERROR)
    # create formatter and add it to the handlers
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    fh.setFormatter(formatter)
    ch.setFormatter(formatter)
    # add the handlers to the logger
    logger.addHandler(fh)
    logger.addHandler(ch)
    
    logger.info('In myapp.py, Started')
    logger.error('This is a error message')
    mylib.do_something()
    logger.info('In myapp.py, Finished')
    
    # mylib.py
    import logging
    
    # getLogger named myapp
    mylib_logger = logging.getLogger('myapp')
    
    def do_something():
        mylib_logger.info('In mylib.py, Doing something')
    

    运行myapp.py,因为控制台的日志级别为ERROR,所以控制台只输出ERROR级别的信息:

    2017-07-05 11:07:36,721 - myapp - ERROR - This is a error message
    

    因为日志文件的日志级别为DEBUG,所以文件中包含DEBUGDEBUG以上级别的信息:

    2017-07-05 11:07:36,721 - myapp - INFO - In myapp.py, Started
    2017-07-05 11:07:36,721 - myapp - ERROR - This is a error message
    2017-07-05 11:07:36,721 - myapp - INFO - In mylib.py, Doing something
    2017-07-05 11:07:36,721 - myapp - INFO - In myapp.py, Finished
    

    了解以上内容就可以实现基本的日志功能了,但是如果想要有效的使用Python的日志功能,还有很多东西需要学习。

    相关文章

      网友评论

          本文标题:Python日志系统的使用(基础版)

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