CustomLogger.py revision 823b8294
1
2import sys
3import os
4import logging
5
6
7# def setup_custom_logger(name, log_path = None):
8#     logging.basicConfig(level   = logging.INFO,
9#                         format  = '%(asctime)s %(name)-10s %(module)-20s %(levelname)-8s %(message)s',
10#                         datefmt = '%m-%d %H:%M')
11
12
13def setup_custom_logger(name, log_path = None):
14    # first make sure path availabe
15    if log_path is None:
16        log_path = os.getcwd()+'/trex_log.log'
17    else:
18        directory = os.path.dirname(log_path)
19        if not os.path.exists(directory):
20            os.makedirs(directory)
21    logging.basicConfig(level   = logging.DEBUG,
22                        format  = '%(asctime)s %(name)-10s %(module)-20s %(levelname)-8s %(message)s',
23                        datefmt = '%m-%d %H:%M',
24                        filename= log_path,
25                        filemode= 'w')
26
27    # define a Handler which writes INFO messages or higher to the sys.stderr
28    consoleLogger = logging.StreamHandler()
29    consoleLogger.setLevel(logging.ERROR)
30    # set a format which is simpler for console use
31    formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
32    # tell the handler to use this format
33    consoleLogger.setFormatter(formatter)
34
35    # add the handler to the logger
36    logging.getLogger(name).addHandler(consoleLogger)