Skip to content

Commit

Permalink
feat(app): add logging config, change data log dir
Browse files Browse the repository at this point in the history
  • Loading branch information
MorvanZhou committed Aug 29, 2024
1 parent c6a019a commit 42b79e2
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@


/temp
/user_behavior_logs
/analytics
46 changes: 46 additions & 0 deletions log-config.json
Original file line number Diff line number Diff line change
@@ -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
}
}
}
6 changes: 6 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,17 @@
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,
port=args.port,
reload=args.reload,
workers=args.workers,
env_file=f".env.{args.mode}",
log_config=log_config,
)
2 changes: 1 addition & 1 deletion run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/retk/const/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/retk/core/ai/llm/knowledge/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions src/retk/core/statistic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
1 change: 0 additions & 1 deletion src/retk/logger.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging

import os
from logging.handlers import RotatingFileHandler

Expand Down

0 comments on commit 42b79e2

Please sign in to comment.