refactor: use config and logger in service
This commit is contained in:
17
src/main.py
17
src/main.py
@@ -1,11 +1,26 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
# created by mmmy on 2025-09-27
|
# created by mmmy on 2025-09-27
|
||||||
|
import os
|
||||||
|
import argparse
|
||||||
import uvicorn
|
import uvicorn
|
||||||
from web.api import api
|
from web.api import api
|
||||||
|
from utils import config, logger
|
||||||
|
|
||||||
# 主函数
|
# 主函数
|
||||||
def main():
|
def main():
|
||||||
uvicorn.run(api, host='0.0.0.0', port=8099)
|
main_path = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
parser = argparse.ArgumentParser(description='IF.u 服务')
|
||||||
|
parser.add_argument('--config', type=str, default=os.path.join(main_path, '../configuration/test_conf.ini'), help='配置文件路径')
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
config.init(args.config)
|
||||||
|
logger.init()
|
||||||
|
|
||||||
|
conf = config.get_instance()
|
||||||
|
|
||||||
|
host = conf.get('web_service', 'server_host', fallback='0.0.0.0')
|
||||||
|
port = conf.getint('web_service', 'server_port', fallback=8099)
|
||||||
|
uvicorn.run(api, host=host, port=port)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
0
src/utils/__init__.py
Normal file
0
src/utils/__init__.py
Normal file
24
src/utils/config.py
Normal file
24
src/utils/config.py
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
import configparser
|
||||||
|
|
||||||
|
config = None
|
||||||
|
|
||||||
|
def init(config_file: str):
|
||||||
|
global config
|
||||||
|
config = configparser.ConfigParser()
|
||||||
|
config.read(config_file)
|
||||||
|
|
||||||
|
def get_instance() -> configparser.ConfigParser:
|
||||||
|
return config
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
# 本文件的绝对路径
|
||||||
|
import os
|
||||||
|
config_file = os.path.join(os.path.dirname(__file__), "../../configuration/test_conf.ini")
|
||||||
|
init(config_file)
|
||||||
|
conf = get_instance()
|
||||||
|
print(conf.sections())
|
||||||
|
for section in conf.sections():
|
||||||
|
print(conf.options(section))
|
||||||
|
for option in conf.options(section):
|
||||||
|
print(f"{section}.{option}={conf.get(section, option)}")
|
||||||
91
src/utils/logger.py
Normal file
91
src/utils/logger.py
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
import logging
|
||||||
|
import os
|
||||||
|
from datetime import datetime
|
||||||
|
from .config import get_instance as get_config
|
||||||
|
|
||||||
|
# 定义颜色代码
|
||||||
|
class Colors:
|
||||||
|
RED = '\033[31m'
|
||||||
|
GREEN = '\033[32m'
|
||||||
|
YELLOW = '\033[33m'
|
||||||
|
BLUE = '\033[34m'
|
||||||
|
MAGENTA = '\033[35m'
|
||||||
|
CYAN = '\033[36m'
|
||||||
|
WHITE = '\033[37m'
|
||||||
|
RESET = '\033[0m' # 重置颜色
|
||||||
|
|
||||||
|
# 自定义控制台处理器,为不同日志级别添加颜色
|
||||||
|
class ColoredConsoleHandler(logging.StreamHandler):
|
||||||
|
def emit(self, record):
|
||||||
|
# 为不同日志级别设置颜色
|
||||||
|
colors = {
|
||||||
|
logging.DEBUG: Colors.CYAN,
|
||||||
|
logging.INFO: Colors.GREEN,
|
||||||
|
logging.WARNING: Colors.YELLOW,
|
||||||
|
logging.ERROR: Colors.RED,
|
||||||
|
logging.CRITICAL: Colors.MAGENTA
|
||||||
|
}
|
||||||
|
|
||||||
|
# 获取对应级别的颜色,默认为白色
|
||||||
|
color = colors.get(record.levelno, Colors.WHITE)
|
||||||
|
|
||||||
|
# 获取原始消息
|
||||||
|
message = self.format(record)
|
||||||
|
|
||||||
|
# 添加颜色并输出
|
||||||
|
self.stream.write(f"{color}{message}{Colors.RESET}\n")
|
||||||
|
self.flush()
|
||||||
|
|
||||||
|
def init():
|
||||||
|
config = get_config()
|
||||||
|
log_dir = config.get("log", "log_dir", fallback="logs")
|
||||||
|
log_file = config.get("log", "log_file", fallback="if.u.service")
|
||||||
|
log_level = config.get("log", "log_level", fallback=logging.INFO)
|
||||||
|
console_log_level = config.get("log", "console_log_level", fallback=logging.DEBUG)
|
||||||
|
|
||||||
|
# 创建logs目录(如果不存在)
|
||||||
|
if not os.path.exists(log_dir):
|
||||||
|
os.makedirs(log_dir)
|
||||||
|
|
||||||
|
# 设置日志格式
|
||||||
|
log_format = "[%(asctime)s.%(msecs)03d][%(filename)s:%(lineno)d][%(levelname)s] %(message)s"
|
||||||
|
date_format = "%Y-%m-%d %H:%M:%S"
|
||||||
|
|
||||||
|
# 创建格式化器
|
||||||
|
formatter = logging.Formatter(log_format, datefmt=date_format)
|
||||||
|
|
||||||
|
# 获取根日志记录器
|
||||||
|
root_logger = logging.getLogger()
|
||||||
|
root_logger.setLevel(logging.NOTSET)
|
||||||
|
|
||||||
|
# 清除现有的处理器
|
||||||
|
root_logger.handlers.clear()
|
||||||
|
|
||||||
|
# 创建控制台处理器并设置颜色
|
||||||
|
console_handler = ColoredConsoleHandler()
|
||||||
|
console_handler.setFormatter(formatter)
|
||||||
|
console_handler.setLevel(console_log_level)
|
||||||
|
root_logger.addHandler(console_handler)
|
||||||
|
|
||||||
|
# 创建文件处理器
|
||||||
|
log_filename = os.path.join(log_dir, f"{log_file}_{datetime.now().strftime('%Y%m%d')}.log")
|
||||||
|
file_handler = logging.FileHandler(log_filename, encoding='utf-8')
|
||||||
|
file_handler.setFormatter(formatter)
|
||||||
|
file_handler.setLevel(log_level)
|
||||||
|
root_logger.addHandler(file_handler)
|
||||||
|
|
||||||
|
# 确保日志消息被正确处理
|
||||||
|
logging.addLevelName(logging.DEBUG, "D")
|
||||||
|
logging.addLevelName(logging.INFO, "I")
|
||||||
|
logging.addLevelName(logging.WARNING, "W")
|
||||||
|
logging.addLevelName(logging.ERROR, "E")
|
||||||
|
logging.addLevelName(logging.CRITICAL, "C")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
init(log_dir="logs", log_file="test", log_level=logging.INFO, console_log_level=logging.DEBUG)
|
||||||
|
logging.debug("debug log")
|
||||||
|
logging.info("info log")
|
||||||
|
logging.warning("warning log")
|
||||||
|
logging.error("error log")
|
||||||
|
logging.critical("critical log")
|
||||||
Reference in New Issue
Block a user