logging store info to different files

Solutions on MaxInterview for logging store info to different files by the best coders in the world

showing results for - "logging store info to different files"
Philippe
04 Sep 2016
1import logging
2formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
3
4
5def setup_logger(name, log_file, level=logging.INFO):
6    """To setup as many loggers as you want"""
7
8    handler = logging.FileHandler(log_file)        
9    handler.setFormatter(formatter)
10
11    logger = logging.getLogger(name)
12    logger.setLevel(level)
13    logger.addHandler(handler)
14
15    return logger
16
17# first file logger
18logger = setup_logger('first_logger', 'first_logfile.log')
19logger.info('This is just info message')
20
21# second file logger
22super_logger = setup_logger('second_logger', 'second_logfile.log')
23super_logger.error('This is an error message')
24
25def another_method():
26   # using logger defined above also works here
27   logger.info('Inside method')
28