Add log support for OpenAI demo
This commit is contained in:
@@ -1,11 +1,76 @@
|
|||||||
import os
|
import os
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import logging
|
||||||
|
import logging.config
|
||||||
from lightrag import LightRAG, QueryParam
|
from lightrag import LightRAG, QueryParam
|
||||||
from lightrag.llm.openai import gpt_4o_mini_complete, openai_embed
|
from lightrag.llm.openai import gpt_4o_mini_complete, openai_embed
|
||||||
from lightrag.kg.shared_storage import initialize_pipeline_status
|
from lightrag.kg.shared_storage import initialize_pipeline_status
|
||||||
|
from lightrag.utils import logger, set_verbose_debug
|
||||||
|
|
||||||
WORKING_DIR = "./dickens"
|
WORKING_DIR = "./dickens"
|
||||||
|
|
||||||
|
def configure_logging():
|
||||||
|
"""Configure logging for the application"""
|
||||||
|
|
||||||
|
# Reset any existing handlers to ensure clean configuration
|
||||||
|
for logger_name in ["uvicorn", "uvicorn.access", "uvicorn.error", "lightrag"]:
|
||||||
|
logger_instance = logging.getLogger(logger_name)
|
||||||
|
logger_instance.handlers = []
|
||||||
|
logger_instance.filters = []
|
||||||
|
|
||||||
|
# Get log directory path from environment variable or use current directory
|
||||||
|
log_dir = os.getenv("LOG_DIR", os.getcwd())
|
||||||
|
log_file_path = os.path.abspath(os.path.join(log_dir, "lightrag_demo.log"))
|
||||||
|
|
||||||
|
print(f"\nLightRAG demo log file: {log_file_path}\n")
|
||||||
|
os.makedirs(os.path.dirname(log_dir), exist_ok=True)
|
||||||
|
|
||||||
|
# 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(
|
||||||
|
{
|
||||||
|
"version": 1,
|
||||||
|
"disable_existing_loggers": False,
|
||||||
|
"formatters": {
|
||||||
|
"default": {
|
||||||
|
"format": "%(levelname)s: %(message)s",
|
||||||
|
},
|
||||||
|
"detailed": {
|
||||||
|
"format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"handlers": {
|
||||||
|
"console": {
|
||||||
|
"formatter": "default",
|
||||||
|
"class": "logging.StreamHandler",
|
||||||
|
"stream": "ext://sys.stderr",
|
||||||
|
},
|
||||||
|
"file": {
|
||||||
|
"formatter": "detailed",
|
||||||
|
"class": "logging.handlers.RotatingFileHandler",
|
||||||
|
"filename": log_file_path,
|
||||||
|
"maxBytes": log_max_bytes,
|
||||||
|
"backupCount": log_backup_count,
|
||||||
|
"encoding": "utf-8",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"loggers": {
|
||||||
|
"lightrag": {
|
||||||
|
"handlers": ["console", "file"],
|
||||||
|
"level": "INFO",
|
||||||
|
"propagate": False,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
# Set the logger level to INFO
|
||||||
|
logger.setLevel(logging.INFO)
|
||||||
|
# Enable verbose debug if needed
|
||||||
|
set_verbose_debug(os.getenv("VERBOSE_DEBUG", "false").lower() == "true")
|
||||||
|
|
||||||
if not os.path.exists(WORKING_DIR):
|
if not os.path.exists(WORKING_DIR):
|
||||||
os.mkdir(WORKING_DIR)
|
os.mkdir(WORKING_DIR)
|
||||||
|
|
||||||
@@ -61,4 +126,6 @@ async def main():
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
# Configure logging before running the main function
|
||||||
|
configure_logging()
|
||||||
asyncio.run(main())
|
asyncio.run(main())
|
||||||
|
Reference in New Issue
Block a user