simplify logging utils and refactor

This commit is contained in:
vasilije 2025-08-18 22:14:12 +02:00
parent fb511310e3
commit 9017b634cd
9 changed files with 124 additions and 54 deletions

View file

@ -106,16 +106,16 @@ def _create_parser() -> tuple[argparse.ArgumentParser, Dict[str, SupportsCliComm
for command_class in command_classes:
command = command_class()
if command.command in installed_commands:
if command.command_string in installed_commands:
continue
command_parser = subparsers.add_parser(
command.command,
command.command_string,
help=command.help_string,
description=command.description if hasattr(command, "description") else None,
)
command.configure_parser(command_parser)
installed_commands[command.command] = command
installed_commands[command.command_string] = command
# Add rich formatting if available
if HAS_RICH:

View file

@ -9,7 +9,7 @@ from cognee.cli.exceptions import CliCommandException, CliCommandInnerException
class AddCommand(SupportsCliCommand):
command = "add"
command_string = "add"
help_string = "Add data to Cognee for knowledge graph processing"
docs_url = DEFAULT_DOCS_URL
description = """

View file

@ -10,7 +10,7 @@ from cognee.cli.exceptions import CliCommandException, CliCommandInnerException
class CognifyCommand(SupportsCliCommand):
command = "cognify"
command_string = "cognify"
help_string = "Transform ingested data into a structured knowledge graph"
docs_url = DEFAULT_DOCS_URL
description = """

View file

@ -9,7 +9,7 @@ from cognee.cli.exceptions import CliCommandException, CliCommandInnerException
class ConfigCommand(SupportsCliCommand):
command = "config"
command_string = "config"
help_string = "Manage cognee configuration settings"
docs_url = DEFAULT_DOCS_URL
description = """
@ -19,7 +19,8 @@ You can:
- View all current configuration settings
- Get specific configuration values
- Set configuration values
- Reset configuration to defaults
- Unset (reset to default) specific configuration values
- Reset all configuration to defaults
Configuration changes will affect how cognee processes and stores data.
"""
@ -41,6 +42,13 @@ Configuration changes will affect how cognee processes and stores data.
# List command
subparsers.add_parser("list", help="List all configuration keys")
# Unset command
unset_parser = subparsers.add_parser("unset", help="Remove/unset a configuration value")
unset_parser.add_argument("key", help="Configuration key to unset")
unset_parser.add_argument(
"--force", "-f", action="store_true", help="Skip confirmation prompt"
)
# Reset command
reset_parser = subparsers.add_parser("reset", help="Reset configuration to defaults")
reset_parser.add_argument(
@ -53,13 +61,15 @@ Configuration changes will affect how cognee processes and stores data.
import cognee
if not hasattr(args, "config_action") or args.config_action is None:
fmt.error("Please specify a config action: get, set, list, or reset")
fmt.error("Please specify a config action: get, set, unset, list, or reset")
return
if args.config_action == "get":
self._handle_get(args)
elif args.config_action == "set":
self._handle_set(args)
elif args.config_action == "unset":
self._handle_unset(args)
elif args.config_action == "list":
self._handle_list(args)
elif args.config_action == "reset":
@ -79,23 +89,32 @@ Configuration changes will affect how cognee processes and stores data.
if args.key:
# Get specific key
try:
value = cognee.config.get(args.key)
fmt.echo(f"{args.key}: {value}")
if hasattr(cognee.config, "get"):
value = cognee.config.get(args.key)
fmt.echo(f"{args.key}: {value}")
else:
fmt.error("Configuration retrieval not implemented yet")
fmt.note("The config system currently only supports setting values, not retrieving them")
fmt.note(f"To set this value: 'cognee config set {args.key} <value>'")
except Exception:
fmt.error(f"Configuration key '{args.key}' not found")
fmt.error(f"Configuration key '{args.key}' not found or retrieval failed")
else:
# Get all configuration
try:
config_dict = (
cognee.config.get_all() if hasattr(cognee.config, "get_all") else {}
)
if config_dict:
fmt.echo("Current configuration:")
for key, value in config_dict.items():
fmt.echo(f" {key}: {value}")
if hasattr(cognee.config, "get_all"):
config_dict = cognee.config.get_all()
if config_dict:
fmt.echo("Current configuration:")
for key, value in config_dict.items():
fmt.echo(f" {key}: {value}")
else:
fmt.echo("No configuration settings found")
else:
fmt.echo("No configuration settings found")
fmt.error("Configuration viewing not implemented yet")
fmt.note("The config system currently only supports setting values, not retrieving them")
fmt.note("Available commands: 'cognee config set <key> <value>'")
except Exception:
fmt.error("Failed to retrieve configuration")
fmt.note("Configuration viewing not fully implemented yet")
except Exception as e:
@ -120,16 +139,72 @@ Configuration changes will affect how cognee processes and stores data.
except Exception as e:
raise CliCommandInnerException(f"Failed to set configuration: {str(e)}")
def _handle_unset(self, args: argparse.Namespace) -> None:
try:
import cognee
# Confirm unset unless forced
if not args.force:
if not fmt.confirm(f"Unset configuration key '{args.key}'?"):
fmt.echo("Unset cancelled.")
return
# Since the config system doesn't have explicit unset methods,
# we need to map config keys to their reset/default behaviors
config_key_mappings = {
# LLM configuration
"llm_provider": ("set_llm_provider", "openai"),
"llm_model": ("set_llm_model", "gpt-5-mini"),
"llm_api_key": ("set_llm_api_key", ""),
"llm_endpoint": ("set_llm_endpoint", ""),
# Database configuration
"graph_database_provider": ("set_graph_database_provider", "kuzu"),
"vector_db_provider": ("set_vector_db_provider", "lancedb"),
"vector_db_url": ("set_vector_db_url", ""),
"vector_db_key": ("set_vector_db_key", ""),
# Chunking configuration
"chunk_size": ("set_chunk_size", 1500),
"chunk_overlap": ("set_chunk_overlap", 10),
}
if args.key in config_key_mappings:
method_name, default_value = config_key_mappings[args.key]
try:
# Get the method and call it with the default value
method = getattr(cognee.config, method_name)
method(default_value)
fmt.success(f"Unset {args.key} (reset to default: {default_value})")
except AttributeError:
fmt.error(f"Configuration method '{method_name}' not found")
except Exception as e:
fmt.error(f"Failed to unset '{args.key}': {str(e)}")
else:
fmt.error(f"Unknown configuration key '{args.key}'")
fmt.note("Available keys: " + ", ".join(config_key_mappings.keys()))
fmt.note("Use 'cognee config list' to see all available configuration options")
except Exception as e:
raise CliCommandInnerException(f"Failed to unset configuration: {str(e)}")
def _handle_list(self, args: argparse.Namespace) -> None:
try:
import cognee
# This would need to be implemented in cognee.config
fmt.note("Available configuration keys:")
fmt.echo(" LLM_MODEL")
fmt.echo(" VECTOR_DB_URL")
fmt.echo(" GRAPH_DB_URL")
fmt.echo(" (Use 'cognee config get' to see current values)")
fmt.echo(" llm_provider, llm_model, llm_api_key, llm_endpoint")
fmt.echo(" graph_database_provider, vector_db_provider")
fmt.echo(" vector_db_url, vector_db_key")
fmt.echo(" chunk_size, chunk_overlap")
fmt.echo("")
fmt.echo("Commands:")
fmt.echo(" cognee config get [key] - View configuration")
fmt.echo(" cognee config set <key> <value> - Set configuration")
fmt.echo(" cognee config unset <key> - Reset key to default")
fmt.echo(" cognee config reset - Reset all to defaults")
except Exception as e:
raise CliCommandInnerException(f"Failed to list configuration: {str(e)}")

View file

@ -9,7 +9,7 @@ from cognee.cli.exceptions import CliCommandException, CliCommandInnerException
class DeleteCommand(SupportsCliCommand):
command = "delete"
command_string = "delete"
help_string = "Delete data from cognee knowledge base"
docs_url = DEFAULT_DOCS_URL
description = """

View file

@ -11,7 +11,7 @@ from cognee.cli.exceptions import CliCommandException, CliCommandInnerException
class SearchCommand(SupportsCliCommand):
command = "search"
command_string = "search"
help_string = "Search and query the knowledge graph for insights, information, and connections"
docs_url = DEFAULT_DOCS_URL
description = """

View file

@ -10,6 +10,7 @@ from typing import Any, Sequence
# CRITICAL: Prevent verbose logging initialization for CLI-only usage
# This must be set before any cognee imports to be effective
os.environ["COGNEE_MINIMAL_LOGGING"] = "true"
os.environ["COGNEE_CLI_MODE"] = "true"
def get_version() -> str:

View file

@ -1,3 +1,4 @@
from abc import abstractmethod
from typing import Protocol, Optional
import argparse
@ -5,7 +6,7 @@ import argparse
class SupportsCliCommand(Protocol):
"""Protocol for defining one cognee cli command"""
command: str
command_string: str
"""name of the command"""
help_string: str
"""the help string for argparse"""
@ -14,10 +15,12 @@ class SupportsCliCommand(Protocol):
docs_url: Optional[str]
"""the default docs url to be printed in case of an exception"""
@abstractmethod
def configure_parser(self, parser: argparse.ArgumentParser) -> None:
"""Configures the parser for the given argument"""
...
@abstractmethod
def execute(self, args: argparse.Namespace) -> None:
"""Executes the command with the given arguments"""
...

View file

@ -173,29 +173,16 @@ def log_database_configuration(logger):
from cognee.infrastructure.databases.graph.config import get_graph_config
try:
# Log relational database configuration
relational_config = get_relational_config()
if relational_config.db_provider == "postgres":
logger.info(f"Postgres host: {relational_config.db_host}:{relational_config.db_port}")
elif relational_config.db_provider == "sqlite":
logger.info(f"SQLite path: {relational_config.db_path}")
# Log vector database configuration
vector_config = get_vectordb_config()
if vector_config.vector_db_provider == "lancedb":
logger.info(f"Vector database path: {vector_config.vector_db_url}")
else:
logger.info(f"Vector database URL: {vector_config.vector_db_url}")
# Log graph database configuration
graph_config = get_graph_config()
if graph_config.graph_database_provider == "kuzu":
logger.info(f"Graph database path: {graph_config.graph_file_path}")
else:
logger.info(f"Graph database URL: {graph_config.graph_database_url}")
# Get base database directory path
from cognee.base_config import get_base_config
base_config = get_base_config()
databases_path = os.path.join(base_config.system_root_directory, "databases")
# Log concise database info
logger.info(f"Database storage: {databases_path}")
except Exception as e:
logger.warning(f"Could not retrieve database configuration: {str(e)}")
logger.debug(f"Could not retrieve database configuration: {str(e)}")
def cleanup_old_logs(logs_dir, max_files):
@ -312,10 +299,6 @@ def setup_logging(log_level=None, name=None):
# Hand back to the original hook → prints traceback and exits
sys.__excepthook__(exc_type, exc_value, traceback)
logger.info("Want to learn more? Visit the Cognee documentation: https://docs.cognee.ai")
logger.info(
"Need help? Reach out to us on our Discord server: https://discord.gg/NQPKmU5CCg"
)
# Install exception handlers
sys.excepthook = handle_exception
@ -399,8 +382,16 @@ def setup_logging(log_level=None, name=None):
# Provide compact logging for CLI mode, detailed for regular mode
if os.getenv("COGNEE_CLI_MODE") == "true":
# Compact initialization for CLI
logger.info(f"cognee {COGNEE_VERSION} initialized")
# Compact initialization for CLI - just basic info
logger.info(f"cognee {COGNEE_VERSION} ready")
# Log basic database path only
try:
from cognee.base_config import get_base_config
base_config = get_base_config()
databases_path = os.path.join(base_config.system_root_directory, "databases")
logger.info(f"Storage: {databases_path}")
except Exception:
pass # Silent fail for CLI mode
else:
# Detailed initialization for regular usage
@ -412,7 +403,7 @@ def setup_logging(log_level=None, name=None):
os_info=OS_INFO,
)
logger.info("Want to learn more? Visit the Cognee documentation: https://docs.cognee.ai")
# Log database configuration
log_database_configuration(logger)