Python logging - excluding submodule
Question: I have a python **main** which users various submodules. structure is like
this:

    
    
    root:.
    │   main.py
    │
    └───MyModule
            file1.py
            file2.py
            special.py
    

MyModule outputs some important logs (each file does logger =
logging.getLogger(**name**) ). However - "special.py" logs need to be stored
separately.

I attempt:

    
    
    MyModuleHandler = logging.handlers.RotatingFileHandler('MyModule.log', maxBytes= 5000000, backupCount=5)
    MyModuleHandler.setFormatter(formatter)
    MyModuleHandler.setLevel(logging.DEBUG)
    
    
    specialHandler = logging.handlers.RotatingFileHandler('special.log', maxBytes= 5000000, backupCount=5)
    specialHandler.setFormatter(formatter)
    specialHandler.setLevel(logging.INFO)
    
    console = logging.StreamHandler()
    console.setLevel(logging.INFO)
    console.setFormatter(formatter)
    
    logging.getLogger('MyModule.special').setLevel(logging.DEBUG)
    logging.getLogger('MyModule.special').addHandler(specialHandler)
    
    logging.getLogger('MyModule').addHandler(console)
    logging.getLogger('MyModule').setLevel(logging.DEBUG)
    logging.getLogger('MyModule').addHandler(MyModuleHandler)
    

guys, what am I doing wrong?


Answer: OK. This was really silly All i needed was to set the propagate field in the
specified logger.

correct code should be:

    
    
    MyModuleHandler = logging.handlers.RotatingFileHandler('MyModule.log', maxBytes= 5000000, backupCount=5)
    MyModuleHandler.setFormatter(formatter)
    MyModuleHandler.setLevel(logging.DEBUG)
    
    
    specialHandler = logging.handlers.RotatingFileHandler('special.log', maxBytes= 5000000, backupCount=5)
    specialHandler.setFormatter(formatter)
    specialHandler.setLevel(logging.INFO)
    
    console = logging.StreamHandler()
    console.setLevel(logging.INFO)
    console.setFormatter(formatter)
    
    logging.getLogger('MyModule.special').setLevel(logging.DEBUG)
    logging.getLogger('MyModule.special').addHandler(specialHandler)
    logging.getLogger('MyModule.special').propagate = False
    
    logging.getLogger('MyModule').addHandler(console)
    logging.getLogger('MyModule').setLevel(logging.DEBUG)
    logging.getLogger('MyModule').addHandler(MyModuleHandler)
    

