Custom Logging
Configure custom loggers and formatters for your Kern setup.
You can provide your own logging configuration to Kern, to be used instead of the default ones.
This can be useful if you need your system to log in any specific format.
Specifying a custom logging configuration
You can configure Kern to use your own logging configuration by using the configure_agno_logging function.
1import logging23from kern.agent import Agent4from kern.utils.log import configure_agno_logging, log_info56# Set up a custom logger7custom_logger = logging.getLogger("custom_logger")8handler = logging.StreamHandler()9formatter = logging.Formatter("[CUSTOM_LOGGER] %(levelname)s: %(message)s")10handler.setFormatter(formatter)11custom_logger.addHandler(handler)12custom_logger.setLevel(logging.INFO)13custom_logger.propagate = False1415# Configure Kern to use the custom logger16configure_agno_logging(custom_default_logger=custom_logger)1718# All logging will now use the custom logger19log_info("This is using our custom logger!")2021agent = Agent()22agent.print_response("What is 2+2?")Logging to a File
You can configure Kern to log to a file instead of the console:
1import logging2from pathlib import Path34from kern.agent import Agent5from kern.utils.log import configure_agno_logging, log_info67# Create a custom logger that writes to a file8custom_logger = logging.getLogger("file_logger")910# Ensure tmp directory exists11log_file_path = Path("tmp/log.txt")12log_file_path.parent.mkdir(parents=True, exist_ok=True)1314# Use FileHandler to write to file15handler = logging.FileHandler(log_file_path)16formatter = logging.Formatter("%(levelname)s: %(message)s")17handler.setFormatter(formatter)18custom_logger.addHandler(handler)19custom_logger.setLevel(logging.INFO)20custom_logger.propagate = False2122# Configure Kern to use the file logger23configure_agno_logging(custom_default_logger=custom_logger)2425# All logs will be written to tmp/log.txt26log_info("This is using our file logger!")2728agent = Agent()29agent.print_response("Tell me a fun fact")Multiple Loggers
You can configure different loggers for your Agents, Teams and Workflows:
1import logging23from kern.agent import Agent4from kern.team import Team5from kern.workflow import Workflow6from kern.workflow.step import Step7from kern.utils.log import configure_agno_logging, log_info89# Create custom loggers for different components10custom_agent_logger = logging.getLogger("agent_logger")11custom_team_logger = logging.getLogger("team_logger")12custom_workflow_logger = logging.getLogger("workflow_logger")1314# Configure handlers and formatters for each15for logger in [custom_agent_logger, custom_team_logger, custom_workflow_logger]:16 handler = logging.StreamHandler()17 handler.setFormatter(logging.Formatter("[%(name)s] %(levelname)s: %(message)s"))18 logger.addHandler(handler)19 logger.setLevel(logging.INFO)20 logger.propagate = False2122# Workflow logs at DEBUG level when debug_mode is enabled23# Set workflow logger to DEBUG to see these logs24custom_workflow_logger.setLevel(logging.DEBUG)2526# Apply the configuration27configure_agno_logging(28 custom_default_logger=custom_agent_logger,29 custom_agent_logger=custom_agent_logger,30 custom_team_logger=custom_team_logger,31 custom_workflow_logger=custom_workflow_logger,32)3334# All logging will now use the custom agent logger by default35log_info("Using custom loggers!")3637# Create agent and team38agent = Agent()39team = Team(members=[agent])4041# Agent will use custom_agent_logger42agent.print_response("What is 2+2?")4344# Team will use custom_team_logger45team.print_response("Tell me a short joke")4647# Workflow will use custom_workflow_logger48workflow = Workflow(49 debug_mode=True,50 steps=[Step(name="step1", agent=agent)]51)52workflow.print_response("Tell me a fun fact")Using Named Loggers
As it's conventional in Python, you can also provide custom loggers just by setting loggers with specific names. This is useful if you want to set them up using configuration files.
Kern automatically recognizes and uses these logger names:
kernwill be used for all Agent logskern-teamwill be used for all Team logskern-workflowwill be used for all Workflow logs
1import logging2from kern.agent import Agent3from kern.team import Team4from kern.workflow import Workflow5from kern.workflow.step import Step67# Set up named loggers BEFORE creating agents/teams/workflows8logger_configs = [9 ("kern", "agent.log"),10 ("kern-team", "team.log"),11 ("kern-workflow", "workflow.log"),12]1314for logger_name, log_file in logger_configs:15 logger = logging.getLogger(logger_name)16 logger.setLevel(logging.INFO)17 handler = logging.FileHandler(log_file)18 handler.setFormatter(logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s"))19 logger.addHandler(handler)20 logger.propagate = False2122# Kern will automatically detect and use these loggers23agent = Agent()24agent.print_response("Hello from agent!") # Agent logs will go to agent.log2526team = Team(members=[agent])27team.print_response("Hello from team!") # Team logs will go to team.log2829# Workflow requires debug mode to use the workflow logger30workflow = Workflow(31 debug_mode=True,32 steps=[Step(name="step1", agent=agent)]33)34workflow.run("Hello from workflow!") # Workflow logs will go to workflow.log