Python日志模块logging简介

作者:junjie 时间:2022-07-14 23:55:22 

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日志模块logging简介

标签:Python,日志模块,logging
0
投稿

猜你喜欢

  • python目标检测SSD算法训练部分源码详解

    2021-08-01 02:23:15
  • 在生成的静态页面中统计点击次数

    2009-11-19 13:20:00
  • 谨慎使用PHP的引用原因分析

    2023-09-09 10:37:13
  • 获取mssql的xml返回结构的方法

    2007-08-23 12:52:00
  • Python测试框架pytest介绍

    2023-08-03 01:47:59
  • 教你用python实现12306余票查询

    2021-03-16 01:39:19
  • Python爬虫基础初探selenium

    2023-07-26 17:40:50
  • 关于SQL Server数据库中转储设备分析

    2009-01-21 14:55:00
  • 用ASP木马实现FTP和解压缩

    2008-02-13 08:47:00
  • 如何用Anaconda搭建虚拟环境并创建Django项目

    2022-02-25 02:30:21
  • Python中unittest用法实例

    2023-09-02 13:13:45
  • asp如何在ADO中客户端利用好缓存技术?

    2010-06-17 12:50:00
  • java解析php函数json_encode unicode 编码问题

    2023-07-03 17:10:23
  • 在一个网站下再以虚拟目录的方式挂多个网站的方法

    2023-07-24 01:03:57
  • C#调用Python模块的方法

    2021-04-13 15:29:10
  • PHP基于cookie与session统计网站访问量并输出显示的方法

    2023-11-15 08:38:52
  • SQL Server技巧之快速得到表的记录总数

    2011-01-04 14:36:00
  • Python中shapefile转换geojson的示例

    2023-08-03 19:05:04
  • 手机验证设计感悟

    2011-01-20 19:55:00
  • 数据挖掘之Apriori算法详解和Python实现代码分享

    2022-02-07 00:29:59
  • asp之家 网络编程 m.aspxhome.com