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

建站哪家公司比较好而且不贵医药o2o平台有哪些

建站哪家公司比较好而且不贵,医药o2o平台有哪些,制作相册的模板,linux目录不可写 wordpress文章目录 相关链接第三篇#xff1a;日志记录方法与多模块日志1 基本日志记录方法2 在多个模块中使用日志3 文章总结 相关链接 【Python日志功能】一.日志基础与基本配置【Python日志功能】二.高级配置与日志处理器【Python日志功能】三.日志记录方法与多模块日志官方文档日志记录方法与多模块日志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 messagelogging 模块还提供了 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 moduleresult 10 / 0 ZeroDivisionError: division by zero2 在多个模块中使用日志 在实际项目中代码通常被组织在不同的模块或包中。为了在整个项目中统一日志管理我们可以创建一个基础的日志配置并在各个模块中使用该配置。 项目结构 Course03/ ├── main.py ├── module_a.py └── module_b.pymain.py: import logging from module_a import function_a from module_b import function_b# 配置基础日志 logging.basicConfig(levellogging.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 range3 文章总结 在本篇文章中我们探讨了Python logging 模块的日志记录方法以及如何实现统一的日志管理通过设置全局的日志配置保证所有模块的日志记录能够统一输出到同一位置方便项目的集中管理和调试。这些内容为大型项目的日志系统提供了全面的解决方案。
http://www.dnsts.com.cn/news/69031.html

相关文章:

  • 邯郸做网站外包商丘seo优化
  • 创可贴网站怎么做图片大全wordpress内容页标题
  • 什么叫网站建设服务西安汇友网站建设
  • wordpress网站跳转北京seo包年
  • 简易个人网站好的建站网站
  • 单位网站建设服务wordpress 分页目录
  • wordpress建站企业站网页搜索图片
  • 网站开发电话话术南城网站建设公司案例
  • 大型免费网站制作国内的跨境电商平台有哪些
  • 装饰公司 网站模板滨海哪家专业做网站
  • 学做网站的学校设计师交流平台有哪些
  • 网站优化排名软件网站上海市基础工程公司
  • 深圳人才网站建设可以让外国人做问卷调查的网站
  • 国外大型门户网站网站开发对企业有什么用
  • 加强网站建设的制度seo推广招聘
  • 图片网站收录伊利网站建设水平评价
  • php中英文网站源码3d动画制作软件手机版
  • 站长工具网站提交近期舆情热点话题
  • p2p网站建设方案策划书wordpress菜单导航插件
  • 那些网站被k恢复是怎么做的网站建设什么是开发实施实施
  • dede网站 设置404 错误页面linux 一键 WordPress
  • 企业网站模板2016成套房产网站制作流程
  • 凡客建站快车seo经理招聘
  • 网站开发的工作经验要求wordpress英文美食主题
  • 沈阳建站网页模板郑州的兼职网站建设
  • 阿里云主机网站开发河北省沧州建设厅网站
  • 做零售出口的网站网站建设团队哪个最好
  • 网站建设方案-奢侈品广告优化是做什么的
  • 怎样做付费下载的网站网站建设分金手指排名五
  • 搭配服装网站源码建设网站选题应遵循的规则