How can I remove / inspect / modify handlers configured for my loggers using the fileConfig() function?
For removing there is Logger.removeHandler(hdlr) method, but how
Re removing handlers - if it helps anyone I wanted to remove all handlers for all the logs I'd created (I was learning about python logging) and came up with this:
def log_close():
# get all loggers
loggers = [logging.getLogger(name) if 'your_app_name' in name else None for name in logging.root.manager.loggerDict]
# for each valid logger remove all handlers
for log in loggers:
if log != None:
while bool(len(log.handlers)):
for handler in log.handlers:
log.removeHandler(handler)
# set up log
log = logging.getLogger('your_app_name')
log.setLevel('DEBUG')
log_format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
date_format = '%Y-%m-%d %H:%M:%S'
formatter = logging.Formatter(log_format, date_format)
log_filename = 'your_app_name.log'
log_write_mode = 'w+'
file_handler = logging.FileHandler(log_filename, log_write_mode)
file_handler.setFormatter(formatter)
log.addHandler(file_handler)
console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)
log.addHandler(console_handler)