Dynamically changing log level without restarting the application

opensourcegeek picture opensourcegeek · Oct 27, 2013 · Viewed 57k times · Source

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?

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.

Answer

Martijn Pieters picture Martijn Pieters · Oct 27, 2013

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.

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)