当前位置: 首页 > news >正文

长沙做个网站多少钱wordpress取消邮箱验证

长沙做个网站多少钱,wordpress取消邮箱验证,网站全新改版如何做,东莞做网站公司有哪些文章目录 相关链接第三篇:日志记录方法与多模块日志1 基本日志记录方法2 在多个模块中使用日志3 文章总结 相关链接 【Python日志功能】一.日志基础与基本配置【Python日志功能】二.高级配置与日志处理器【Python日志功能】三.日志记录方法与多模块日志官方文档&am…

文章目录

  • 相关链接
  • 第三篇:日志记录方法与多模块日志
  • 1 基本日志记录方法
  • 2 在多个模块中使用日志
  • 3 文章总结

相关链接

  • 【Python日志功能】一.日志基础与基本配置
  • 【Python日志功能】二.高级配置与日志处理器
  • 【Python日志功能】三.日志记录方法与多模块日志
  • 官方文档:logging — Python 的日志记录工具 — Python 3.12.6 文档
  • 个人博客:issey的博客 - 愿无岁月可回首

第三篇:日志记录方法与多模块日志

在大型项目中,代码通常被划分为多个模块或包。为了有效地跟踪和调试程序的运行,我们需要在各个模块中统一使用日志系统。本文将介绍如何在多模块项目中使用Python的logging模块,涵盖基本的日志记录方法、异常信息的记录以及在多个模块中共享日志配置的方法。

1 基本日志记录方法

logging 模块提供了多种方法来记录不同级别的日志消息,这些方法对应着不同的严重程度级别:

  • debug:调试信息,用于诊断问题,级别最低。
  • info:常规信息,确认程序按预期运行。
  • warning:警告信息,表明潜在的问题。
  • error:错误信息,程序已出现问题。
  • critical:严重错误,程序可能无法继续运行。

代码示例:

import logging# 创建日志记录器
logger = logging.getLogger('basic_logger')
logger.setLevel(logging.DEBUG)# 创建控制台处理器
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)# 创建格式化器并添加到处理器
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
console_handler.setFormatter(formatter)# 将处理器添加到日志记录器
logger.addHandler(console_handler)# 记录不同级别的日志
logger.debug('This is a DEBUG message')
logger.info('This is an INFO message')
logger.warning('This is a WARNING message')
logger.error('This is an ERROR message')
logger.critical('This is a CRITICAL message')

控制台输出:

2024-09-17 20:46:41,803 - basic_logger - DEBUG - This is a DEBUG message
2024-09-17 20:46:41,803 - basic_logger - INFO - This is an INFO message
2024-09-17 20:46:41,803 - basic_logger - WARNING - This is a WARNING message
2024-09-17 20:46:41,803 - basic_logger - ERROR - This is an ERROR message
2024-09-17 20:46:41,803 - basic_logger - CRITICAL - This is a CRITICAL message

logging 模块还提供了 exception 方法,用于记录异常信息。exception 方法会自动记录异常堆栈信息,非常适合在 try-except 语句中使用。

log_exception.py:

import logging
import os# 获取脚本所在目录
script_dir = os.path.dirname(os.path.abspath(__file__))# 定义日志文件的路径
log_file_path = os.path.join(script_dir, 'log/exception.log')# 创建日志记录器
logger = logging.getLogger('exception_logger')
logger.setLevel(logging.DEBUG)# 创建控制台处理器和文件处理器
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)file_handler = logging.FileHandler(log_file_path)
file_handler.setLevel(logging.DEBUG)# 创建格式化器并添加到处理器
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
console_handler.setFormatter(formatter)
file_handler.setFormatter(formatter)# 将处理器添加到日志记录器
logger.addHandler(console_handler)
logger.addHandler(file_handler)# 模拟异常并记录
try:result = 10 / 0
except ZeroDivisionError:logger.exception('An exception occurred')

log文件输出:

2024-09-17 20:53:46,430 - exception_logger - ERROR - An exception occurred
Traceback (most recent call last):File "/home/issey/workplace/Docker_workplace/Logger_Study/Course03/log_exception.py", line 32, in <module>result = 10 / 0
ZeroDivisionError: division by zero

2 在多个模块中使用日志

在实际项目中,代码通常被组织在不同的模块或包中。为了在整个项目中统一日志管理,我们可以创建一个基础的日志配置,并在各个模块中使用该配置。

项目结构

Course03/
├── main.py
├── module_a.py
└── module_b.py

main.py:

import logging
from module_a import function_a
from module_b import function_b# 配置基础日志
logging.basicConfig(level=logging.DEBUG,format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',handlers=[logging.StreamHandler(),logging.FileHandler('log/project.log')])# 创建主日志记录器
logger = logging.getLogger('main')# 记录主文件日志
logger.info('This is an INFO message from main')# 调用模块函数
function_a()
function_b()

module_a.py:

import logging# 创建模块 A 的日志记录器
logger = logging.getLogger('module_a')def function_a():logger.debug('This is a DEBUG message from module A')logger.info('This is an INFO message from module A')logger.warning('This is a WARNING message from module A')try:result = 10 / 0except ZeroDivisionError:logger.exception('Exception occurred in module A')

module_b.py:

import logging# 创建模块 B 的日志记录器
logger = logging.getLogger('module_b')def function_b():logger.debug('This is a DEBUG message from module B')logger.info('This is an INFO message from module B')logger.warning('This is a WARNING message from module B')try:result = [1, 2, 3][5]except IndexError:logger.exception('Exception occurred in module B')

当运行 main.py 时,日志将统一输出到控制台和 project.log 文件中,日志内容包括来自主模块和子模块的日志消息。

log日志输出:

2024-09-17 21:12:26,607 - main - INFO - This is an INFO message from main
2024-09-17 21:12:26,608 - module_a - DEBUG - This is a DEBUG message from module A
2024-09-17 21:12:26,608 - module_a - INFO - This is an INFO message from module A
2024-09-17 21:12:26,608 - module_a - WARNING - This is a WARNING message from module A
2024-09-17 21:12:26,608 - module_a - ERROR - Exception occurred in module A
Traceback (most recent call last):File "/home/issey/workplace/Docker_workplace/Logger_Study/Course03/module_a.py", line 11, in function_aresult = 10 / 0
ZeroDivisionError: division by zero
2024-09-17 21:12:26,608 - module_b - DEBUG - This is a DEBUG message from module B
2024-09-17 21:12:26,608 - module_b - INFO - This is an INFO message from module B
2024-09-17 21:12:26,609 - module_b - WARNING - This is a WARNING message from module B
2024-09-17 21:12:26,609 - module_b - ERROR - Exception occurred in module B
Traceback (most recent call last):File "/home/issey/workplace/Docker_workplace/Logger_Study/Course03/module_b.py", line 11, in function_bresult = [1, 2, 3][5]
IndexError: list index out of range

3 文章总结

在本篇文章中,我们探讨了Python logging 模块的日志记录方法以及如何实现统一的日志管理,通过设置全局的日志配置,保证所有模块的日志记录能够统一输出到同一位置,方便项目的集中管理和调试。这些内容为大型项目的日志系统提供了全面的解决方案。

http://www.yayakq.cn/news/334002/

相关文章:

  • 网站模板上传教程个人简历模板下载 免费完整版
  • 典当行网站做网站每月收入
  • 闵行区做网站多语种网站
  • 企业综合管理系统seo关键词排名网络公司
  • 网站关键词是指什么上海seo排名
  • 会所网站建设优质网站建设服务
  • 电子商务网站建设的主页php网站开发答辩问的问题
  • 静态网站 动态推广网站排名优化seo教程
  • 网站报价功能清单深圳比较出名的互联网公司
  • 不懂代码怎么做网站百度指数平台官网
  • 个人网站建设素材做网上商城网站哪家好
  • 网站建设设置分享功能自媒体软文发布平台
  • 小学网站模板源码辽宁建设工程信息网为什么上不去啦
  • wordpress如何建立网站网站策划公司
  • 网站做兼容需要多少钱网站建设入门培训
  • 国外建筑公司网站网站出租目录做菠菜 有什么坏处
  • 潍坊网站建设哪家好网站seo查询工具
  • 青岛做网站公司用爬虫做数据整合网站
  • 网站开发证浙江建设银行网站
  • 怎么样做一家卖东西的网站网络营销策划的流程及要点
  • 安徽安搜做的网站怎么样产品宣传短视频制作
  • 做问卷不花钱的网站忻州建设公司网站
  • 上海商城网站建设公司转做海外买手的网站
  • 苏州网站建设报价wordpress弹窗
  • 网站建设检查整改情况报告抚州招聘网站建设
  • 做产品设计之前怎么查资料国外网站做网站预付款 怎么做账
  • 好的建站软件厦门园网站忱建设
  • 高密制作网站网络营销有什么用
  • 怎么做论坛的网站网站优化待遇
  • 阿里云虚拟主机做淘客网站大数据营销平台那么多