This is an automated email from the ASF dual-hosted git repository. jin pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/incubator-hugegraph-ai.git
The following commit(s) were added to refs/heads/main by this push: new d01e07e fix(llm): Merge all logs into one file (#171) d01e07e is described below commit d01e07e160247d92c905422766a2e28376172057 Author: Aryan Kumar Baghel <118632488+arya...@users.noreply.github.com> AuthorDate: Mon Feb 24 11:40:40 2025 +0530 fix(llm): Merge all logs into one file (#171) TODO: gradio web-access log still out of control --------- Co-authored-by: imbajin <j...@apache.org> --- .../src/hugegraph_llm/demo/rag_demo/app.py | 6 +-- hugegraph-llm/src/hugegraph_llm/utils/log.py | 45 +++++++++++++++---- hugegraph-python-client/setup.py | 2 + .../src/pyhugegraph/utils/log.py | 50 ++++++++++++++++++---- 4 files changed, 81 insertions(+), 22 deletions(-) diff --git a/hugegraph-llm/src/hugegraph_llm/demo/rag_demo/app.py b/hugegraph-llm/src/hugegraph_llm/demo/rag_demo/app.py index fc1646b..0fc6947 100644 --- a/hugegraph-llm/src/hugegraph_llm/demo/rag_demo/app.py +++ b/hugegraph-llm/src/hugegraph_llm/demo/rag_demo/app.py @@ -16,7 +16,6 @@ # under the License. import argparse - import gradio as gr import uvicorn from fastapi import FastAPI, Depends, APIRouter @@ -193,7 +192,4 @@ if __name__ == "__main__": parser.add_argument("--port", type=int, default=8001, help="port") args = parser.parse_args() - import logging - logging.getLogger("uvicorn.access").propagate = False - - uvicorn.run("hugegraph_llm.demo.rag_demo.app:create_app", host=args.host, port=args.port, reload=True) + uvicorn.run("hugegraph_llm.demo.rag_demo.app:create_app", host=args.host, port=args.port, factory=True, reload=True) diff --git a/hugegraph-llm/src/hugegraph_llm/utils/log.py b/hugegraph-llm/src/hugegraph_llm/utils/log.py index 0a18673..f1116f7 100755 --- a/hugegraph-llm/src/hugegraph_llm/utils/log.py +++ b/hugegraph-llm/src/hugegraph_llm/utils/log.py @@ -16,15 +16,44 @@ import logging import os -from pyhugegraph.utils import log +from pyhugegraph.utils.log import init_logger -# TODO: unify the log format in the project (include gradle(fastapi) frame) -# Configure log file path and maximum size +# Configure common settings LOG_DIR = "logs" -if not os.path.exists(LOG_DIR): - os.makedirs(LOG_DIR) +os.makedirs(LOG_DIR, exist_ok=True) LOG_FILE = os.path.join(LOG_DIR, "llm-server.log") +INFO = logging.INFO -# Create a logger -log = log.init_logger(log_output=LOG_FILE, log_level=logging.DEBUG, logger_name="rag", - max_log_size=20 * 1024 * 1024) +# Initialize the root logger first with Rich handler +root_logger = init_logger( + log_output=LOG_FILE, + log_level=INFO, + logger_name="root", + propagate_logs=True, + stdout_logging=True +) + +# Initialize custom logger +log = init_logger( + log_output=LOG_FILE, + log_level=logging.DEBUG, # Adjust level if needed + logger_name="llm", +) + +# Configure Uvicorn (FastAPI) logging +uvicorn_logger = logging.getLogger("uvicorn") +uvicorn_logger.handlers.clear() +uvicorn_logger.handlers.extend(root_logger.handlers) +uvicorn_logger.setLevel(INFO) + +# Configure Gradio logging +gradio_logger = logging.getLogger("gradio") +gradio_logger.handlers.clear() # remove default handlers +gradio_logger.handlers.extend(root_logger.handlers) +gradio_logger.setLevel(INFO) + +# Suppress `watchfiles` logging +watchfiles_logger = logging.getLogger("watchfiles") +watchfiles_logger.handlers.clear() +watchfiles_logger.handlers.extend(root_logger.handlers) +watchfiles_logger.setLevel(logging.ERROR) diff --git a/hugegraph-python-client/setup.py b/hugegraph-python-client/setup.py index 158e734..65ddab5 100644 --- a/hugegraph-python-client/setup.py +++ b/hugegraph-python-client/setup.py @@ -18,6 +18,8 @@ import setuptools from pkg_resources import parse_requirements +# TODO: replace it by poetry/uv configs (e.g. pyproject.toml) + with open("README.md", "r", encoding="utf-8") as fh: long_description = fh.read() diff --git a/hugegraph-python-client/src/pyhugegraph/utils/log.py b/hugegraph-python-client/src/pyhugegraph/utils/log.py index 7ccd794..b263d32 100644 --- a/hugegraph-python-client/src/pyhugegraph/utils/log.py +++ b/hugegraph-python-client/src/pyhugegraph/utils/log.py @@ -24,6 +24,37 @@ from logging.handlers import RotatingFileHandler from rich.logging import RichHandler +""" +HugeGraph Logger Util +====================== + +A unified logging module that provides consistent logging functionality across the HugeGraph project. + +Key Features: +- Uses "Rich" library for enhanced console output with proper formatting and colors +- Provides both console and file logging capabilities with rotation +- Includes utility functions for controlled logging frequency + +Best Practices: +- Other modules should reuse this logger instead of creating new logging configurations +- Use the provided init_logger() function to maintain consistent log formatting +- If additional functionality is needed, extend this module rather than creating new loggers + +Example Usage: + from pyhugegraph.utils.log import init_logger + + # Initialize logger with both console and file output + log = init_logger( + log_output="logs/myapp.log", + log_level=logging.INFO, + logger_name="myapp" + ) + + # Use the log/logger + log.info("Application started") + log.debug("Processing data...") + log.error("Error occurred: %s", error_msg) +""" __all__ = [ "init_logger", "fetch_log_level", @@ -38,15 +69,15 @@ DEFAULT_BUFFER_SIZE: int = 1024 * 1024 # 1MB @lru_cache() # avoid creating multiple handlers when calling init_logger() def init_logger( - log_output=None, - log_level=logging.INFO, - rank=0, - *, - logger_name="client", # users should set logger name for modules - propagate_logs: bool = False, - stdout_logging: bool = True, - max_log_size=50 * 1024 * 1024, # 50 MB - backup_logs=5, + log_output=None, + log_level=logging.INFO, + rank=0, + *, + logger_name="client", # users should set logger name for modules + propagate_logs: bool = False, + stdout_logging: bool = True, + max_log_size=50 * 1024 * 1024, # 50 MB + backup_logs=5, ): """ Initialize the logger and set its verbosity level to "DEBUG". @@ -200,4 +231,5 @@ def fetch_log_level(level_name: str): raise ValueError(f"Invalid log level: {level_name}") return level + log = init_logger(log_output="logs/output.log", log_level=logging.INFO)