feat: add LOG_FILE_ENABLED environment variable to control file logging
This commit is contained in:
1 parent
4f174253e6
commit
4206abf474
3 files changed
+36
-8
No files matched your search
+34
-8
@@ -3,14 +3,30 @@
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
import json
|
||||
import traceback
|
||||
from pathlib import Path
|
||||
from loguru import logger
|
||||
|
||||
# 日志目录
|
||||
LOG_DIR = Path(__file__).parent.parent.parent / "logs"
|
||||
LOG_DIR.mkdir(parents=True, exist_ok=True)
|
||||
DEFAULT_LOG_DIR = Path(__file__).parent.parent.parent / "logs"
|
||||
LOG_DIR = Path(os.getenv("LOG_DIR", str(DEFAULT_LOG_DIR)))
|
||||
_LOG_DIR_READY = False
|
||||
|
||||
|
||||
def _prepare_log_dir() -> bool:
|
||||
"""确保日志目录可用"""
|
||||
global LOG_DIR, _LOG_DIR_READY
|
||||
if _LOG_DIR_READY:
|
||||
return True
|
||||
try:
|
||||
LOG_DIR.mkdir(parents=True, exist_ok=True)
|
||||
_LOG_DIR_READY = True
|
||||
return True
|
||||
except Exception:
|
||||
_LOG_DIR_READY = False
|
||||
return False
|
||||
|
||||
|
||||
def _format_json(record) -> str:
|
||||
@@ -52,6 +68,12 @@ def _format_json(record) -> str:
|
||||
|
||||
return json.dumps(log_entry, ensure_ascii=False)
|
||||
|
||||
def _env_flag(name: str, default: bool) -> bool:
|
||||
raw = os.getenv(name)
|
||||
if raw is None:
|
||||
return default
|
||||
return raw.strip().lower() in ("1", "true", "yes", "on", "y")
|
||||
|
||||
|
||||
def _make_json_sink(output):
|
||||
"""创建 JSON sink"""
|
||||
@@ -79,6 +101,7 @@ def setup_logging(
|
||||
):
|
||||
"""设置日志配置"""
|
||||
logger.remove()
|
||||
file_logging = _env_flag("LOG_FILE_ENABLED", file_logging)
|
||||
|
||||
# 控制台输出
|
||||
if json_console:
|
||||
@@ -98,12 +121,15 @@ def setup_logging(
|
||||
|
||||
# 文件输出
|
||||
if file_logging:
|
||||
logger.add(
|
||||
_file_json_sink,
|
||||
level=level,
|
||||
format="{message}",
|
||||
enqueue=True,
|
||||
)
|
||||
if _prepare_log_dir():
|
||||
logger.add(
|
||||
_file_json_sink,
|
||||
level=level,
|
||||
format="{message}",
|
||||
enqueue=True,
|
||||
)
|
||||
else:
|
||||
logger.warning("File logging disabled: no writable log directory.")
|
||||
|
||||
return logger
|
||||
|
||||
|
||||
@@ -49,6 +49,7 @@ Default password: `grok2api` (config key `app.app_key`, change it in production)
|
||||
| Variable | Description | Default | Example |
|
||||
| :--- | :--- | :--- | :--- |
|
||||
| `LOG_LEVEL` | Log level | `INFO` | `DEBUG` |
|
||||
| `LOG_FILE_ENABLED` | Enable file logging | `true` | `false` |
|
||||
| `SERVER_HOST` | Bind address | `0.0.0.0` | `0.0.0.0` |
|
||||
| `SERVER_PORT` | Service port | `8000` | `8000` |
|
||||
| `SERVER_WORKERS` | Uvicorn worker count | `1` | `2` |
|
||||
|
||||
@@ -60,6 +60,7 @@ docker compose up -d
|
||||
| 变量名 | 说明 | 默认值 | 示例 |
|
||||
| :---------------------- | :-------------------------------------------------- | :---------- | :-------------------------------------------------- |
|
||||
| `LOG_LEVEL` | 日志级别 | `INFO` | `DEBUG` |
|
||||
| `LOG_FILE_ENABLED` | 是否启用文件日志 | `true` | `false` |
|
||||
| `SERVER_HOST` | 服务监听地址 | `0.0.0.0` | `0.0.0.0` |
|
||||
| `SERVER_PORT` | 服务端口 | `8000` | `8000` |
|
||||
| `SERVER_WORKERS` | Uvicorn worker 数量 | `1` | `2` |
|
||||
|
||||
Reference in new issue
Block a user