> ## Documentation Index
> Fetch the complete documentation index at: https://dify-6c0370d8-add-new-agent.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 输出插件日志

作为插件开发者，你可能希望在插件处理过程中，为了开发或调试目的，将任意字符串输出到日志中。

为此，插件 SDK 实现了一个适用于 Python 标准库 `logging` 的处理器。通过使用它，你可以将任意字符串输出到**远程调试时的标准输出**以及**插件守护进程的容器日志**（仅社区版）。

## 示例

导入 `plugin_logger_handler` 并将其添加到你的 logger 处理器中。以下是工具插件的示例代码。

```python theme={null}
from collections.abc import Generator
from typing import Any
from dify_plugin import Tool
from dify_plugin.entities.tool import ToolInvokeMessage


# 导入 logging 和自定义处理器
import logging
from dify_plugin.config.logger_format import plugin_logger_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]:

        # 以不同级别输出日志信息
        logger.info("This is an INFO log message.")
        logger.warning("This is a WARNING log message.")
        logger.error("This is an ERROR log message.")

        yield self.create_text_message("Hello, Dify!")
```

***

[编辑此页面](https://github.com/langgenius/dify-docs/edit/main/zh/develop-plugin/features-and-specs/plugin-types/plugin-logging.mdx) | [提交问题](https://github.com/langgenius/dify-docs/issues/new?template=docs.yml)
