• logging模块介绍


    在我们的实际开发过程中,我们有时候需要记录一些重要操作,或者程序运行情况,我们就需要在程序里面写入日志,来达到更快的排错跟记录重要操作的目的。在Python中logging模块就很好的解决了这个问题,当然logging模块中常见的有5中日志记录模式,两种配置方式。

    1,日志记录模式

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

    import logging  
    logging.debug('debug message')  
    logging.info('info message')  
    logging.warning('warning message')  
    logging.error('error message')  
    logging.critical('critical message') 

    2,basicconfig配置方式

    import logging  
    logging.basicConfig(level=logging.DEBUG,  # 设置日志级别(大于等于)
                        format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',  # 写入日志的内容
                        filename='/tmp/test.log')  # 文件保存路径
      
    logging.debug('debug message')  
    logging.info('info message')  
    logging.warning('warning message')  
    logging.error('error message')  
    logging.critical('critical message')

    配置参数:

    logging.basicConfig()函数中可通过具体参数来更改logging模块默认行为,可用参数有:
    
    filename:用指定的文件名创建FiledHandler,这样日志会被存储在指定的文件中。
    filemode:文件打开方式,在指定了filename时使用这个参数,默认值为“a”还可指定为“w”。
    format:指定handler使用的日志显示格式。
    datefmt:指定日期时间格式。
    level:设置rootlogger(后边会讲解具体概念)的日志级别
    stream:用指定的stream创建StreamHandler。可以指定输出到sys.stderr,sys.stdout或者文件(f=open(‘test.log’,’w’)),默认为sys.stderr。若同时列出了filename和stream两个参数,则stream参数会被忽略。

    format参数中可能用到的格式化字符串:

    %(name)s Logger的名字
    %(levelno)s 数字形式的日志级别
    %(levelname)s 文本形式的日志级别
    %(pathname)s 调用日志输出函数的模块的完整路径名,可能没有
    %(filename)s 调用日志输出函数的模块的文件名
    %(module)s 调用日志输出函数的模块名
    %(funcName)s 调用日志输出函数的函数名
    %(lineno)d 调用日志输出函数的语句所在的代码行
    %(created)f 当前时间,用UNIX标准的表示时间的浮 点数表示
    %(relativeCreated)d 输出日志信息时的,自Logger创建以 来的毫秒数
    %(asctime)s 字符串形式的当前时间。默认格式是 “2003-07-08 16:49:45,896”。逗号后面的是毫秒
    %(thread)d 线程ID。可能没有
    %(threadName)s 线程名。可能没有
    %(process)d 进程ID。可能没有
    %(message)s用户输出的消息

    3,logger对象配置方式

    前面的basicconfig配置看起来比较简单,当然他也有一些缺点,在日志写入文件中的时候会出现中文乱码的问题,不能在文件跟屏幕同时输出,当然这里的对象配置就能很好的解决这些问题了。

    import logging
    
    logger = logging.getLogger()  # 实例化一个logger对象
    fh = logging.FileHandler('111.txt',encoding='utf-8')  # FileHandler为文件,StreamHandler为屏幕上,第一个参数为文件名,后面指定编码方式
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')  # 日志写入的格式化字符串内容
    logger.setLevel(logging.DEBUG)  # 设置对象的最低等级,默认为WARNING
    fh.setLevel(logging.DEBUG)  # 设置绑定的fh的最低等级,默认为WARNING
    fh.setFormatter(formatter)  # 把格式化写入fh文件句柄中
    logger.addHandler(fh)  # 把fh文件句柄加入logger对象中
    
    logger.debug('logger debug message')
    logger.info('logger info message')
    logger.warning('logger warning message')
    logger.error('logger error message')
    logger.critical('logger critical message')
  • 相关阅读:
    How to change hostname on SLE
    How to install starDIct on suse OS?
    python logging usage
    How to reset password for unknow root
    How to use wget ?
    How to only capute sub-matched character by grep
    How to inspect who is caller of func and who is the class of instance
    How to use groovy script on jenkins
    Vim ide for shell development
    linux高性能服务器编程 (二) --IP协议详解
  • 原文地址:https://www.cnblogs.com/zzqit/p/10222040.html
Copyright © 2020-2023  润新知