脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python logging模块进行封装实现原理解析

Python logging模块进行封装实现原理解析

2020-08-09 14:48不放弃自己 Python

这篇文章主要介绍了Python logging模块进行封装实现原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

1. 简介

      追踪某些软件运行时所发生事件的方法, 可以在代码中调用日志中某些方法来记录发生的事情

一个事件可以用一个可包含可选变量数据的消息来描述

事件有自己的重要性等级

2. 使用logging日志系统四大组件

  • loggers日志器
    • 提供应用程序代码直接使用的接口
  • handlers处理器
    • 用于将日志记录发送到指定的目的位置
  • filters过滤器
    • 过滤, 决定哪些输出哪些日志记录, 其余忽略
  • formatters格式器
    • 控制日志输出格式

使用代码如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import os, time, logging, sys
from Common.plugs.get_config import r_config
 
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
if sys.platform == "win32":
  ENV_CONF_DIR = os.path.join(BASE_DIR, 'Common/conf/env_config.ini').replace('/', '\\')
else:
  ENV_CONF_DIR = os.path.join(BASE_DIR, 'Common/conf/env_config.ini')
log_path = r_config(ENV_CONF_DIR, "log", "log_path")
 
 
class Log:
 
  def __init__(self, log_path):
    self.logName = os.path.join(log_path, '{0}.log'.format(time.strftime('%Y-%m-%d')))
 
  def console_log(self, level, message):
    # 创建一个logger
    logger = logging.getLogger()
    logger.setLevel(logging.DEBUG)
 
    # 创建一个handler,用于 debug 写入日志文件
    debug_file = logging.FileHandler(self.logName, 'a+', encoding='utf-8')
    debug_file.setLevel(logging.DEBUG)
 
    # 再创建一个handler,用于输出到控制台
    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
 
    # 定义handler的输出格式
 
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
 
    debug_file.setFormatter(formatter)
    ch.setFormatter(formatter)
 
    # 给logger添加handler
    logger.addHandler(debug_file)
    logger.addHandler(ch)
 
    # 记录一条日志
    if level == 'info':
      logger.info(message)
    elif level == 'debug':
      logger.debug(message)
    elif level == 'warning':
      logger.warning(message)
    elif level == 'error':
      logger.error(message)
 
    elif level == 'critical':
      logger.critical(message)
 
    logger.removeHandler(ch)
    logger.removeHandler(debug_file)
    debug_file.close()
 
  def debug(self, message): #最详细日志信息, 多用于问题诊断
    self.console_log('debug', message)
 
  def info(self, message): #仅次于DEBUG, 多用于记录关键点信息, 确保程序按预期执行
    self.console_log('info', message)
 
  def warning(self, message): #低等级故障, 但程序仍能运行, 如磁盘空间不足警告
    self.console_log('warning', message)
 
  def error(self, message): #由于比WARNING严重的问题, 导致某些功能不能正常运行时的记录
    self.console_log('error', message)
 
  def critical(self, message): 严重错误, 导致应用程序不能继续运行时的记录
    self.console_log('critical', message)
 
 
if __name__ == '__main__':
  Log(log_path).info("adasd")
  Log(log_path).error("dsadasddasd")
'''

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.cnblogs.com/hghua/p/13268007.html

延伸 · 阅读

精彩推荐