Skip to content

Commit d01e07e

Browse files
Aryankbimbajin
andauthored
fix(llm): Merge all logs into one file (#171)
TODO: gradio web-access log still out of control --------- Co-authored-by: imbajin <[email protected]>
1 parent 9a9de77 commit d01e07e

File tree

4 files changed

+81
-22
lines changed

4 files changed

+81
-22
lines changed

hugegraph-llm/src/hugegraph_llm/demo/rag_demo/app.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
# under the License.
1717

1818
import argparse
19-
2019
import gradio as gr
2120
import uvicorn
2221
from fastapi import FastAPI, Depends, APIRouter
@@ -193,7 +192,4 @@ def create_app():
193192
parser.add_argument("--port", type=int, default=8001, help="port")
194193
args = parser.parse_args()
195194

196-
import logging
197-
logging.getLogger("uvicorn.access").propagate = False
198-
199-
uvicorn.run("hugegraph_llm.demo.rag_demo.app:create_app", host=args.host, port=args.port, reload=True)
195+
uvicorn.run("hugegraph_llm.demo.rag_demo.app:create_app", host=args.host, port=args.port, factory=True, reload=True)

hugegraph-llm/src/hugegraph_llm/utils/log.py

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,44 @@
1616
import logging
1717
import os
1818

19-
from pyhugegraph.utils import log
19+
from pyhugegraph.utils.log import init_logger
2020

21-
# TODO: unify the log format in the project (include gradle(fastapi) frame)
22-
# Configure log file path and maximum size
21+
# Configure common settings
2322
LOG_DIR = "logs"
24-
if not os.path.exists(LOG_DIR):
25-
os.makedirs(LOG_DIR)
23+
os.makedirs(LOG_DIR, exist_ok=True)
2624
LOG_FILE = os.path.join(LOG_DIR, "llm-server.log")
25+
INFO = logging.INFO
2726

28-
# Create a logger
29-
log = log.init_logger(log_output=LOG_FILE, log_level=logging.DEBUG, logger_name="rag",
30-
max_log_size=20 * 1024 * 1024)
27+
# Initialize the root logger first with Rich handler
28+
root_logger = init_logger(
29+
log_output=LOG_FILE,
30+
log_level=INFO,
31+
logger_name="root",
32+
propagate_logs=True,
33+
stdout_logging=True
34+
)
35+
36+
# Initialize custom logger
37+
log = init_logger(
38+
log_output=LOG_FILE,
39+
log_level=logging.DEBUG, # Adjust level if needed
40+
logger_name="llm",
41+
)
42+
43+
# Configure Uvicorn (FastAPI) logging
44+
uvicorn_logger = logging.getLogger("uvicorn")
45+
uvicorn_logger.handlers.clear()
46+
uvicorn_logger.handlers.extend(root_logger.handlers)
47+
uvicorn_logger.setLevel(INFO)
48+
49+
# Configure Gradio logging
50+
gradio_logger = logging.getLogger("gradio")
51+
gradio_logger.handlers.clear() # remove default handlers
52+
gradio_logger.handlers.extend(root_logger.handlers)
53+
gradio_logger.setLevel(INFO)
54+
55+
# Suppress `watchfiles` logging
56+
watchfiles_logger = logging.getLogger("watchfiles")
57+
watchfiles_logger.handlers.clear()
58+
watchfiles_logger.handlers.extend(root_logger.handlers)
59+
watchfiles_logger.setLevel(logging.ERROR)

hugegraph-python-client/setup.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import setuptools
1919
from pkg_resources import parse_requirements
2020

21+
# TODO: replace it by poetry/uv configs (e.g. pyproject.toml)
22+
2123
with open("README.md", "r", encoding="utf-8") as fh:
2224
long_description = fh.read()
2325

hugegraph-python-client/src/pyhugegraph/utils/log.py

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,37 @@
2424

2525
from rich.logging import RichHandler
2626

27+
"""
28+
HugeGraph Logger Util
29+
======================
30+
31+
A unified logging module that provides consistent logging functionality across the HugeGraph project.
32+
33+
Key Features:
34+
- Uses "Rich" library for enhanced console output with proper formatting and colors
35+
- Provides both console and file logging capabilities with rotation
36+
- Includes utility functions for controlled logging frequency
37+
38+
Best Practices:
39+
- Other modules should reuse this logger instead of creating new logging configurations
40+
- Use the provided init_logger() function to maintain consistent log formatting
41+
- If additional functionality is needed, extend this module rather than creating new loggers
42+
43+
Example Usage:
44+
from pyhugegraph.utils.log import init_logger
45+
46+
# Initialize logger with both console and file output
47+
log = init_logger(
48+
log_output="logs/myapp.log",
49+
log_level=logging.INFO,
50+
logger_name="myapp"
51+
)
52+
53+
# Use the log/logger
54+
log.info("Application started")
55+
log.debug("Processing data...")
56+
log.error("Error occurred: %s", error_msg)
57+
"""
2758
__all__ = [
2859
"init_logger",
2960
"fetch_log_level",
@@ -38,15 +69,15 @@
3869

3970
@lru_cache() # avoid creating multiple handlers when calling init_logger()
4071
def init_logger(
41-
log_output=None,
42-
log_level=logging.INFO,
43-
rank=0,
44-
*,
45-
logger_name="client", # users should set logger name for modules
46-
propagate_logs: bool = False,
47-
stdout_logging: bool = True,
48-
max_log_size=50 * 1024 * 1024, # 50 MB
49-
backup_logs=5,
72+
log_output=None,
73+
log_level=logging.INFO,
74+
rank=0,
75+
*,
76+
logger_name="client", # users should set logger name for modules
77+
propagate_logs: bool = False,
78+
stdout_logging: bool = True,
79+
max_log_size=50 * 1024 * 1024, # 50 MB
80+
backup_logs=5,
5081
):
5182
"""
5283
Initialize the logger and set its verbosity level to "DEBUG".
@@ -200,4 +231,5 @@ def fetch_log_level(level_name: str):
200231
raise ValueError(f"Invalid log level: {level_name}")
201232
return level
202233

234+
203235
log = init_logger(log_output="logs/output.log", log_level=logging.INFO)

0 commit comments

Comments
 (0)