• python的logging模块


    python提供了一个日志处理的模块,那就是logging
    导入logging模块使用以下命令;

    import logging
    

    logging模块的用法:

    1.简单的将日志打印到屏幕上

    import logging
    
    logging.debug("This is debug message")
    logging.info("This is info message")
    logging.warning("This is warning message")
    logging.error("This is error message")
    logging.critical("This is critical message")
    

    会在屏幕上显示出以下内容:

    WARNING:root:This is warning message
    ERROR:root:This is error message
    CRITICAL:root:This is critical message
    

    默认情况下python的logging模块将日志打印到了标准输出中,也就是屏幕上,且只显示了大于等于WARNING级别的日志.
    这说明默认的日志级别设置为WARNING(日志级别等级CRITICAL > ERROR > WARNING > INFO > DEBUG)
    默认直接输出的日志格式为日志级别:Logger名称:用户:输出消息。

    2.现在修改日志的默认输出级别为debug,重新设定输出时间的格式,

    import logging
    
    logging.basicConfig(level=logging.DEBUG,
                        format="%(asctime)s %(levelname)s %(message)s",
                        datefmt="%Y-%m-%d %H:%M:%S")
    
    logging.debug("This is debug message")
    logging.info("This is info message")
    logging.warning("This is warning message")
    logging.error("This is error message")
    logging.critical("This is critical message")
    

    会在屏幕上显示以下信息;

    2017-07-02 10:41:18 DEBUG This is debug message
    2017-07-02 10:41:18 INFO This is info message
    2017-07-02 10:41:18 WARNING This is warning message
    2017-07-02 10:41:18 ERROR This is error message
    2017-07-02 10:41:18 CRITICAL This is critical message
    

    3.现在想把程序产生的日志写入文件当中,可以这样设定:

    import logging
    
    logging.basicConfig(level=logging.DEBUG,
                        format="%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s",
                        datefmt="%Y-%m-%d %H:%M:%S",
                        filename="log.txt",
                        filemode="w")
    
    logging.debug("This is debug message")
    logging.info("This is info message")
    logging.warning("This is warning message")
    logging.error("This is error message")
    logging.critical("This is critical message")
    

    运行程序,会在脚本目录下生成一个名为log.txt的文件。
    log.txt文件的内容如下:

    2017-07-02 10:49:13 logging_modules.py[line:211] DEBUG This is debug message
    2017-07-02 10:49:13 logging_modules.py[line:212] INFO This is info message
    2017-07-02 10:49:13 logging_modules.py[line:213] WARNING This is warning message
    2017-07-02 10:49:13 logging_modules.py[line:214] ERROR This is error message
    2017-07-02 10:49:13 logging_modules.py[line:215] CRITICAL This is critical message
    

    在这里设定日志文件的输出使用的是basicConfig这个方法:

    logging.basicConfig函数各参数:
    filename: 指定输出日志的文件名
    filemode: 和file函数意义相同,指定日志文件的打开模式,写入模式用'w',追加模式使用'a'
    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: 打印日志信息
    datefmt: 指定时间格式,同time.strftime()
    level: 设置日志级别,默认为logging.WARNING,这里设定为logging.DEBUG
    

    4.既想现在就看到输出的日志,又想把程序运行的日志保存在文件里,方便以后查看,可以这样设定:

    import logging
    
    logger=logging.getLogger()
    #创建一个file_handle变量,用于把日志写入到文件
    file_handle=logging.FileHandler("log1.txt")
    #创建一个stream_handle变量,用于输出日志到屏幕上
    stream_handle=logging.StreamHandler()
    #设定输出日志的级别为debug级别
    logger.setLevel(logging.DEBUG)
    #设定输出日志的格式
    fmt=logging.Formatter("%(asctime)s-%(levelname)s-%(message)s")
    #为写入文件的日志添加已设定的格式
    file_handle.setFormatter(fmt)
    #为输出到屏幕的日志添加已设定的格式 
    stream_handle.setFormatter(fmt)
    logger.addHandler(file_handle)
    logger.addHandler(stream_handle)
    
    #设定输出日志的信息
    logging.debug("This is debug message")
    logging.info("This is info message")
    logging.warning("This is warning message")
    logging.error("This is error message")
    logging.critical("This is critical message")
    

    运行程序后,会生成一个名为log1.txt的文件,文件的内容和屏幕上显示的内容都是:

    2017-07-02 11:04:53,622-DEBUG-This is debug message
    2017-07-02 11:04:53,623-INFO-This is info message
    2017-07-02 11:04:53,623-WARNING-This is warning message
    2017-07-02 11:04:53,623-ERROR-This is error message
    2017-07-02 11:04:53,624-CRITICAL-This is critical message
    

    在这里,还可以添加以下选项用来指定把要写入文件的日志设定为debug级别,而输出到屏幕上的日志还是warning级别

    fh.setLevel(logging.Debug)
  • 相关阅读:
    旧的非flash版Metalink的入口
    了解rman catalog的兼容性
    Identify ksusetxn DID:An Deadlock ID
    [zt]如何有效地报告Bug
    深入了解ASMM
    解决sqlplus的segmentation fault或hang问题
    [zt]提问的艺术
    oracle 好书 05 ( 内存组件与 oracle 进程 )
    oracle 好书 03 ( 数据字典 )
    Oracle 好书 02 ( 安装oracle 10g软件及创建数据库 )
  • 原文地址:https://www.cnblogs.com/renpingsheng/p/7105488.html
Copyright © 2020-2023  润新知