crawler_task_management/public_function/public_func.py

43 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: utf-8 -*-
import os
import yaml
import logging
from pathlib import Path
from datetime import datetime
from logging.handlers import TimedRotatingFileHandler
def create_logger(file_name):
# 使用固定文件名,不包含日期
log_dir = os.path.join(Path(__file__).resolve().parent.parent, 'logs')
os.makedirs(log_dir, exist_ok=True)
log_filename = os.path.join(log_dir, f'{file_name}.log')
logger = logging.getLogger("emu_dsl")
if logger.handlers:
return logger
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# 配置TimedRotatingFileHandler设置时区
file_handler = TimedRotatingFileHandler(filename=log_filename, when='midnight', interval=1,
backupCount=3, encoding='utf-8', utc=False)
file_handler.setFormatter(formatter)
file_handler.suffix = "%Y%m%d" # 设置备份文件的后缀格式
logger.addHandler(file_handler)
logger.setLevel(logging.INFO)
return logger
def read_config(path):
if Path(path).exists():
with open(path, encoding="utf-8") as f:
config = yaml.safe_load(f)
return config
else:
raise FileNotFoundError
if __name__ == '__main__':
create_logger(file_name="all_task_management")