Opt: Separate logs, all instances save to their own file

This commit is contained in:
LmeSzinc 2021-10-16 12:59:29 +08:00
parent 1481a9b582
commit 2ec4f0a217
2 changed files with 24 additions and 12 deletions

View File

@ -11,7 +11,7 @@ from module.config.config import AzurLaneConfig, TaskEnd
from module.config.config_updater import ConfigUpdater
from module.config.utils import deep_get
from module.exception import *
from module.logger import logger, log_file
from module.logger import logger
class AzurLaneAutoScript:
@ -84,7 +84,7 @@ class AzurLaneAutoScript:
image_time = datetime.strftime(data['time'], '%Y-%m-%d_%H-%M-%S-%f')
image = handle_sensitive_image(data['image'])
image.save(f'{folder}/{image_time}.png')
with open(log_file, 'r', encoding='utf-8') as f:
with open(logger.log_file, 'r', encoding='utf-8') as f:
lines = f.readlines()
start = 0
for index, line in enumerate(lines):
@ -228,6 +228,9 @@ class AzurLaneAutoScript:
name=self.config.Campaign_Name, folder=self.config.Campaign_Event, mode=self.config.Campaign_Mode)
def loop(self):
logger.info(f'Start scheduler loop: {self.config_name}')
logger.set_file_logger(self.config_name)
is_first = True
while 1:
if is_first and self.config.task == 'Restart':

View File

@ -31,16 +31,22 @@ os.chdir(os.path.join(os.path.dirname(__file__), '../'))
# Add file logger
pyw_name = os.path.splitext(os.path.basename(sys.argv[0]))[0]
if '_' in pyw_name:
pyw_name = '_'.join(pyw_name.split('_')[:-1])
log_file = f'./log/{datetime.date.today()}_{pyw_name}.txt'
try:
file = logging.FileHandler(log_file, encoding='utf-8')
except FileNotFoundError:
os.mkdir('./log')
file = logging.FileHandler(log_file, encoding='utf-8')
file.setFormatter(formatter)
logger.addHandler(file)
def set_file_logger(name=pyw_name):
if '_' in name:
name = name.split('_', 1)[0]
log_file = f'./log/{datetime.date.today()}_{name}.txt'
try:
file = logging.FileHandler(log_file, encoding='utf-8')
except FileNotFoundError:
os.mkdir('./log')
file = logging.FileHandler(log_file, encoding='utf-8')
file.setFormatter(formatter)
logger.handlers = [h for h in logger.handlers if not isinstance(h, logging.FileHandler)]
logger.addHandler(file)
logger.log_file = log_file
def hr(title, level=3):
@ -73,5 +79,8 @@ def attr_align(name, text, front='', align=22):
logger.hr = hr
logger.attr = attr
logger.attr_align = attr_align
logger.set_file_logger = set_file_logger
logger.log_file: str
logger.set_file_logger()
logger.hr('Start', level=0)