mirror of
https://github.com/langgenius/dify-docs.git
synced 2026-03-27 13:28:32 +07:00
71 lines
2.6 KiB
Plaintext
71 lines
2.6 KiB
Plaintext
---
|
||
dimensions:
|
||
type:
|
||
primary: implementation
|
||
detail: standard
|
||
level: intermediate
|
||
standard_title: Debugging logs
|
||
language: zh
|
||
description: 介绍如何在 Dify 插件开发中为你的工具添加日志输出功能,以便于开发和调试。
|
||
title: 输出开发和调试插件过程中的日志
|
||
---
|
||
|
||
作为插件开发者,你可能希望在插件处理过程中,为了开发或调试目的,将任意字符串输出到日志中。
|
||
|
||
为此,插件 SDK 实现了一个适用于 Python 标准库 `logging` 的处理器。通过使用它,你可以将任意字符串输出到**远程调试时的标准输出**以及**插件守护进程的容器日志**(仅社区版)。
|
||
|
||
## 示例
|
||
|
||
导入 `plugin_logger_handler` 并将其添加到你的 logger 处理器中。以下是工具插件的示例代码。
|
||
|
||
```python
|
||
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 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.
|
||
*/}
|
||
|
||
<CardGroup cols="2">
|
||
<Card
|
||
title="编辑此页面"
|
||
icon="pen-to-square"
|
||
href="https://github.com/langgenius/dify-docs-mintlify/edit/main/plugin_dev_zh/0222-debugging-logs.zh.mdx"
|
||
>
|
||
通过直接提交修改来帮助改进文档内容
|
||
</Card>
|
||
<Card
|
||
title="提交问题"
|
||
icon="github"
|
||
href="https://github.com/langgenius/dify-docs-mintlify/issues/new?title=文档问题%3A%20debugging-logs&body=%23%23%20问题描述%0A%3C%21--%20请简要描述您发现的问题%20--%3E%0A%0A%23%23%20页面链接%0Ahttps%3A%2F%2Fgithub.com%2Flanggenius%2Fdify-docs-mintlify%2Fblob%2Fmain%2Fplugin_dev_zh%2F0222-debugging-logs.zh.mdx%0A%0A%23%23%20建议修改%0A%3C%21--%20如果有具体的修改建议,请在此说明%20--%3E%0A%0A%3C%21--%20感谢您对文档质量的关注!%20--%3E"
|
||
>
|
||
发现错误或有改进建议?请提交问题反馈
|
||
</Card>
|
||
</CardGroup>
|