summaryrefslogtreecommitdiffstats
path: root/scripts/automation/regression/CustomLogger.py
blob: 14ef1362314d6a132d6d019ab6f3f4065781d7ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import sys
import os
import logging


# def setup_custom_logger(name, log_path = None):
#     logging.basicConfig(level   = logging.INFO, 
#                         format  = '%(asctime)s %(name)-10s %(module)-20s %(levelname)-8s %(message)s',
#                         datefmt = '%m-%d %H:%M')


def setup_custom_logger(name, log_path = None):
    # first make sure path availabe
    if log_path is None:
        log_path = os.getcwd()+'/trex_log.log'
    else:
        directory = os.path.dirname(log_path)
        if not os.path.exists(directory):
            os.makedirs(directory)
    logging.basicConfig(level   = logging.DEBUG, 
                        format  = '%(asctime)s %(name)-10s %(module)-20s %(levelname)-8s %(message)s',
                        datefmt = '%m-%d %H:%M',
                        filename= log_path, 
                        filemode= 'w')

    # define a Handler which writes INFO messages or higher to the sys.stderr
    consoleLogger = logging.StreamHandler()
    consoleLogger.setLevel(logging.ERROR)
    # set a format which is simpler for console use
    formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
    # tell the handler to use this format
    consoleLogger.setFormatter(formatter)

    # add the handler to the logger
    logging.getLogger(name).addHandler(consoleLogger)