From 398ad56933e8877dc810606c6fb39f07968e2a93 Mon Sep 17 00:00:00 2001 From: Cxx0822 <1556464090@qq.com> Date: Mon, 20 Oct 2025 15:43:03 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=9B=B4=E6=94=B9=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/elastic.py | 11 +++++++---- config/logging.py | 31 +------------------------------ config/setting.py | 1 + 3 files changed, 9 insertions(+), 34 deletions(-) diff --git a/config/elastic.py b/config/elastic.py index bf589e9..f04d723 100644 --- a/config/elastic.py +++ b/config/elastic.py @@ -10,8 +10,11 @@ ES_PASSWORD = "19940822Cxx" BLOG_INDEX = "blog" # 注意Python的elasticsearch的版本要和服务器的一致 -es = Elasticsearch(ES_HOST, http_auth=(ES_USER, ES_PASSWORD)) +try: + es = Elasticsearch(ES_HOST, http_auth=(ES_USER, ES_PASSWORD)) -if not es.indices.exists(index=BLOG_INDEX): - es.indices.create(index=BLOG_INDEX, body=BLOG_MAPPING) - logger.info(f"Elastic索引 {BLOG_INDEX} 创建成功") + if not es.indices.exists(index=BLOG_INDEX): + es.indices.create(index=BLOG_INDEX, body=BLOG_MAPPING) + logger.info(f"Elastic索引 {BLOG_INDEX} 创建成功") +except Exception as e: + logger.error(f"elastic异常: {str(e)}") diff --git a/config/logging.py b/config/logging.py index 2c6a00a..079c195 100644 --- a/config/logging.py +++ b/config/logging.py @@ -3,18 +3,13 @@ import sys from fluent import sender from loguru import logger -from pathlib import Path from config.setting import settings -FLUENTD_HOST = 'host.docker.internal' +FLUENTD_HOST = settings.FLUENTD_HOST FLUENTD_PORT = 24224 TOPIC_TAG = 'blog-service' -# 日志目录 -LOG_DIR = Path(__file__).parent.parent / "logs" -LOG_DIR.mkdir(exist_ok=True) - # 日志级别 LOG_LEVEL = settings.LOG_LEVEL.upper() @@ -88,30 +83,6 @@ logger.add( diagnose=True # 显示诊断信息 ) -# # 添加文件处理器 - 常规日志 -# logger.add( -# sink=LOG_DIR / "app.log", -# level="INFO", -# format=FILE_FORMAT, -# rotation="10 MB", # 日志文件大小达到10MB时自动分割 -# retention="7 days", # 保留7天的日志 -# compression="zip", # 归档时压缩为zip -# enqueue=True, # 异步写入 -# serialize=False, # 不使用JSON格式 -# ) -# -# # 添加文件处理器 - 错误日志 -# logger.add( -# sink=LOG_DIR / "error.log", -# level="ERROR", -# format=FILE_FORMAT, -# rotation="10 MB", -# retention="30 days", -# compression="zip", -# enqueue=True, -# serialize=False, -# ) - atexit.register(fluent_sender.close) # 导出配置好的logger diff --git a/config/setting.py b/config/setting.py index c013a85..52454cc 100644 --- a/config/setting.py +++ b/config/setting.py @@ -7,6 +7,7 @@ class Settings(BaseSettings): DB_HOST: str DB_PASSWORD: str ES_HOST: str + FLUENTD_HOST: str class Config: env_file = ".env" # 指定.env文件路径