python - 如何动态记录不同级别的消息?例如:logging.x("message") where x in ['warn',error',..]
问题描述
我想要一个函数def log(message, level): logging.level("message")
,而不是写logging.error("message")
,我只想调用这个函数 log("message",error)。这是代码。
import logging
def logging_exception(message, level):
raise Exception(message)
logging.level(message)
logging_exception("this is an error",error)
预期结果:error message : this is an error
解决方案
如果您乐于使用整数日志级别定义而不是文本级别定义(https://docs.python.org/3/library/logging.html#logging-levels),那么您可以使用 log 方法而不是个人水平方法
import logging
logging.log(50,"This is a critical log message")
输出:
CRITICAL:root:This is a critical log message
推荐阅读
- excel - 当列和行与平面文件不匹配时,如何解决(使用)ETL
- node.js - Node、Express 和在端点中解析流式 JSON 而不阻塞线程
- jasmine - Proteactor- jasmine - 如何在另一个 js 中执行一个 js 文件
- ios - 将图像添加到按钮时,如何禁用 UIButton 的标题向右移动?
- python - Python/Arduino 串行通信
- typescript - 如何使用泛型类型改进函数 TypeScript?
- opencv - 如何计算两个图像之间的百分比相似度?
- python - 在Python中按日期过滤excel中的数据
- python - 如何在时间点计算 Pandas 中的累积 groupby 计数?
- ansible - 如何通过jinja2模板更新ansible角色的变量