动态更改日志级别,而无需重新启动应用程序 [英] Dynamically changing log level without restarting the application

查看:64
本文介绍了动态更改日志级别,而无需重新启动应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在python中使用fileConfig更改日志级别而无需重新启动应用程序.如果无法通过fileConfig实现,是否还有其他方法可以得到相同的结果?

Is it possible to change the log level using fileConfig in python without restarting the application. If it cannot be achieved through fileConfig is there some other way to get the same result?

更新:这是针对服务器上运行的应用程序,我希望系统管理员能够更改应用程序在运行时选择的配置文件,并动态更改日志级别.当时我正在使用gevent,因此我将代码添加为使用inotify来选择对配置文件进行更改的答案之一.

Update: This was for an application running on a server, I wanted sys admins to be able to change a config file that would be picked during run time by application and change the log level dynamically. I was working with gevent at that time hence I've added my code as one of the answers which uses inotify to pick changes to config file.

推荐答案

fileConfig 是一种基于文件为您配置日志级别的机制.您可以随时在程序中动态更改它.

fileConfig is a mechanism to configure the log level for you based on a file; you can dynamically change it at any time in your program.

调用 .setLevel()在要更改其日志级别的日志记录对象上.通常,您会在根目录上执行此操作:

Call .setLevel() on the logging object for which you want to change the log level. Usually you'd do that on the root:

logging.getLogger().setLevel(logging.DEBUG)

这篇关于动态更改日志级别,而无需重新启动应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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