配置第三方脚本的日志记录 [英] Configuring the logging of a third party script

查看:113
本文介绍了配置第三方脚本的日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个第三方python控制台脚本,我不想修改该源.

I have a third party python console script, which source I don't want to modify.

但是我想配置由脚本及其库完成的日志记录.该脚本使用标准的python日志记录,但不支持对其进行配置.

But I want to configure the logging which is done by the script and its libraries. The script uses the standard python logging, but does not support configuration of it.

脚本使用以下模式:

import logging
logger=logging.getLogger(__name__)

用例:

  • 我希望忽略文件foo.py的INFO消息.
  • 我想在日志消息中包含PID.

如果我不想修改控制台脚本的源代码,该如何配置日志记录?

How can I configure the logging, if I don't want to modify the sources of the console script?

通过cron调用脚本.

如果使用此脚本,如何配置日志记录?

How can I configure the logging if this script?

此答案中创建包装器脚本并非解决方案我.

Linux进程层次结构如下:

The linux process hierarchy looks like this:

Cron -> third_party_script

cron和third_party_script之间应该有任何胶水",包装"或脏物入侵"脚本.

There should be any "glue", "wrapping" or "dirty-hack" script between cron and third_party_script.

我想练习关注点分离".我希望能够一次在一个地方配置日志记录. virtualenv的所有python代码都应使用此配置.编写包装器将是一种解决方法.我想要一个解决方案.

I want to practice "separation of concerns". I want to be able to configure logging one time and in one place. This configuration should get used by all python code of a virtualenv. Writing a wrapper would be a work-around. I want a solution.

几个月后,我认为一个pth文件将是一个简单的答案.

Several months later I think a pth file would an simple answer.

推荐答案

几个月前,我问了这个问题.不幸的是,我没有任何让我满意的答案.

I asked this question several months ago. Unfortunately I got no answer which satisfied me.

使用日志记录和对其进行设置之间的区别对我来说很重要.

The distinction between using logging and setting it up is important for me.

这是我的解决方案:在我们的上下文中,我们设置了在usercustomize.py中被调用的方法中的日志记录.

This is my solution: In our context we set up logging in a method which gets called in usercustomize.py.

通过这种方式,可选插件可以使用日志记录而无需进行设置.

This way the optional plugins can use the logging without the need to set it up.

这几乎解决了我的所有需求.

This almost solved all my needs.

到目前为止,我发现没有比usercustomize.py更好的方法了.我的理想解决方案是将我称为virtualenvcustomize.py:如果解释程序加载virtualenv,则一些初始化代码将运行.到目前为止,我找不到这样的钩子.如果您有解决方案,请告诉我.

Up to now I found no better way than usercustomize.py. My perfect solution would be something I would call virtualenvcustomize.py: Some initialization code which gets run if the interpreter loads virtualenv. Up to now I could not find such a hook. Please let me know if you have a solution.

这篇关于配置第三方脚本的日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆