在 Python 中是否可以使用C++样式的日志记录宏?
Are C++ style logging macros possible in Python?
是否可以在Python中实现以下(伪)代码的等效代码?
#define DEBUG(topic, msg) LOG_IMPL(Logger.DEBUG, topic, msg)
#define INFO(topic, msg) LOG_IMPL(Logger.INFO, topic, msg)
#define LOG_IMPL(level, topic, msg) if(Logger.level() <= level) { Logger.log(level, topic, msg); }
DEBUG("MyComponent", "What you logging at?")
这样做的好处是你不必计算字符串日志消息,例如连接字符串、调用 .format() 等。
更新:
懒惰的记录器消息字符串评估 - 这回答了我的问题,所以我将投票关闭这篇文章。
Python附带电池,logging module
是stdlib的一部分:
from logging import getLogger
log = getLogger('my.module')
log.debug('Debug level messages')
log.warning('Warning!')
log.info('Informative message')
log.error('Error messages')
log.exception('Use this in an exception handler, the exception will be included automatically')
上面一组方法是log.log(level, msg)
方法的快捷方式,该方法采用任意(整数)级别,logging
模块定义DEBUG
、WARNING
和其他级别。
这些方法支持 python 字符串格式模板的延迟计算;仅当消息的日志级别实际超过正在记录的日志记录级别时,才会插入额外的参数:
log.warning('Warning message: the %s is missing %i frobnars', systemname, count)
仅当日志消息实际到达处理程序时,才会以等效的 'Warning message: the %s is missing %i frobnars' % (systemname, count)
记录上述消息。
如何对消息使用 lambda:
log( lambda : (string1 + string2 + "%d %d" % (val1, val2)) )
并且让日志函数仅在启用日志记录时调用传入的函数。
你试过logging
模块吗?例:
import logging
logging.basicConfig(filename='example.log',level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')
来源: http://docs.python.org/howto/logging.html#logging-basic-tutorial
我想出了一个解决方案,允许对日志消息进行延迟评估,同时仍然允许我将自定义格式化程序和处理程序封装在一个小型日志记录代理类中。
除非写入日志消息(日志记录处理此消息),否则不会评估格式字符串;这是通过单独传递格式字符串和参数来实现的。
@classmethod
def info(cls, component, msg, *args):
"""Log an info message"""
cls.__log(cls.Level.INFO, component, msg, (args)
@classmethod
def __log(cls, level, component, msg, *args):
"""Log a message at the requested level"""
logging.getLogger("local").log(level, " - ".join([component, msg.format(*args)]))
Logger.info("MyComponent", "My message with arg '{0}'", "TestArg")
相关文章:
- 如何实现具有多个平台__FILE__和__LINE__信息的 C/C++ 可变参数日志记录宏?
- C++ 中混合二进制/文本日志记录的最佳做法
- 轻松日志记录++如何避免多个初始化
- 使用字符串流加速 std::cout 日志记录
- 我的游戏引擎的 spdlog 日志记录出现奇怪的"unresolved external symbol"错误
- 使用 #define 进行跟踪日志记录以避免性能问题
- 如何在代码中启用/禁用 spdlog 日志记录?
- 我正在尝试用 c++ 制作一个日志记录框架,但信息没有传递给记录器的子组件,我做错了什么?
- 使用 Python、ROS 和 C++ 进行日志记录
- C++日志记录类实例标识符
- 如何在C++中重载<<线程安全日志记录的运算符?
- 在 C++17 中将无限参数传递给日志记录函数
- 配置 log4cpp 日志记录级别优先级
- 如何使用 Boost V2 1.70 日志记录库在第一个索引(例如 logtrail01.txt)保留最新的日志文件名?
- 如何将日志记录写入Visual Studio输出窗口?
- 使用现有日志记录库 (C++) 限制日志大小
- "正在初始化":无法从"事件日志记录 *"转换为"事件日志记录 []"
- 提升日志记录text_file_backend没有wchar_t版本?
- 多线程应用程序中的独立日志记录
- C++类似cout的函数,将数据写入文件(日志记录)