美文网首页
日志logging

日志logging

作者: 陈水寒 | 来源:发表于2018-01-29 09:40 被阅读5次
import logging
import sys

# 获取logger实例,如果参数为空则返回root logger
logger = logging.getLogger("AppName")

# 指定logger输出格式
formatter = logging.Formatter('%(asctime)s %(levelname)-8s: %(message)s')

# 文件日志
file_handler = logging.FileHandler("test.log")
file_handler.setFormatter(formatter)  # 可以通过setFormatter指定输出格式

# 控制台日志
console_handler = logging.StreamHandler(sys.stdout)
console_handler.formatter = formatter  # 也可以直接给formatter赋值

# 为logger添加的日志处理器
logger.addHandler(file_handler)
logger.addHandler(console_handler)

# 指定日志的最低输出级别,默认为WARN级别
logger.setLevel(logging.INFO)

# 输出不同级别的log
logger.debug('this is debug info')
logger.info('this is information')
logger.warn('this is warning message')
logger.error('this is error message')
logger.fatal('this is fatal message, it is same as logger.critical')
logger.critical('this is critical message')

# 2016-10-08 21:59:19,493 INFO    : this is information
# 2016-10-08 21:59:19,493 WARNING : this is warning message
# 2016-10-08 21:59:19,493 ERROR   : this is error message
# 2016-10-08 21:59:19,493 CRITICAL: this is fatal message, it is same as logger.critical
# 2016-10-08 21:59:19,493 CRITICAL: this is critical message

# 移除一些日志处理器
logger.removeHandler(file_handler)

相关文章

  • logging

    Menu logging定义 logging - basicConfig logging定义 日志模块 loggi...

  • Python39_日志处理

    logging模块 日志基础 日志级别(从低到高): logging.NOTSET:不设置 loging.debu...

  • 日志框架

    日志门面 Apache Commons Logging (之前叫 Jakarta Commons Logging,...

  • appium自动化测试日志收集-logging

    关于日志级别: logging对象和filehandler都可以设置日志级别,logging设置的日志级别是控制台...

  • logging模块

    (一)什么是logging模块? logging是Python内置的日志模块,用于生成程序的日志 (二...

  • springboot通过java -jar启动

    命令参数 00x0 日志配置: 默认日志:使用logging.path、logging.file 参数来指定日志输...

  • python logging模块应用详解

    python的日志模块,logging。 首先介绍日志的级别:日志的严重程度 logging中包含了四个主要的类:...

  • Spring Boot - 配置文件

    1.logging:日志 org.springframework.boot.logging.logback包下提供...

  • Python Logging 指南

    Python Logging 指南 文章翻译自官方文档:Logging HOWTO 基础日志教程 日志记录是一种跟...

  • python常用模块练习题

    练习题 1.logging模块有几个日志级别? logging的日志可以分为debug(), info(), wa...

网友评论

      本文标题:日志logging

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