2021-05-11 17:59:29 +08:00
|
|
|
import logging
|
2021-05-17 10:14:35 +08:00
|
|
|
import sys
|
2021-05-11 17:59:29 +08:00
|
|
|
|
2021-06-09 16:19:48 +08:00
|
|
|
from config.log_config import log_config
|
2021-05-11 17:59:29 +08:00
|
|
|
|
|
|
|
|
2021-05-13 12:17:15 +08:00
|
|
|
class TestLog:
|
2021-06-02 19:21:33 +08:00
|
|
|
def __init__(self, logger, log_debug, log_file, log_err):
|
2021-05-11 17:59:29 +08:00
|
|
|
self.logger = logger
|
2021-06-02 19:21:33 +08:00
|
|
|
self.log_debug = log_debug
|
2021-05-11 17:59:29 +08:00
|
|
|
self.log_file = log_file
|
|
|
|
self.log_err = log_err
|
|
|
|
|
|
|
|
self.log = logging.getLogger(self.logger)
|
|
|
|
self.log.setLevel(logging.DEBUG)
|
|
|
|
|
|
|
|
try:
|
2021-07-12 13:11:53 +08:00
|
|
|
formatter = logging.Formatter("[%(asctime)s - %(levelname)s - %(name)s]: "
|
|
|
|
"%(message)s (%(filename)s:%(lineno)s)")
|
2021-06-02 19:21:33 +08:00
|
|
|
dh = logging.FileHandler(self.log_debug)
|
|
|
|
dh.setLevel(logging.DEBUG)
|
|
|
|
dh.setFormatter(formatter)
|
|
|
|
self.log.addHandler(dh)
|
|
|
|
|
|
|
|
fh = logging.FileHandler(self.log_file)
|
|
|
|
fh.setLevel(logging.INFO)
|
2021-05-11 17:59:29 +08:00
|
|
|
fh.setFormatter(formatter)
|
|
|
|
self.log.addHandler(fh)
|
|
|
|
|
2021-06-02 19:21:33 +08:00
|
|
|
eh = logging.FileHandler(self.log_err)
|
2021-05-11 17:59:29 +08:00
|
|
|
eh.setLevel(logging.ERROR)
|
|
|
|
eh.setFormatter(formatter)
|
|
|
|
self.log.addHandler(eh)
|
|
|
|
|
2021-06-04 18:34:34 +08:00
|
|
|
ch = logging.StreamHandler(sys.stdout)
|
|
|
|
ch.setLevel(logging.DEBUG)
|
|
|
|
ch.setFormatter(formatter)
|
2021-07-09 14:03:47 +08:00
|
|
|
# self.log.addHandler(ch)
|
2021-06-04 18:34:34 +08:00
|
|
|
|
2021-05-11 17:59:29 +08:00
|
|
|
except Exception as e:
|
2021-06-07 15:21:36 +08:00
|
|
|
print("Can not use %s or %s or %s to log. error : %s" % (log_debug, log_file, log_err, str(e)))
|
2021-05-11 17:59:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
"""All modules share this unified log"""
|
2021-06-09 16:19:48 +08:00
|
|
|
log_debug = log_config.log_debug
|
|
|
|
log_info = log_config.log_info
|
|
|
|
log_err = log_config.log_err
|
2021-06-07 15:21:36 +08:00
|
|
|
test_log = TestLog('ci_test', log_debug, log_info, log_err).log
|