Files
dify-docs/ja-jp/plugins/best-practice/how-to-print-strings-to-logs-for-debugging.mdx
2025-05-17 04:00:59 +08:00

81 lines
3.8 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: 開発やデバッグのためのログの出力
---
{/*
コントリビューター注:
----------------
これはレガシードキュメントであり、非推奨になります。
このバージョンに変更を加えないでください。
すべての更新は新しいバージョンに向けられるべきです:
/plugin_dev_ja/0222-debugging-logs.ja
*/}
<Card title="このドキュメントはまもなく非推奨になります" icon="circle-exclamation" href="/plugin_dev_ja/0222-debugging-logs.ja">
<p>ドキュメント再編の一環として、このページは段階的に廃止されます。</p>
<p><u><b>このカードをクリックして</b></u>、最新情報が含まれる更新版にリダイレクトしてください。</p>
<p>新しいドキュメントに不一致や改善が必要な箇所を見つけた場合は、ページ下部の「問題を報告」ボタンを使用してください。</p>
</Card>
プラグインの開発者は、開発やデバッグの目的で、プラグインの処理の過程で任意の文字列をログに出力したいと考えることがあるでしょう。
この目的で、プラグインの SDK には、Python の標準ライブラリである `logging` 用のハンドラが実装されています。これを利用すれば、**リモートデバッグ中の標準出力** にも **プラグインデーモンのコンテナログ**(コミュニティ版のみ)にも、任意の文字列を出力できます。
## サンプル
`plugin_logger_handler` をインポートして、ロガーにハンドラとして追加します。以下は、ツールプラグインのサンプルコードです。
```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.
*/}
<CardGroup cols="2">
<Card
title="このページを編集する"
icon="pen-to-square"
href="https://github.com/langgenius/dify-docs-mintlify/edit/main/ja-jp/plugins/best-practice/how-to-print-strings-to-logs-for-debugging.mdx"
>
直接貢献することでドキュメントの改善にご協力ください
</Card>
<Card
title="問題を報告する"
icon="github"
href="https://github.com/langgenius/dify-docs-mintlify/issues/new?title=ドキュメントの問題%3A%20o-print-strings-to-logs-for-debugg&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%2Fja-jp/plugins/best-practice%2Fhow-to-print-strings-to-logs-for-debugging.mdx%0A%0A%23%23%20提案される変更%0A%3C%21--%20特定の変更案がある場合は、ここで説明してください%20--%3E%0A%0A%3C%21--%20ドキュメントの品質向上にご協力いただきありがとうございます%20--%3E"
>
エラーを見つけたり提案がありますか?お知らせください
</Card>
</CardGroup>