1823b8294SYaroslav Brustinov
2823b8294SYaroslav Brustinovimport sys
3823b8294SYaroslav Brustinovimport os
4823b8294SYaroslav Brustinovimport logging
5823b8294SYaroslav Brustinov
6823b8294SYaroslav Brustinov
7823b8294SYaroslav Brustinov# def setup_custom_logger(name, log_path = None):
8823b8294SYaroslav Brustinov#     logging.basicConfig(level   = logging.INFO,
9823b8294SYaroslav Brustinov#                         format  = '%(asctime)s %(name)-10s %(module)-20s %(levelname)-8s %(message)s',
10823b8294SYaroslav Brustinov#                         datefmt = '%m-%d %H:%M')
11823b8294SYaroslav Brustinov
12823b8294SYaroslav Brustinov
13823b8294SYaroslav Brustinovdef setup_custom_logger(name, log_path = None):
14823b8294SYaroslav Brustinov    # first make sure path availabe
15823b8294SYaroslav Brustinov    if log_path is None:
16823b8294SYaroslav Brustinov        log_path = os.getcwd()+'/trex_log.log'
17823b8294SYaroslav Brustinov    else:
18823b8294SYaroslav Brustinov        directory = os.path.dirname(log_path)
19823b8294SYaroslav Brustinov        if not os.path.exists(directory):
20823b8294SYaroslav Brustinov            os.makedirs(directory)
21823b8294SYaroslav Brustinov    logging.basicConfig(level   = logging.DEBUG,
22823b8294SYaroslav Brustinov                        format  = '%(asctime)s %(name)-10s %(module)-20s %(levelname)-8s %(message)s',
23823b8294SYaroslav Brustinov                        datefmt = '%m-%d %H:%M',
24823b8294SYaroslav Brustinov                        filename= log_path,
25823b8294SYaroslav Brustinov                        filemode= 'w')
26823b8294SYaroslav Brustinov
27823b8294SYaroslav Brustinov    # define a Handler which writes INFO messages or higher to the sys.stderr
28823b8294SYaroslav Brustinov    consoleLogger = logging.StreamHandler()
29823b8294SYaroslav Brustinov    consoleLogger.setLevel(logging.ERROR)
30823b8294SYaroslav Brustinov    # set a format which is simpler for console use
31823b8294SYaroslav Brustinov    formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
32823b8294SYaroslav Brustinov    # tell the handler to use this format
33823b8294SYaroslav Brustinov    consoleLogger.setFormatter(formatter)
34823b8294SYaroslav Brustinov
35823b8294SYaroslav Brustinov    # add the handler to the logger
36823b8294SYaroslav Brustinov    logging.getLogger(name).addHandler(consoleLogger)