---
schema_version: "1"
id: 55470dc29bcf39439707446f64b5d3bd71e1403d96bd1a67a27d25e2300937ba
title: MDC ログ
collection: mulesoft
version: current
locale: ja-jp
portal_url: https://docs.salesforce.com/mulesoft/current/ja-jp/mule-runtime/4.4/logging-mdc.md
canonical_url: https://docs.mulesoft.com/jp/mule-runtime/4.4/logging-mdc
source_site: docs.mulesoft.com
content_type: documentation
content_hash: de8eca2cbb02d415f6bf7165ee3554f1298bd9dd7e3e78f3a198410fe6623859
description: MuleSoft Documentation Site
release: "4.4"
latest: false
---
# MDC ログ

Mapped Diagnostic Context (MDC) では、より多くのコンテキストまたは情報が現在の Mule イベントのログに提供されるため、ログが強化され、追跡が改善されます。

デフォルトでは、Mule では、現在のイベントの場所を表示する ​`processor`​ と、イベントの相関 ID を表示する ​`event`​ の 2 つの MDC エントリが記録されます。

Mule Tracing Module を使用すると、特定の Mule イベントのログコンテキストの変数を追加、削除、クリアすることでログを強化できます。このログコンテキストは、対応するイベントの実行全体にわたって存在します。

## [](#前提条件)前提条件

MDC ログ操作を使用するには、次のタスクを完了します。

- アプリケーションに Mule Tracing Module をインストールします。
- `log4j2.xml`​ ファイル内のパターンレイアウトを ​`MDC`​ に変更します。

<table><tbody><tr><td class="icon"><i class="fa icon-note" title="Note"></i></td><td class="content">Anypoint Runtime Fabric または CloudHub 2.0 を使用して Mule アプリケーションをデプロイしている場合、MDC ログはサポートされません。</td></tr></tbody></table>

## [](#mule-tracing-module-のインストール)Mule Tracing Module のインストール

次の手順に従って、アプリケーションに Mule Tracing Module をインストールします。

1. Anypoint Studio で Mule プロジェクトを開きます。
2. \[Mule Palette (Mule パレット)\] に移動します。
3. **\[Search in Exchange (Exchange で検索)\]**​ を選択して、Mule Tracing Module を検索します。
4. モジュールを選択し、​**\[Add (追加)\]**​ をクリックします。
5. **\[Finish (完了)\]**​ をクリックします。

## [](#log4j2-xml-ファイル内のパターンレイアウトの変更)log4j2.xml ファイル内のパターンレイアウトの変更

この変更では、相関 ID とプロセッサーパスを含む MDC コンテキストを自動的に追加するように Mule に指示します。Mule アプリケーションの ​`log4j2.xml`​ ファイルまたは Mule インスタンスのファイルを変更できます。このファイルは ​`/conf/log4j2.xml`​ にあります。

次の手順に従って、パターンレイアウトを MDC に変更します。

1. `log4j2.xml`​ ファイルを編集目的で開きます。
2. `[processor: %X{processorPath}; event: %X{correlationId}]`​ を ​`[%MDC]`​ に置き換えます。

### [](#log4j2-xml-ファイルの設定例)log4j2.xml ファイルの設定例

例: MDC ログを含まないデフォルトの log4j2.xml ファイル。

<PatternLayout pattern="%-5p %d \[%t\] \[processor: %X{processorPath}; event: %X{correlationId}\] %c: %m%n"/>

例: MDC ログを含む更新後の log4j2.xml ファイル。

<PatternLayout pattern="%-5p %d \[%t\] \[%MDC\] %c: %m%n"/>

## [](#使用可能な操作)使用可能な操作

<table class="tableblock frame-all grid-all fit-content spread">
<colgroup>
<col>
<col>
<col>
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">XML 要素</th>
<th class="tableblock halign-left valign-top">説明</th>
<th class="tableblock halign-left valign-top">受け入れられる属性</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p><code>set-logging-variable</code></p>
</div></div></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p>ログ変数とその値を設定します。</p>
</div></div></td>
<td class="tableblock halign-left valign-top"><div class="content"><table class="tableblock frame-all grid-all fit-content spreadcols=" a"="">
<colgroup>
<col>
<col>
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">属性名</th>
<th class="tableblock halign-left valign-top">説明</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>variableName</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">記録する変数の名前。</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>value</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">変数に割り当てる値。DataWeave 式を受け入れます。</p></td>
</tr>
</tbody>
</table></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p><code>remove-logging-variable</code></p>
</div></div></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p>ログ変数を削除します。</p>
</div></div></td>
<td class="tableblock halign-left valign-top"><div class="content"><table class="tableblock frame-all grid-all fit-content spreadcols=" a"="">
<colgroup>
<col>
<col>
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">属性名</th>
<th class="tableblock halign-left valign-top">説明</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>variableName</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">削除する変数の名前。</p></td>
</tr>
</tbody>
</table></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p><code>clear-logging-variables</code></p>
</div></div></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p>すべてのログ変数を削除します。このオプションでは、プロセッサーパスまたは相関 ID は削除されません。</p>
</div></div></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p>なし</p>
</div></div></td>
</tr>
</tbody>
</table>

## [](#アプリケーションでの-mdc-ログの設定)アプリケーションでの MDC ログの設定

MDC ログを設定するには、使用可能ないずれかの操作を Mule アプリケーションフローに追加して、対応する属性を指定します。

たとえば、ログ変数を設定するには、​`<tracing:set-logging-variable>`​ XML 要素をアプリケーションフローに挿入します。

<flow name="exampleFlow">
    ...
    <tracing:set-logging-variable variableName="testVar" value="testValue" />
    ...
</flow>

フローの実行後、出力ログは次のようになります。

INFO  2021-04-08 16:58:26,882 \[\[MuleRuntime\].uber.15: \[test-project-app\].exampleFlow.CPU\_LITE @18f679\] \[{correlationId=c85e16c0-98a4-11eb-bc34-cac765a2219b, processorPath=exampleFlow/processors/2, testVar=testValue}\] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: Example

このログコンテキストは、Logger コンポーネントのすべての出力と Mule Runtime で生成されるすべての内部ログに影響します。このため、予期しないエラーが発生している場合など、潜在的な問題を見つけ出すための追加の情報が必要な場合に、Mule で処理しているイベントに、より多くのコンテキストを追加できます。

## [](#例)例

次のアプリケーションを考えてみます。

![MDC ログの例](/mulesoft/current/ja-jp/mule-runtime/4.4/_images/mruntime-mdc-example.png)

<flow name="logging-variables">
    <http:listener config-ref="HTTP\_Listener\_config" path="/order"/>
    <tracing:set-logging-variable variableName="customerId" value="#\[payload.customerId\]"/>
    <tracing:set-logging-variable variableName="requestPath" value='#\["$(attributes.method):$(attributes.requestPath)"\]'/>
    <logger level="INFO" message="#\[output application/json --- payload\]" />
</flow>

次の要求を送信します。

curl --location --request GET '0.0.0.0:8081/order' \\
--header 'Content-Type: application/json' \\
--data-raw '{
    "orderId": 548102842,
    "customerId": "ARG-12934",
    "items": \[
        "CP-123",
        "CP-452"
    \]
}'

出力ログは次のようになります。

INFO  2021-04-09 11:14:38,409 \[\[MuleRuntime\].uber.05: \[tracing-module\].tracing-moduleFlow.CPU\_LITE @34a62707\] \[processor: tracing-moduleFlow/processors/2; event: eb2b2461-993d-11eb-8a64-4865ee1fd814\] {correlationId=eb2b2461-993d-11eb-8a64-4865ee1fd814, customerId=ARG-12934, processorPath=tracing-moduleFlow/processors/2, requestPath=GET:/order} org.mule.runtime.core.internal.processor.LoggerMessageProcessor: {
    "orderId": 548102842,
    "customerId": "ARG-12934",
    "items": \[
        "CP-123",
        "CP-452"
    \]
}

## [](#関連情報)関連情報

- [ログの設定](https://docs.mulesoft.com/jp/mule-runtime/4.4/logging-in-mule)