From 28eb45506889bb44986de20061771777b7038e2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=B9=E8=88=9F?= <13007110208@163.com> Date: Fri, 18 Jul 2025 17:45:06 +0800 Subject: [PATCH] 1 --- main.py | 31 +++++++++++++++++++--- src/utils/Log.py | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 src/utils/Log.py diff --git a/main.py b/main.py index dd13ce4..e0797ac 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,29 @@ -def main(): - print("Hello from unified-python!") +from fastapi import FastAPI +import uvicorn +from src.utils.Log import Log + +app = FastAPI(title="yin_home文档") -if __name__ == "__main__": - main() +@app.get('/weather', tags=["获取天气数据"], summary="返回实时天气数据") +def weather(): + return { + "code": 200, + "data": { + "city": "上海", + "weather": "多云", + "temperature": "25", + "wind": "南风", + "humidity": "60", + "pressure": "1010", + "update_time": "2021-09-01 12:00:00" + } + } + + + + + + +if __name__ == '__main__': + uvicorn.run(app='main:app', host='0.0.0.0', port=1314, reload=True) diff --git a/src/utils/Log.py b/src/utils/Log.py new file mode 100644 index 0000000..b786529 --- /dev/null +++ b/src/utils/Log.py @@ -0,0 +1,68 @@ +import logging +import os +from datetime import datetime + +# 定义全局变量 log_path +cur_path = os.path.dirname(os.path.realpath(__file__)) +log_path = os.path.join(os.path.dirname(cur_path), 'logs') + + +class Log(): + def __init__(self, logger_name='my_logger'): + self.logger = logging.getLogger(logger_name) + if self.logger.hasHandlers(): + self.logger.handlers.clear() + self.logger.setLevel(logging.INFO) + + if not os.path.exists(log_path): + os.makedirs(log_path) + + self.update_log_file() + + def update_log_file(self): + self.log_name = os.path.join(log_path, 'yinhome.log') + + for handler in self.logger.handlers[:]: + self.logger.removeHandler(handler) + + fh = logging.FileHandler(self.log_name, 'a', encoding='utf-8') + fh.setLevel(logging.INFO) + + ch = logging.StreamHandler() + ch.setLevel(logging.INFO) + + formatter = logging.Formatter( + '[%(asctime)s] %(filename)s line:%(lineno)d [%(levelname)s]%(message)s', + datefmt="%Y-%m-%d %H:%M:%S" + ) + fh.setFormatter(formatter) + ch.setFormatter(formatter) + + self.logger.addHandler(fh) + self.logger.addHandler(ch) + + def getlog(self): + current_date = datetime.now().strftime("%Y_%m_%d") + log_date = os.path.basename(self.log_name).split('.')[0] + if current_date != log_date: + self.update_log_file() + return self.logger + + def info(self, msg, *args, **kwargs): + logger = self.getlog() + logger.info(msg, *args, **kwargs) + + def error(self, msg, *args, **kwargs): + logger = self.getlog() + logger.error(msg, *args, **kwargs) + + def warning(self, msg, *args, **kwargs): + logger = self.getlog() + logger.warning(msg, *args, **kwargs) + + +if __name__ == "__main__": + log = Log() + log.info("---测试开始----") + log.error("操作步骤1,2,3") + log.warning("----测试结束----") \ No newline at end of file