import logging
import os.path
import time
class Logger(object):
def __init__(self, logger):
"""Specify the file path to save the log, the log level, and the calling file
Save the log to the specified file"""
# create a logger
self.logger = logging.getLogger(logger)
self.logger.setLevel(logging.DEBUG)
# Create a handler for writing to the log file
rq = time.strftime('%y%m%d %H%M', time.localtime(time.time()))
# Save logs in /Logs in the project root directory
log_path = os.path.dirname(os.path.abspath('.')) + '\logs\\'
# If the case organization structure is /testsuit/featuremodel/xxx.py, then the parent path of the relative path obtained is the project root directory
log_name = log_path + rq + '.log'
fh =logging.FileHandler(log_name, 'a', encoding='utf-8')
fh.setLevel(logging.INFO)
# Create another handler for output to the console
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
# Define the output format of the handler
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
# Add handler to logger
self.logger.addHandler(fh)
self.logger.addHandler(ch)
def getlog(self):
return self.logger