I used to Python logging, it works fine. The logging.basicConfig(...)
set in one module (a some.py
file), then we can use logging
every where. Obviously, logging is global.
The question is how logging find it's settings, when we not call the module
where basicConfig(...) appeared (in some.py file )? Is logging scan all the packages?
Even the logging.basicConfig(...)
put into an any.py
and the module (any.py
) never get imported, or not used anywhere, the logging setting take effect!
To understand logging you have dive into Python's standard library sources.
Here is the trick:
#/usr/lib/python3.2/logging/__init__.py
...
root = RootLogger(WARNING)
Logger.root = root
Logger.manager = Manager(Logger.root)
...
# and
def basicConfig(**kwargs):
...
hdlr = StreamHandler(stream)
fs = kwargs.get("format", BASIC_FORMAT)
dfs = kwargs.get("datefmt", None)
style = kwargs.get("style", '%')
fmt = Formatter(fs, dfs, style)
hdlr.setFormatter(fmt)
root.addHandler(hdlr)
So, when you call basicconfig()
with certain parameters, root
logger is set.
Finally getLogger
:
def getLogger(name=None):
"""
Return a logger with the specified name, creating it if necessary.
If no name is specified, return the root logger.
"""
if name:
return Logger.manager.getLogger(name)
else:
return root
I think there is no magic scanning here.
Try to test it this way in a separate test
directory:
test/main.py:
import logging
logging.info('test')
test/any.py:
import logging
logging.basicConfig(filename='test.log', level=logging.INFO)
python main.py
Result: NO test.log
file.
Now let's update the test:
test/main.py:
import logging
import any
logging.info('test')
python main.py
Result: new test.log
file with INFO:root:test
string inside.
So I guess that any.py
in your case is imported somehow,
despite your expectations.
You may find the way any.py
is imported easily,
just add few lines there:
test/any.py:
from traceback import print_stack
print_stack()
...
python main.py
Result:
File "main.py", line 2, in
import any
File "any.py", line 2, in
print_stack()
This stack shows that any.py
is imported from main.py
.
I hope you will find where it is imported from in your case.
no magics
happens when it put into a new place. In my old project, I do put the logging setting logging.basicConfig() into a new and not-used file, and it do show up. Only thing possible to scan the file is the testing tool, pytest in case - Andrew_1510 2012-04-06 17:54