I have got various modules in which I use Python logging heavily. When I import them into the main module like in the Python documentation and try to run it, I don't get any output from the logging. Has anyone got any idea what is happening?
Logging is called in the module imported by the public
module imported below (the piece of code is too large to be put up here). The piece of code below is where the whole program is run and the logging is initialized:
import logging
from bottle import run, debug
import public
logging.basicConfig(level=logging.DEBUG)
if __name__ == '__main__':
logging.info('Started')
debug(mode=True)
run(host='localhost', port = 8080, reloader=True)
logging.info('Finished')
EDIT 1:
The following was based on a misunderstanding of the OP's code (from the comment) and a false assumption on my part. As such, it's invalid.
There was at least one error with the code you provided.
debug(mode=True)
is wrong for two reasons:mode=True
is an assignment, not a test for equalityThe following, stripped of any ancillary modules and code, runs and logs for me:
Run from the command line:
Your problem is probably being caused by the
import public
statement making a call tologging.debug(...)
or similar. What then happens is this:import public
. As a side-effect, this calls e.g.logging.debug
or similar, which automatically callsbasicConfig
, which adds aStreamHandler
to the root logger, but doesn't change the level.basicConfig
, but as the root logger already has a handler, it does nothing (as documented).WARNING
, yourinfo
anddebug
calls produce no output.You really should avoid side-effects on import: for example, your call to
basicConfig
should be in theif __name__ == '__main__'
clause. With thispublic.py
:and this
main.py
:You get the following output:
You'll see from this that Bottle actually re-runs the script in a separate process, which accounts for the doubling up of messages. You can illustrate this by using a format string which shows the process ID: if you use
then you get output like
Note that if you add a side-effect producing statement to
public.py
like this:at the module level, then you get no logging output at all:
which appears to confirm the above analysis.
I have try this code in my project. run configure_loggint(logpath) in the main.
and you can use