文章目录
前言
学习记录,有错误请指正,谢谢
一、logging是什么?
logging 模块是Python标准库中提供的用于日志记录的模块,它允许你在应用程序中记录各种信息,例如警告、错误、调试信息等,以便在运行时进行问题诊断、跟踪应用程序状态以及记录重要事件。
二、使用
1.一个简单案例
代码如下(示例):
import logging # 导入logging
logging.basicConfig() # 基本的日志配置。这里没有传递任何参数,
#日志级别被设置为 WARNING,并且没有指定日志格式。
logging.debug('This is a debug message')#记录了一个 DEBUG 级别的日志消息
logging.info('This is an info message')#记录了一个 info 级别的日志消息
logging.warning('This is a warning message')#记录了一个 warning 级别的日志消息
logging.error('This is an error message')#记录了一个 error 级别的日志消息
logging.critical('This is a critical message')#记录了一个 critical 级别的日志消息
输出结果如下:
WARNING:root:This is a warning message
ERROR:root:This is an error message
CRITICAL:root:This is a critical message
只输出WARNING,ERROR和CRITICAL是因为默认日志级别被设置为 WARNING,只会输出比WARNING更高级别的消息。
2.另一个示例
代码如下(示例):
import logging
# 配置日志格式
logging.basicConfig(
level=logging.DEBUG, # 设置日志级别为DEBUG,可以根据需要调整
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
datefmt='%a, %d %b %Y %H:%M:%S',
handlers=[
logging.FileHandler("app.log"), # 将日志保存到文件
logging.StreamHandler() # 输出日志到终端
]
)
# 输出不同级别的日志
logging.debug("This is a debug message")
logging.info("This is an info message")
logging.warning("This is a warning message")
logging.error("This is an error message")
logging.critical("This is a critical message")
输出结果如下:
Thu, 03 Aug 2023 20:44:46 - root - DEBUG - This is a debug message
Thu, 03 Aug 2023 20:44:46 - root - INFO - This is an info message
Thu, 03 Aug 2023 20:44:46 - root - WARNING - This is a warning message
Thu, 03 Aug 2023 20:44:46 - root - ERROR - This is an error message
Thu, 03 Aug 2023 20:44:46 - root - CRITICAL - This is a critical message
level=logging.DEBUG
- 设置日志级别为DEBUG级别。
format=“%(asctime)s - %(name)s - %(levelname)s - %(message)s”,
- 定义了日志消息的格式。
datefmt=‘%a, %d %b %Y %H:%M:%S’,
- 定义了时间输出格式。
handlers
- 是一个处理程序列表,其中包含两个处理程序:
- FileHandler用于将日志保存到文件(可以指定路径,相对位置会根据程序入口函数指定)。如
logging.basicConfig( level=logging.DEBUG, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", handlers=[ logging.FileHandler("/path/to/your/log/directory/app.log"), # 设置日志文件路径 logging.StreamHandler() ] )
- StreamHandler用于将日志输出到终端。
3.logging和logger的区别,为什么要用logger而不是一直用logging?
Python的logging模块中,logger和logging之间的关系是这样的:logging是模块本身,而logger是模块中的一个类的实例。
通常推荐使用logger的实例来进行日志记录的原因有以下几点:
- 更好的组织和分层: 使用logger的实例可以更好地组织和分层化日志记录。每个模块或组件都可以拥有自己的logger实例,从而更好地区分不同模块的日志输出。这有助于在大型应用程序中跟踪日志,以及进行故障排查和调试。
- 更灵活的配置: 使用logger实例可以让你在不同的地方配置不同的日志处理程序、级别和格式。这样,你可以根据需要进行更灵活的日志配置,而不影响其他部分的日志记录。
- 更好的代码质量: 使用logger实例可以提高代码的可维护性和可读性。它允许你在整个应用程序中保持一致的日志记录风格,并且让代码更具可读性,因为你可以直观地了解哪个模块正在记录哪些日志。
- 更好的性能: 使用logger实例可以避免在多个地方重复配置logging,从而提高性能,因为日志记录器的配置通常只需要执行一次。
创建Logger实例。
import logging
# 配置日志格式
logging.basicConfig(
level=logging.DEBUG, # 设置日志级别为DEBUG,可以根据需要调整
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
handlers=[
logging.FileHandler("app.log"), # 将日志保存到文件
logging.StreamHandler() # 输出日志到终端
]
)
# 创建一个Logger实例
logger = logging.getLogger(__name__)
# 输出不同级别的日志
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")
跨模块使用
项目目录如下
my_app/
├── main.py
└── other_module.py
- main中配置
# main.py import logging def setup_logging(): # 配置日志格式 logging.basicConfig( level=logging.DEBUG, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", handlers=[ logging.FileHandler("/path/to/your/log/directory/app.log"), # 设置日志文件路径 logging.StreamHandler() ] ) # 在这里添加其他处理程序、过滤器等 # 在程序入口调用配置函数 if __name__ == "__main__": setup_logging() # 导入其他模块并开始运行应用程序逻辑 import other_module other_module.do_something()
- other_module.py
# other_module.py import logging # 在其他模块中,直接导入Logger实例 logger = logging.getLogger(__name__) def do_something(): logger.debug("This is a debug message from other_module")
参考
https://blog.csdn.net/claroja/article/details/102601920
https://zhuanlan.zhihu.com/p/445411809
https://www.runoob.com/python/att-time-strptime.html