I have the following lines of code that initialize logging. I comment one out and leave the other to be used. The problem that I'm facing is that the one that is meant to log to the file not logging to file. It is instead logging to the console. Please help.
For logging to Console:
logging.basicConfig(level=logging.INFO,
format='%(asctime)s [%(levelname)s] (%(threadName)-10s) %(message)s',)
for file logging
logging.basicConfig(filename='server-soap.1.log',level=logging.INFO,
format='%(asctime)s [%(levelname)s] (%(threadName)-10s) %(message)s')
From the source code of logging I found the flows:
So, if some module we import called the
basicConfig()
method before us, our call will do nothing.A solution I found can work is that you can reload logging before your own calling to
basicConfig()
, such asI found out what the problem was. It was in the ordering of the imports and the logging definition.
The effect of the poor ordering was that the libraries that I imported before defining the logging using
logging.basicConfig()
defined the logging. This therefore took precedence to the logging that I was trying to define later usinglogging.basicConfig()
Below is how I needed to order it:
But the faulty ordering that I initially had was:
In case
basicConfig()
does not work:which gives me the following output:
For the sake of reference, Python Logging Cookbook is readworthy.
Another solution that worked for me is instead of tracing down which module might be importing
logging
or even callingbasicConfig
before me is to just callsetLevel
afterbasicConfig
again.Sort of crude, seems hacky, fixed my problem, worth a share.