これは、log4j2 JSONLayoutを使用して行うことができます。
例:あなたが偽=コンパクト、JSONはprity形式のプリントになります設定した場合、ここで
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<JSONLayout compact="true" eventEol="true" properties="true"/>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
- セカンドキー値のペアとしてpu値に
ThreadContext
を入れることができます。
例:
ここ
ThreadContext
for (int i = 0; i < 5; i++) {
ThreadContext.clearAll();
ThreadContext.put("delay", String.valueOf(i));
ThreadContext.put("name", "Ash " + i);
LOGGER.info("Testing ", "k1", "v1", "k2", "v2");
}
がはっきりするまで、すべてのキーと値のペアを保持します。
出力:
{"timeMillis":1491819918389,"thread":"main","level":"INFO","loggerName":"com.ashraful.ilpexp.LogTest","message":"Testing ","endOfBatch":false,"loggerFqcn":"org.apache.logging.log4j.spi.AbstractLogger","contextMap":{"delay":"0","name":"Ash 0"},"threadId":1,"threadPriority":5}
{"timeMillis":1491819918504,"thread":"main","level":"INFO","loggerName":"com.ashraful.ilpexp.LogTest","message":"Testing ","endOfBatch":false,"loggerFqcn":"org.apache.logging.log4j.spi.AbstractLogger","contextMap":{"delay":"1","name":"Ash 1"},"threadId":1,"threadPriority":5}
{"timeMillis":1491819918504,"thread":"main","level":"INFO","loggerName":"com.ashraful.ilpexp.LogTest","message":"Testing ","endOfBatch":false,"loggerFqcn":"org.apache.logging.log4j.spi.AbstractLogger","contextMap":{"delay":"2","name":"Ash 2"},"threadId":1,"threadPriority":5}
{"timeMillis":1491819918505,"thread":"main","level":"INFO","loggerName":"com.ashraful.ilpexp.LogTest","message":"Testing ","endOfBatch":false,"loggerFqcn":"org.apache.logging.log4j.spi.AbstractLogger","contextMap":{"delay":"3","name":"Ash 3"},"threadId":1,"threadPriority":5}
{"timeMillis":1491819918505,"thread":"main","level":"INFO","loggerName":"com.ashraful.ilpexp.LogTest","message":"Testing ","endOfBatch":false,"loggerFqcn":"org.apache.logging.log4j.spi.AbstractLogger","contextMap":{"delay":"4","name":"Ash 4"},"threadId":1,"threadPriority":5}
にlog4j2.xmlパターンのレイアウトを変更。 – Fildor
理想的には、私はそれを避けたいが、方法がないことを知ることも有用である。 – LetsPlayYahtzee
タイムスタンプはメッセージ自体のメタ情報の一部であり、実際にはメッセージをメタデータに移動したい。あなたはMDCを試しましたか? - > PatternLayoutを使用する場合は、コンテキスト内の現在のユーザーをメッセージに再利用します。それがうまくいかない場合は、誰も解決策が出てこないので、自分のJsonLayoutを実装することをお勧めします... – slowy