代码优化
This commit is contained in:
parent
350c9e83fa
commit
0191f3cda5
3
main.py
3
main.py
|
|
@ -7,10 +7,11 @@ from pathlib import Path
|
|||
from typing import Dict, Any, Optional, List
|
||||
from fastapi import FastAPI, HTTPException, Depends
|
||||
|
||||
from public_function.public_func import read_config
|
||||
from public_function.public_func import read_config, create_logger
|
||||
from model.model import GoodsInfo, DataReceive, AccountCreate, AccountUpdate, DeviceResetData
|
||||
|
||||
app = FastAPI()
|
||||
logger = create_logger(file_name="crawler_main")
|
||||
|
||||
|
||||
def get_config():
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
import os
|
||||
import yaml
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
|
@ -7,19 +8,22 @@ from logging.handlers import TimedRotatingFileHandler
|
|||
|
||||
|
||||
def create_logger(file_name):
|
||||
now_date = datetime.now().strftime('%Y%m%d')
|
||||
log_filename = os.path.join(Path(__file__).resolve().parent.parent, f'logs/{file_name}_{now_date}.log')
|
||||
path = Path(log_filename).resolve().parent
|
||||
os.makedirs(path, exist_ok=True)
|
||||
# 使用固定文件名,不包含日期
|
||||
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")
|
||||
# 防止重复添加handler
|
||||
if logger.handlers:
|
||||
return logger
|
||||
|
||||
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
||||
file_handler = TimedRotatingFileHandler(log_filename, when='midnight', backupCount=7, encoding='utf-8')
|
||||
|
||||
# 配置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
|
||||
|
|
@ -32,3 +36,7 @@ def read_config(path):
|
|||
return config
|
||||
else:
|
||||
raise FileNotFoundError
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
create_logger(file_name="all_task_management")
|
||||
|
|
|
|||
|
|
@ -2,10 +2,12 @@
|
|||
import time
|
||||
import json
|
||||
from typing import Dict, Any
|
||||
|
||||
from public_function.asyn_mysql import AsyncMySQL
|
||||
from public_function.public_func import create_logger
|
||||
from public_function.redis_task_manager import RedisTaskManager
|
||||
|
||||
logger = create_logger(file_name="all_task_management")
|
||||
|
||||
|
||||
class AllTask:
|
||||
def __init__(self, config_data: Dict[str, Any]):
|
||||
|
|
|
|||
Loading…
Reference in New Issue