• Python logging 模块


    logging 的日志级别可以分为 debug(), info(), warning(), error(), critical() 5个级别。logging 默认只会打印 warning 级别的日志

    import logging
    
    logging.debug('debug message')
    logging.info('info message')
    logging.warning('warning message')
    logging.error('error message')
    logging.critical('critical message')
    
    运行结果:
    WARNING:root:warning message
    ERROR:root:error message
    CRITICAL:root:critical message
    

    灵活配置日志级别,日志格式和输出文件位置

    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='my.log')
    
    logging.debug('debug message')
    logging.info('info message')
    logging.warning('warning message')
    logging.error('error message')
    logging.critical('critical message')
    
    运行结果:
    在当前目录下生成一个文件 my.log
    文件内容为:
    2018-04-19 10:27:49 loggin_model.py[line:18] DEBUG debug message
    2018-04-19 10:27:49 loggin_model.py[line:19] INFO info message
    2018-04-19 10:27:49 loggin_model.py[line:20] WARNING warning message
    2018-04-19 10:27:49 loggin_model.py[line:21] ERROR error message
    2018-04-19 10:27:49 loggin_model.py[line:22] CRITICAL critical message
    

    在logging.basicConfig()函数中可通过具体参数来更改logging模块默认行为,可用参数有
    filename:用指定的文件名创建FiledHandler(后边会具体讲解handler的概念),这样日志会被存储在指定的文件中。
    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用户输出的消息

    日志信息同时输出到控制台和文件

    import logging
    
    logger = logging.getLogger()
    # logger.setLevel(logging.DEBUG)         # 可以通过 setLevel 来设置日志级别
    
    fn = logging.FileHandler('my.log')       # 创建一个handler,用于写入日志文件
    
    ch = logging.StreamHandler()             # 创建一个handler,用于输出到控制台
    
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    
    fn.setFormatter(formatter)
    ch.setFormatter(formatter)
    
    logger.addHandler(fn)                    # logger 对象添加多个fh和ch对象
    logger.addHandler(ch)
    
    logging.debug('debug message')
    logging.info('info message')
    logging.warning('warning message')
    logging.error('error message')
    logging.critical('critical message')
    
    运行结果:
    控制台和 my.log 文件内都存在下面文本
    2018-04-19 10:56:33,976 - root - WARNING - warning message
    2018-04-19 10:56:33,976 - root - ERROR - error message
    2018-04-19 10:56:33,977 - root - CRITICAL - critical message
    
  • 相关阅读:
    centos下卸载rpm包
    centos下添加环境变量和启动apache
    centos(linux) 下如何查看端口占用情况及杀死进程
    如何使上层的div遮住的链接可以点击
    jquery.blockUI.2.31.js 弹出层项目介绍
    fieldset、legend、display html元素
    <a>标签中href="javascript:;"的意思
    memcache 学习笔记
    sublime text3 JS语法检测插件
    Apache Rewrite的主要功能
  • 原文地址:https://www.cnblogs.com/klvchen/p/8880494.html
Copyright © 2020-2023  润新知