Log4J(2.6.2)を使用してJSONファイルにログを保存しようとしています。私はRollingFileAppender
にJsonLayout
を使用しています。すでに書かれているファイルに追加しようとしていない限り、正しく動作しています。Log4J - JsonLayoutとRollingFileAppenderが無効なJSONを生成しています
はここでレイアウトとアペンダを設定するための私のコードです:
[
{
"timeMillis" : 1469620840442,
"thread" : "SimpleAsyncTaskExecutor-43",
"level" : "ERROR",
"loggerName" : "MyLogger",
"message" : "my log message",
"endOfBatch" : false,
"loggerFqcn" : "org.apache.logging.log4j.spi.AbstractLogger",
"threadId" : 243,
"threadPriority" : 5
}
, {
"timeMillis" : 1469620840442,
"thread" : "SimpleAsyncTaskExecutor-43",
"level" : "DEBUG",
"loggerName" : "MyLogger",
"message" : "my log message",
"endOfBatch" : false,
"loggerFqcn" : "org.apache.logging.log4j.spi.AbstractLogger",
"threadId" : 243,
"threadPriority" : 5
}
]
I:
Layout<?> layout = JsonLayout.createLayout(config, false, false, false, true, false, true, "[", "]", Charset.defaultCharset());
String appenderFileName = "mylogfile-latest.log.json";
String appenderFilePattern = "mylogfile-%i.log.json";
String appenderName = "MyAppender";
Appender appender = RollingFileAppender.createAppender(appenderFileName, appenderFilePattern, "true", appenderName, "true", "256", "true",
SizeBasedTriggeringPolicy.createPolicy(this.configuration.getLogMaxSize().toString()),
null, layout, null, "false", "false", null, config);
私が言ったように、それは罰金、私は私のログファイルに書いている最初の時間を働いています私のアペンダーなどを閉じてアプリケーションを再起動してください。この既存のファイルに新しいログを書き込んでいるときは、ここにあります:
[
{
"timeMillis" : 1469620840442,
"thread" : "SimpleAsyncTaskExecutor-43",
"level" : "ERROR",
"loggerName" : "MyLogger",
"message" : "my log message",
"endOfBatch" : false,
"loggerFqcn" : "org.apache.logging.log4j.spi.AbstractLogger",
"threadId" : 243,
"threadPriority" : 5
}
, {
"timeMillis" : 1469620840442,
"thread" : "SimpleAsyncTaskExecutor-43",
"level" : "DEBUG",
"loggerName" : "MyLogger",
"message" : "my log message",
"endOfBatch" : false,
"loggerFqcn" : "org.apache.logging.log4j.spi.AbstractLogger",
"threadId" : 243,
"threadPriority" : 5
}
]
{
"timeMillis" : 1469620840490,
"thread" : "SimpleAsyncTaskExecutor-43",
"level" : "ERROR",
"loggerName" : "MyLogger",
"message" : "my log message",
"endOfBatch" : false,
"loggerFqcn" : "org.apache.logging.log4j.spi.AbstractLogger",
"threadId" : 245,
"threadPriority" : 5
}
, {
"timeMillis" : 1469620840492,
"thread" : "SimpleAsyncTaskExecutor-43",
"level" : "DEBUG",
"loggerName" : "MyLogger",
"message" : "my log message",
"endOfBatch" : false,
"loggerFqcn" : "org.apache.logging.log4j.spi.AbstractLogger",
"threadId" : 245,
"threadPriority" : 5
}
]
この問題により、GsonがこのファイルからList
を作成できないため、ログを解析することができなくなります。
]
を削除して厄介なハックを実行せずにこの問題を解決する方法がありますか?それをコンマで置き換えてください。
ありがとうございます!