Python logging模块

日志作用:

调试

辅助定位问题

数据分析

日志的级别

 

 

五种日志级别按从低到高排序:

  DEBUG < INFO < WARNING < ERROR < CRITICAL

日志的用法:

import logging
logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s -%(message)s')
logging.debug('Some debugging details.')
logging.info('The logging module is working')
logging.warning('An error message is about to be logged.')
logging.error('An error has occurred.')
logging.critical('The program is unable to recover!')

 

官网信息:

https://docs.python.org/3/howto/logging.html 

basicConfig需要在最前面定义好

import logging
#默认设置warning级别,即warning级别以上log才会打印
logging.warning('Watch out!')  # will print a message to the console
logging.info('I told you so')  # will not print anything

设置默认级别

logging.basicConfig(level=logging.INFO)

设置log保存在文件里

import logging

logging.basicConfig(filename='example.log', encoding='utf-8', level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')
logging.error('And non-ASCII stuff, too, like Øresund and Malmö')

显示日期和时间

import logging
logging.basicConfig(format='%(asctime)s %(message)s')
logging.warning('is when this event was logged.')

更改日期时间显示格式

logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
logging.warning('is when this event was logged.')

 

posted @   lms21  阅读(25)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示