首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >python-daemon和日志:以交互方式设置日志级别

python-daemon和日志:以交互方式设置日志级别
EN

Stack Overflow用户
提问于 2018-08-06 04:49:03
回答 1查看 746关注 0票数 1

我有一个python守护进程,它通过ThreadedTCPServer (灵感来自食谱示例:https://docs.python.org/2/howto/logging-cookbook.html#sending-and-receiving-logging-events-across-a-network )记录到一个文件中,因为我将有许多这样的进程写入到同一个文件中。我从ipython控制台使用subprocess.Popen控制守护进程的生成,这就是应用程序的运行方式。我能够成功地从主ipython进程和守护进程写入日志文件,但我无法通过简单地在ipython中设置根记录器的级别来更改这两者的级别。这是应该成为可能的事情吗?或者需要自定义功能来单独设置守护进程的logging.level?

编辑:根据要求,这里尝试提供我正在尝试实现的伪代码示例。我希望这是一个足够的描述。

daemon_script.py

代码语言:javascript
复制
import logging
import daemon 
from other_module import function_to_run_as_daemon

class daemon(object):
    def __init__(self):
         self.daemon_name = __name__
         logging.basicConfig()  # <--- required, or I don't get any log messages
         self.logger = logging.getLogger(self.daemon_name)
         self.logger.debug( "Created logger successfully" )

    def run(self):
        with daemon.daemonContext( files_preserve = [self.logger.handlers[0].stream] )
            self.logger.debug( "Daemonised successfully - about to enter function" )
            function_to_run_as_daemon()

if __name__ == "__main__":
    d = daemon()
    d.run()

然后在ipython中,我会运行下面这样的代码

代码语言:javascript
复制
>>> import logging
>>> rootlogger = logging.getLogger()
>>> rootlogger.info( "test" )
INFO:root:"test"
>>> subprocess.Popen( ["python" , "daemon_script.py"] )
DEBUG:__main__:"Created logger successfully"
DEBUG:__main__:"Daemonised successfully - about to enter function"
# now i'm finished debugging and testing, i want to reduce the level for all the loggers by changing the level of the handler
# Note that I also tried changing the level of the root handler, but saw no change
>>> rootlogger.handlers[0].setLevel(logging.INFO)
>>> rootlogger.info( "test" )
INFO:root:"test"
>>> print( rootlogger.debug("test") )
None
>>> subprocess.Popen( ["python" , "daemon_script.py"] )
DEBUG:__main__:"Created logger successfully"
DEBUG:__main__:"Daemonised successfully - about to enter function"

我认为我可能没有正确地处理这个问题,但是,我不清楚什么方法能更好地工作。任何建议都将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-06 08:11:37

在守护进程中创建的记录器与在ipython中创建的记录器不同。您可以通过打印两个记录器对象本身来测试这一点,这将显示它们的内存地址。

我认为一个更好的模式是,当你运行守护进程时,无论你想不想处于"debug“模式,你都可以通过。换句话说,像这样调用popen:

代码语言:javascript
复制
subprocess.Popen( ["python" , "daemon_script.py", "debug"] )

你可以像上面那样传递一个表示“调试模式开启”的字符串,也可以传递表示“调试”的日志级别常量,例如: subprocess.Popen( "python“,"daemon_script.py","10”) (https://docs.python.org/2/library/logging.html#levels)

然后在守护进程的init函数中使用argv,例如,获取该参数并使用它:

代码语言:javascript
复制
...
import sys

def __init__(self):
     self.daemon_name = __name__
     logging.basicConfig()  # <--- required, or I don't get any log messages
     log_level = int(sys.argv[1])  # Probably don't actually just blindly convert it without error handling
     self.logger = logging.getLogger(self.daemon_name)
     self.logger.setLevel(log_level)
     ...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51698157

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档