As @offbyone comments, you can add redundant handlers to a single registrar instance. python docs for logging say -
"Multiple calls to getLogger () with the same name return a link to the same log object."
Therefore, we do not need to worry about making the implementation singletonic, as it already is.
Unfortunately, for handlers associated with the same registrar instance, the same is not true . There may be duplicate handlers.
Example -
Copy this code and save it in main.py
import logging print 'inside main.py', print '-'*50 def logger(): print 'initializing logger....' logPath = '.' fileName = 'temp'
and the following code in sub.py
print 'inside sub.py', print '-'*50 print 'importing main.py' import main print 'imported main.py' import logging print 'getting logger instance in sub' sub_logger = main.logger() print 'got logger instance in sub' sub_logger.info("utilizing sub_logger") print 'exiting sub.py', print '-'*50
Run sub.py
narayan@y510p:~/code/so$ python sub.py inside sub.py -------------------------------------------------- importing main.py inside main.py -------------------------------------------------- initializing logger.... adding handlers- logger initialized.... associated handlers - 2 <logging.FileHandler object at 0x7f7158740c90> <logging.StreamHandler object at 0x7f7158710b10> 2015-08-04 07:41:01,824 [main.py] [<module>] [INFO] [41] utilizing main.py logger. exiting main.py -------------------------------------------------- imported main.py getting logger instance in sub initializing logger.... adding handlers- logger initialized.... associated handlers - 4
Consequently, multiple method calls returning the same log have added duplicate handlers.
Now, for your question -
is there any way to check if a handler already exists
Yes there is -
logger.handlers returns a list of all handlers associated with this logger .
Before adding handlers to the registrar instance, do not add duplicate handlers In main.py, just do not comment on the line labeled if not len(logger.handlers): and indent the following two lines:
if not len(logger.handlers): logger.addHandler(fileHandler) logger.addHandler(consoleHandler)
Now run sub.py again
narayan@y510p:~/code/so$ python sub.py inside sub.py -------------------------------------------------- importing main.py inside main.py -------------------------------------------------- initializing logger.... adding handlers- logger initialized.... associated handlers - 2 <logging.FileHandler object at 0x7fd67a891c90> <logging.StreamHandler object at 0x7fd67a862b10> 2015-08-04 08:14:45,620 [main.py] [<module>] [INFO] [41] utilizing main.py logger. exiting main.py -------------------------------------------------- imported main.py getting logger instance in sub initializing logger.... adding handlers- logger initialized.... associated handlers - 2
Also, if you want to limit the type of handlers that are added to the log instance, you can do something like this -
print 'adding handlers- '
Hope this helps!
Edit:
Unfortunately, the same is not true for those associated with handlers with the same registrar instance. Duplicate handlers may be attached.
It turns out that the above statement is not entirely true.
Suppose we created and configured a logger called "main_logger" in the main module (which simply configures the logger, returns nothing).
# get the logger instance logger = logging.getLogger("main_logger")
Now in the submodule, if we create a child logger after the naming hierarchy "main_logger.sub_module_logger", we do not need to configure it in the submodule. Simply create a registrar for the naming hierarchy.
# get the logger instance logger = logging.getLogger("main_logger.sub_module_logger")
And he will not add a duplicate handler.
Link- Using Logging in Multiple Modules