mirror of
https://github.com/langgenius/dify-docs.git
synced 2026-03-27 13:28:32 +07:00
59 lines
1.9 KiB
Plaintext
59 lines
1.9 KiB
Plaintext
---
|
|
dimensions:
|
|
type:
|
|
primary: implementation
|
|
detail: standard
|
|
level: intermediate
|
|
standard_title: Debugging logs
|
|
language: en
|
|
description: Learn how to add logging to your Dify plugin tools for development and debugging purposes.
|
|
title: Outputting Logs for Plugin Development and Debugging
|
|
---
|
|
|
|
As a plugin developer, you may want to print arbitrary strings to logs during plugin processing for development or debugging purposes.
|
|
|
|
For this purpose, the plugin SDK implements a handler for Python's standard `logging` library. By using this, you can output any string to both the **standard output during remote debugging** and the **plugin daemon container logs** (community edition only).
|
|
|
|
## Sample
|
|
|
|
Import `plugin_logger_handler` and add it to your logger as a handler. Below is a sample code for a tool plugin.
|
|
|
|
```python
|
|
from collections.abc import Generator
|
|
from typing import Any
|
|
from dify_plugin import Tool
|
|
from dify_plugin.entities.tool import ToolInvokeMessage
|
|
|
|
|
|
# Import logging and custom handler
|
|
import logging
|
|
from dify_plugin.config.logger_format import plugin_logger_handler
|
|
|
|
# Set up logging with the custom handler
|
|
logger = logging.getLogger(__name__)
|
|
logger.setLevel(logging.INFO)
|
|
logger.addHandler(plugin_logger_handler)
|
|
|
|
|
|
class LoggerDemoTool(Tool):
|
|
def _invoke(self, tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessage]:
|
|
|
|
# Log messages with different severity levels
|
|
logger.info("This is a INFO log message.")
|
|
logger.warning("This is a WARNING log message.")
|
|
logger.error("This is a ERROR log message.")
|
|
|
|
yield self.create_text_message("Hello, Dify!")
|
|
```
|
|
|
|
{/*
|
|
Contributing Section
|
|
DO NOT edit this section!
|
|
It will be automatically generated by the script.
|
|
*/}
|
|
|
|
---
|
|
|
|
[Edit this page](https://github.com/langgenius/dify-docs/edit/main/plugin-dev-en/0222-debugging-logs.mdx) | [Report an issue](https://github.com/langgenius/dify-docs/issues/new?template=docs.yml)
|
|
|