You have 2 distinct loggers and you're only configuring one.
The first is the one you make in log.py
and set up correctly. Its name however will be log
, because you have imported this module from read_file.py
.
The second logger, the one you're hoping is the same as the first, is the one you assign to the variable logger
in read_file.py
. Its name will be __main__
because you're calling this module from the command line. You're not configuring this logger.
What you could do is to add a parameter to set_up_log
to pass the name of the logger in, e.g.
def set_up_log(logname):
logger = logging.getLogger(logname)
That way, you will set the handlers and formatters for the correct logging instance.
Organizing your logs in a hierarchy is the way logging was intended to be used by Vinay Sajip, the original author of the module. So your modules would only log to a logging instance with the fully qualified name, as given by __name__
. Then your application code could set up the loggers, which is what you're trying to accomplish with your set_up_log
function. You just need to remember to pass it the relevant name, that's all. I found this reference very useful at the time.