diff --git a/.env.example b/.env.example index 8a14cdb3..de9b6452 100644 --- a/.env.example +++ b/.env.example @@ -23,6 +23,9 @@ ### Logging level # LOG_LEVEL=INFO # VERBOSE=False +# LOG_DIR=/path/to/log/directory # Log file directory path, defaults to current working directory +# LOG_MAX_BYTES=10485760 # Log file max size in bytes, defaults to 10MB +# LOG_BACKUP_COUNT=5 # Number of backup files to keep, defaults to 5 ### Max async calls for LLM # MAX_ASYNC=4 diff --git a/gunicorn_config.py b/gunicorn_config.py index 810fc721..a13054e3 100644 --- a/gunicorn_config.py +++ b/gunicorn_config.py @@ -4,8 +4,13 @@ import logging from lightrag.kg.shared_storage import finalize_share_data from lightrag.api.lightrag_server import LightragPathFilter -# 获取日志文件路径 -log_file_path = os.path.abspath(os.path.join(os.getcwd(), "lightrag.log")) +# Get log directory path from environment variable +log_dir = os.getenv("LOG_DIR", os.getcwd()) +log_file_path = os.path.abspath(os.path.join(log_dir, "lightrag.log")) + +# Get log file max size and backup count from environment variables +log_max_bytes = int(os.getenv("LOG_MAX_BYTES", 10485760)) # Default 10MB +log_backup_count = int(os.getenv("LOG_BACKUP_COUNT", 5)) # Default 5 backups # These variables will be set by run_with_gunicorn.py workers = None @@ -25,8 +30,8 @@ timeout = int(os.getenv("TIMEOUT", 120)) keepalive = 5 # Logging configuration -errorlog = os.getenv("ERROR_LOG", log_file_path) # 默认写入到 lightrag.log -accesslog = os.getenv("ACCESS_LOG", log_file_path) # 默认写入到 lightrag.log +errorlog = os.getenv("ERROR_LOG", log_file_path) # Default write to lightrag.log +accesslog = os.getenv("ACCESS_LOG", log_file_path) # Default write to lightrag.log logconfig_dict = { "version": 1, @@ -44,8 +49,8 @@ logconfig_dict = { "class": "logging.handlers.RotatingFileHandler", "formatter": "standard", "filename": log_file_path, - "maxBytes": 10485760, # 10MB - "backupCount": 5, + "maxBytes": log_max_bytes, + "backupCount": log_backup_count, "encoding": "utf8", }, }, diff --git a/lightrag/api/lightrag_server.py b/lightrag/api/lightrag_server.py index d00d39d1..4d0a6390 100644 --- a/lightrag/api/lightrag_server.py +++ b/lightrag/api/lightrag_server.py @@ -430,8 +430,13 @@ def configure_logging(): logger.handlers = [] logger.filters = [] - # Configure basic logging - log_file_path = os.path.abspath(os.path.join(os.getcwd(), "lightrag.log")) + # Get log directory path from environment variable + log_dir = os.getenv("LOG_DIR", os.getcwd()) + log_file_path = os.path.abspath(os.path.join(log_dir, "lightrag.log")) + + # Get log file max size and backup count from environment variables + log_max_bytes = int(os.getenv("LOG_MAX_BYTES", 10485760)) # Default 10MB + log_backup_count = int(os.getenv("LOG_BACKUP_COUNT", 5)) # Default 5 backups logging.config.dictConfig( { @@ -455,8 +460,8 @@ def configure_logging(): "formatter": "detailed", "class": "logging.handlers.RotatingFileHandler", "filename": log_file_path, - "maxBytes": 10 * 1024 * 1024, # 10MB - "backupCount": 5, + "maxBytes": log_max_bytes, + "backupCount": log_backup_count, "encoding": "utf-8", }, },