Flex Gateway新着情報
Governance新着情報
Monitoring API ManagerTracing Module の Clear logging variables、Remove logging variable、Set logging variable 操作を使用すると、1 つの Mule イベントのログコンテキストの変数を追加、削除、クリアすることでログを強化できます。
Mapped Diagnostic Context (MDC) では、より多くのコンテキストまたは情報が現在の Mule イベントのログに提供されるため、ログが強化され、追跡が改善されます。デフォルトでは、Mule では、現在のイベントの場所を表示する processor
と、イベントの相関 ID を表示する event
の 2 つの MDC エントリが記録されます。
Tracing module logging 操作を使用するには、次のタスクを完了します。
モジュールを Mule プロジェクトに追加します。
log4j.xml
ファイル内のパターンレイアウトを MDC
に変更します。
Tracing Module を Mule プロジェクトに追加して、XML コードにモジュールの名前空間およびスキーマの場所を自動的に入力し、プロジェクトの pom.xml
ファイルに必須の連動関係を追加します。
[Mule Palette (Mule パレット)] ビューで、[(X) Search in Exchange ((X) Exchange 内を検索)] をクリックします。
[Add Dependencies to Project (連動関係をプロジェクトに追加)] ウィンドウで、検索項目に「tracing
」と入力します。
[Available modules (使用可能なモジュール)] で [Tracing Module] をクリックします。
[Add (追加)] をクリックします。
[Finish (完了)] をクリックします。
この変更では、相関 ID とプロセッサーパスを含む MDC コンテキストを自動的に追加するように Mule に指示します。変更を行うには、Mule アプリケーションで log4j.xml
を変更するか、Mule インスタンス内で /conf/log4j2.xml
を変更します。
次の手順に従って、パターンレイアウトを MDC に変更します。
log4j.xml
ファイルを編集目的で開きます。
[processor: %X{processorPath}; event: %X{correlationId}]
を [%MDC]
に置き換えます。
<PatternLayout pattern="%-5p %d [%t] [processor: %X{processorPath}; event: %X{correlationId}] %c: %m%n"/>
<PatternLayout pattern="%-5p %d [%t] [%MDC] %c: %m%n"/>
logging variables 操作を設定するには、Clear logging variables、Remove logging variable、Set logging variables のいずれかの使用可能な操作を Mule アプリケーションフローに追加して、対応する属性を指定します。
次の例では、Set logging variables 操作を設定します。
Studio の [Mule Palette (Mule パレット)] ビューで、[Tracing] > [Set logging variable] を選択します。
[Set logging variable] を Mule アプリケーションにドラッグします。
[Variable name (変数名)] を testVar
に設定します。
[Value (値)] を「testValue
」に設定します。
設定 XML エディターでは、<tracing:set-logging-variable>
設定は次のように記述されます。
<flow name="exampleFlow">
...
<tracing:set-logging-variable variableName="testVar" value="testValue" />
...
</flow>
xml
フローの実行後、出力ログは次のようになります。
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 Engine で生成されるすべての内部ログに影響します。
次の例では、Set logging variable 操作を設定し、要求ペイロードおよび属性に基づいて変数を記録します。
[Mule Palette (Mule パレット)] ビューで、[HTTP] > [Listener] を選択します。
[Listener] を Studio キャンバスにドラッグします。
[Listener] 設定画面で、[Path (パス)] を /order
に設定します。
[Connector configuration (コネクタ設定)] 項目の横にあるプラス記号 (+) をクリックして、アプリケーション内の HTTP リスナーのすべてのインスタンスで使用できるグローバル要素を設定します。
[Mule Palette (Mule パレット)] ビューで、[Tracing] > [Set logging variable] を選択します。
[Set logging variable] を [HTTP] の [Listener] の右にドラッグします。
[Variable name (変数名)] を customerId
に設定します。
[Value (値)] を #[payload.customerId]
に設定して、要求で送信される顧客 ID を記録します。
別の [Set logging variable] 操作を最初の [Set logging variable] 操作の右にドラッグします。
[Variable name (変数名)] を requestPath
に設定します。
[Value (値)] を '#["$(attributes.method):$(attributes.requestPath)"]'
に設定して、要求パスを記録します。
[Logger] コンポーネントを 2 番目の [Set logging variable] 操作の右にドラッグします。
[Message (メッセージ)] を #[output application/json --- payload]
に設定して、要求ペイロードを記録します。
Mule アプリケーションを保存して実行します。
次の curl コマンドを実行します。
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" ] }'
xml
出力では、customerId
、requestPath
、要求ペイロードが記録されます。
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" ] }
xml
この例のフローをすばやく Mule アプリケーションに読み込むには、次のコードを Studio XML エディターに貼り付けます。
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:tracing="http://www.mulesoft.org/schema/mule/tracing"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/tracing http://www.mulesoft.org/schema/mule/tracing/current/mule-tracing.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="2258f968-60ad-41d3-a1c1-5afeffd89297" >
<http:listener-connection host="0.0.0.0" port="8081" />
</http:listener-config>
<flow name="tracingmodule2Flow" >
<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>
</mule>
xml