Python日志模块logging简介

 更新时间:2015年04月13日 10:12:33   投稿:junjie  
这篇文章主要介绍了Python日志模块logging简介,本文讲解了Logger、Handler、Formatter、日志配置管理、通过文件配置管理日志等内容,需要的朋友可以参考下

logging分为4个模块: loggers, handlers, filters, and formatters.

●loggers: 提供应用程序调用的接口
●handlers: 把日志发送到指定的位置
●filters: 过滤日志信息
●formatters: 格式化输出日志

Logger

Logger.setLevel() 设置日志级别
Logger.addHandler()和Logger.removeHandler() 增加和删除日志处理器
Logger.addFilter()和Logger.removeFilter() 增加和删除过滤器
Logger.debug(), Logger.info(), Logger.warning(), Logger.error(), and Logger.critical() 创建不同的级别的日志
getLogger() 获取日志的根实例

Handler

setLevel() 设置日志级别
setFormatter() 设置输出格式
addFilter() and removeFilter() 增加和删除过滤器

Formatter

默认形式为: %Y-%m-%d %H:%M:%S.
格式为: %()s

日志配置管理

硬编码形式

复制代码 代码如下:

import logging

# create logger
logger = logging.getLogger('simple_example')
logger.setLevel(logging.DEBUG)

# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)

# create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

# add formatter to ch
ch.setFormatter(formatter)

# add ch to logger
logger.addHandler(ch)

# 'application' code
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')


输出
复制代码 代码如下:

$ python simple_logging_module.py
2005-03-19 15:10:26,618 - simple_example - DEBUG - debug message
2005-03-19 15:10:26,620 - simple_example - INFO - info message
2005-03-19 15:10:26,695 - simple_example - WARNING - warn message
2005-03-19 15:10:26,697 - simple_example - ERROR - error message
2005-03-19 15:10:26,773 - simple_example - CRITICAL - critical message

通过文件配置管理日志

代码:

复制代码 代码如下:

import logging
import logging.config

logging.config.fileConfig('logging.conf')

# create logger
logger = logging.getLogger('simpleExample')

# 'application' code
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')


配置文件:
复制代码 代码如下:

[loggers]
keys=root,simpleExample

[handlers]
keys=consoleHandler

[formatters]
keys=simpleFormatter

[logger_root]
level=DEBUG
handlers=consoleHandler

[logger_simpleExample]
level=DEBUG
handlers=consoleHandler
qualname=simpleExample
propagate=0

[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,)

[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt=


输出:
复制代码 代码如下:

$ python simple_logging_config.py
2005-03-19 15:38:55,977 - simpleExample - DEBUG - debug message
2005-03-19 15:38:55,979 - simpleExample - INFO - info message
2005-03-19 15:38:56,054 - simpleExample - WARNING - warn message
2005-03-19 15:38:56,055 - simpleExample - ERROR - error message
2005-03-19 15:38:56,130 - simpleExample - CRITICAL - critical message

日志格式

%(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: 打印日志信息

流程图

相关文章

  • Python+MediaPipe实现检测人脸功能详解

    Python+MediaPipe实现检测人脸功能详解

    MediaPipe是用于构建多模态(例如视频、音频或任何时间序列数据)、跨平台(即eAndroid、IOS、web、边缘设备)应用ML管道的框架。本文将利用MediaPipe实现检测人脸功能,需要的可以参考一下
    2022-02-02
  • 基于Python制作一个微信聊天机器人

    基于Python制作一个微信聊天机器人

    这篇文章主要为大家详细介绍了如何基于Python制作一个微信聊天机器人,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以参考下
    2024-01-01
  • Python抖音快手代码舞(字符舞)的实现方法

    Python抖音快手代码舞(字符舞)的实现方法

    这篇文章主要给大家介绍了关于Python抖音快手代码舞的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02
  • Python 获取异常(Exception)信息的几种方法

    Python 获取异常(Exception)信息的几种方法

    这篇文章主要介绍了Python 获取异常(Exception)信息的几种方法,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下
    2020-12-12
  • python爬虫模块URL管理器模块用法解析

    python爬虫模块URL管理器模块用法解析

    这篇文章主要介绍了python爬虫模块URL管理器模块用法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-02-02
  • python接口测试对修改密码接口进行压测

    python接口测试对修改密码接口进行压测

    这篇文章主要为大家介绍了python接口测试对修改密码接口进行压测的脚本实现,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • 浅谈Python爬虫基本套路

    浅谈Python爬虫基本套路

    这篇文章主要介绍了Python爬虫基本套路,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-03-03
  • Python(Tornado)模拟登录小米抢手机

    Python(Tornado)模拟登录小米抢手机

    用Python(Tornado)模拟登录小米帐号,抢小米手机
    2013-11-11
  • Sanic框架Cookies操作示例

    Sanic框架Cookies操作示例

    这篇文章主要介绍了Sanic框架Cookies操作,结合实例形式分析了Sanic框架cookie读取、写入及删除等简单操作技巧,需要的朋友可以参考下
    2018-07-07
  • python可视化实现代码

    python可视化实现代码

    今天小编就为大家分享一篇关于python可视化实现代码,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-01-01

最新评论