diff --git a/.gitignore b/.gitignore index 250d83a..501c0a5 100644 --- a/.gitignore +++ b/.gitignore @@ -25,4 +25,4 @@ /temp -/user_behavior_logs +/analytics diff --git a/log-config.json b/log-config.json new file mode 100644 index 0000000..f45f274 --- /dev/null +++ b/log-config.json @@ -0,0 +1,46 @@ +{ + "version": 1, + "disable_existing_loggers": false, + "formatters": { + "default": { + "()": "uvicorn.logging.DefaultFormatter", + "fmt": "%(levelname)s | %(asctime)s | %(message)s", + "use_colors": null + }, + "access": { + "()": "uvicorn.logging.AccessFormatter", + "fmt": "%(levelname)s | %(asctime)s | %(message)s" + } + }, + "handlers": { + "default": { + "formatter": "default", + "class": "logging.StreamHandler", + "stream": "ext://sys.stderr" + }, + "access": { + "formatter": "access", + "class": "logging.StreamHandler", + "stream": "ext://sys.stdout" + } + }, + "loggers": { + "uvicorn": { + "handlers": [ + "default" + ], + "level": "INFO", + "propagate": false + }, + "uvicorn.error": { + "level": "INFO" + }, + "uvicorn.access": { + "handlers": [ + "access" + ], + "level": "INFO", + "propagate": false + } + } +} diff --git a/main.py b/main.py index d3f83e3..2a66b8b 100644 --- a/main.py +++ b/main.py @@ -25,6 +25,11 @@ os.environ["RETHINK_SERVER_DEBUG"] = "true" if args.debug else "false" if args.password is not None: os.environ["RETHINK_SERVER_PASSWORD"] = args.password + + log_config = uvicorn.config.LOGGING_CONFIG + print(log_config) + log_config["formatters"]["access"]["fmt"] = "%(levelname)s | %(asctime)s | %(message)s" + log_config["formatters"]["default"]["fmt"] = "%(levelname)s | %(asctime)s | %(message)s" uvicorn.run( "retk.application:app", host=args.host, @@ -32,4 +37,5 @@ reload=args.reload, workers=args.workers, env_file=f".env.{args.mode}", + log_config=log_config, ) diff --git a/run.sh b/run.sh index bcec114..a612529 100644 --- a/run.sh +++ b/run.sh @@ -71,4 +71,4 @@ if [ $mode == "local" ]; then fi echo "Running in $mode mode with reload=$reload on $host:$port" # set working directory to retk -uvicorn retk.application:app $reload --host $host --port $port --env-file .env.$mode +uvicorn retk.application:app $reload --log-config log-config.json --host $host --port $port --env-file .env.$mode diff --git a/src/retk/const/settings.py b/src/retk/const/settings.py index 1e3688b..8b91fbd 100644 --- a/src/retk/const/settings.py +++ b/src/retk/const/settings.py @@ -6,7 +6,7 @@ DOT_DATA = ".data" FRONTEND_DIR = RETHINK_DIR / "dist-local" LOCAL_FILE_URL_PRE_DIR = "files" -USER_BEHAVIOR_LOG_DIR = Path(os.path.join(os.getcwd(), "user_behavior_logs")) +ANALYTICS_DIR = Path(os.path.join(os.getcwd(), "analytics")) MAX_USER_BEHAVIOR_LOG_SIZE = 1024 * 1024 * 10 # 10MB MD_MAX_LENGTH = 100_000 REQUEST_ID_MAX_LENGTH = 50 diff --git a/src/retk/core/ai/llm/knowledge/ops.py b/src/retk/core/ai/llm/knowledge/ops.py index d6bc8e7..52cd0cd 100644 --- a/src/retk/core/ai/llm/knowledge/ops.py +++ b/src/retk/core/ai/llm/knowledge/ops.py @@ -49,6 +49,8 @@ async def _batch_send( svr_group = {} for case in cases: if is_extend: + if case.summary_code != const.CodeEnum.OK: + continue service = case.extend_service model = case.extend_model content = case.summary diff --git a/src/retk/core/statistic.py b/src/retk/core/statistic.py index be906b7..8b6df48 100644 --- a/src/retk/core/statistic.py +++ b/src/retk/core/statistic.py @@ -25,16 +25,17 @@ async def add_user_behavior( ): if is_local_db() or aiofiles is None: return - current_log_file = const.settings.USER_BEHAVIOR_LOG_DIR / f"behavior.log" + data_dir = const.settings.ANALYTICS_DIR / "user_behavior" + current_log_file = data_dir / f"behavior.log" lock = asyncio.Lock() if not current_log_file.exists(): - const.settings.USER_BEHAVIOR_LOG_DIR.mkdir(parents=True, exist_ok=True) + data_dir.mkdir(parents=True, exist_ok=True) await __write_new(current_log_file, lock) time_now = datetime.now() # if the file is too large, rename it to current time and create a new one if current_log_file.stat().st_size > const.settings.MAX_USER_BEHAVIOR_LOG_SIZE: - backup_file = const.settings.USER_BEHAVIOR_LOG_DIR / f"{time_now.strftime('%Y%m%d-%H%M%S')}.log" + backup_file = data_dir / f"{time_now.strftime('%Y%m%d-%H%M%S')}.log" current_log_file.rename(backup_file) await __write_new(current_log_file, lock) diff --git a/src/retk/logger.py b/src/retk/logger.py index f77e208..02dcfad 100644 --- a/src/retk/logger.py +++ b/src/retk/logger.py @@ -1,5 +1,4 @@ import logging - import os from logging.handlers import RotatingFileHandler