A plugin that logs important information at each callback point. This plugin helps print all critical events in the console. It is not a replacement of existing logging in ADK. It rather helps terminal based debugging by showing all logs in the console, and serves as a simple demo for every
| 35 | |
| 36 | |
| 37 | class LoggingPlugin(BasePlugin): |
| 38 | """A plugin that logs important information at each callback point. |
| 39 | |
| 40 | This plugin helps print all critical events in the console. It is not a |
| 41 | replacement of existing logging in ADK. It rather helps terminal based |
| 42 | debugging by showing all logs in the console, and serves as a simple demo for |
| 43 | everyone to leverage when developing new plugins. |
| 44 | |
| 45 | This plugin helps users track the invocation status by logging: |
| 46 | - User messages and invocation context |
| 47 | - Agent execution flow |
| 48 | - LLM requests and responses |
| 49 | - Tool calls with arguments and results |
| 50 | - Events and final responses |
| 51 | - Errors during model and tool execution |
| 52 | |
| 53 | Example: |
| 54 | >>> logging_plugin = LoggingPlugin() |
| 55 | >>> runner = Runner( |
| 56 | ... agents=[my_agent], |
| 57 | ... # ... |
| 58 | ... plugins=[logging_plugin], |
| 59 | ... ) |
| 60 | """ |
| 61 | |
| 62 | def __init__(self, name: str = "logging_plugin"): |
| 63 | """Initialize the logging plugin. |
| 64 | |
| 65 | Args: |
| 66 | name: The name of the plugin instance. |
| 67 | """ |
| 68 | super().__init__(name) |
| 69 | |
| 70 | @override |
| 71 | async def on_user_message_callback( |
| 72 | self, |
| 73 | *, |
| 74 | invocation_context: InvocationContext, |
| 75 | user_message: types.Content, |
| 76 | ) -> Optional[types.Content]: |
| 77 | """Log user message and invocation start.""" |
| 78 | self._log(f"🚀 USER MESSAGE RECEIVED") |
| 79 | self._log(f" Invocation ID: {invocation_context.invocation_id}") |
| 80 | self._log(f" Session ID: {invocation_context.session.id}") |
| 81 | self._log(f" User ID: {invocation_context.user_id}") |
| 82 | self._log(f" App Name: {invocation_context.app_name}") |
| 83 | self._log( |
| 84 | " Root Agent:" |
| 85 | f" {invocation_context.agent.name if hasattr(invocation_context.agent, 'name') else 'Unknown'}" |
| 86 | ) |
| 87 | self._log(f" User Content: {self._format_content(user_message)}") |
| 88 | if invocation_context.branch: |
| 89 | self._log(f" Branch: {invocation_context.branch}") |
| 90 | return None |
| 91 | |
| 92 | @override |
| 93 | async def before_run_callback( |
| 94 | self, *, invocation_context: InvocationContext |