Custom Logging

Example showing how to use a custom logger with Kern.

1"""
2Custom Logging
3=============================
4
5Example showing how to use a custom logger with Kern.
6"""
7
8import logging
9
10from kern.agent import Agent
11from kern.utils.log import configure_agno_logging, log_info
12
13
14def get_custom_logger():
15 """Return an example custom logger."""
16 custom_logger = logging.getLogger("custom_logger")
17 handler = logging.StreamHandler()
18 formatter = logging.Formatter("[CUSTOM_LOGGER] %(levelname)s: %(message)s")
19 handler.setFormatter(formatter)
20 custom_logger.addHandler(handler)
21 custom_logger.setLevel(logging.INFO) # Set level to INFO to show info messages
22 custom_logger.propagate = False
23 return custom_logger
24
25
26# Get the custom logger we will use for the example.
27custom_logger = get_custom_logger()
28
29# Configure Kern to use our custom logger. It will be used for all logging.
30configure_agno_logging(custom_default_logger=custom_logger)
31
32# Every use of the logging function in kern.utils.log will now use our custom logger.
33log_info("This is using our custom logger!")
34
35# Now let's setup an Agent and run it.
36# All logging coming from the Agent will use our custom logger.
37# ---------------------------------------------------------------------------
38# Create Agent
39# ---------------------------------------------------------------------------
40agent = Agent()
41
42# ---------------------------------------------------------------------------
43# Run Agent
44# ---------------------------------------------------------------------------
45if __name__ == "__main__":
46 agent.print_response("What can I do to improve my sleep?")

Run the Example

1# Clone and setup repo
2git clone https://github.com/kern-ai/kern.git
3cd kern/cookbook/02_agents/14_advanced
4
5# Create and activate virtual environment
6./scripts/demo_setup.sh
7source .venvs/demo/bin/activate
8
9python custom_logging.py