我目前正试图摆脱 print() 并开始使用 ELK 堆栈和 structlog 模块集中日志收集来生成结构化的 json 日志行。这对于我使用 loggingHelper 模块自己编写的模块来说非常好,我可以导入和使用
logger = Logger()
在其他模块和脚本中。这是 loggingHelper 模块类:
class Logger:
"""
Wrapper Class to import within other modules and scripts
All the config and log binding (script
"""
def __init__(self):
self.__log = None
logging.basicConfig(level=logging.DEBUG, format='%(message)s')
structlog.configure(logger_factory=LoggerFactory(),
processors=[structlog.stdlib.add_log_level,
structlog.processors.TimeStamper(fmt="iso"),
structlog.processors.JSONRenderer()])
logger = structlog.get_logger()
main_script = os.path.basename(sys.argv[0]) if sys.argv[0] else None
frame = inspect.stack()[1]
log_invocation = os.path.basename(frame[0].f_code.co_filename)
user = getpass.getuser()
"""
Who executed the __main__, what was the executed __main__ file,
where did the log event happen?
"""
self.__log = logger.bind(executedScript = main_script,
logBirth = log_invocation,
executingUser = user)
def info(self, msg, **kwargs):
self.__log.info(msg, **kwargs)
def debug(self, msg, **kwargs):
self.__log.debug(msg, **kwargs)
def error(self, msg, **kwargs):
self.__log.error(msg, **kwargs)
def warn(self, msg, **kwargs):
self.__log.warning(msg, **kwargs)
这会产生格式良好的输出(每行一个 JSON),filebeat 能够读取并转发到 Elasticsearch。但是,第三方库完全粉碎了格式良好的日志。
{"executingUser": "xyz", "logBirth": "efood.py", "executedScript": "logAlot.py", "context": "SELECT displayname FROM point_of_sale WHERE name = '123'", "level": "debug", "timestamp": "2019-03-15T12:52:42.792398Z", "message": "querying local"}
{"executingUser": "xyz", "logBirth": "efood.py", "executedScript": "logAlot.py", "level": "debug", "timestamp": "2019-03-15T12:52:42.807922Z", "message": "query successful: got 0 rows"}
building service object
auth version used is: v4
Traceback (most recent call last):
File "logAlot.py", line 26, in <module>
ef.EfoodDataControllerMerchantCenter().get_displayname(123)
File "/home/xyz/src/toolkit/commons/connectors/efood.py", line 1126, in get_displayname
return efc.select_from_local(q)['displayname'].values[0]
IndexError: index 0 is out of bounds for axis 0 with size 0
正如您所看到的,来自第三方 Librara(googleapiclient)的信息级别和错误级别消息都是在不经过日志处理器的情况下打印的。
使用我编写的 loggingHelper 模块捕获和格式化在执行一个脚本中发生的所有事情的最佳方式(也是最 Pythonic)是什么?这甚至是最佳实践吗?
编辑:目前记录器确实写入标准输出本身,然后使用>>和2>&1将其重定向到crontab中的文件。如果我想将通过第三方库日志记录写入 stdout/stderr 的所有内容重定向到我,这对我来说似乎是一种不好的做法,因为这会导致循环,对吗?因此,我的目标不是重定向,而是捕获日志处理器中的所有内容。相应地更改了标题。