Skip to content Skip to sidebar Skip to footer

Logging In Python. Handlers And Console Duplicates

I have a simple logging setup: def main() # .... # Create logger logging.basicConfig(filemode='w', level=logging.DEBUG) logger = logging.getLogger(__name__) logger.setLe

Solution 1:

logging.basicConfig(filemode='w', level=logging.DEBUG)

creates a StreamHandler. So your code is creating two StreamHandlers, one with logging level DEBUG and another with level INFO.

basicConfig is a convenience function. If you want to create your own handlers, it is not necessary to call basicConfig. (Or you can call basicConfig and add additional handlers...)


If you don't supply a filename in the call to basicConfig, then a StreamHandler is added to the root logger. This is the code inside the basicConfig function:

if handlers is None:
    filename = kwargs.get("filename")
    if filename:
        mode = kwargs.get("filemode", 'a')
        h = FileHandler(filename, mode)
    else:
        stream = kwargs.get("stream")
        h = StreamHandler(stream)

Post a Comment for "Logging In Python. Handlers And Console Duplicates"