简体   繁体   English

Log4j2 JSONLayout不附加右(])方括号

[英]Log4j2 JSONLayout not appending closing (]) square bracket

Here is my log4j2.xml config file:这是我的log4j2.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="OFF">
    <Appenders>
        <File name="JsonFile" fileName="logs/myLog.log" immediateFlush="true" append="false">
            <JSONLayout complete="true" charset="UTF-8" compact="false" eventEol="false" properties="true"/>
        </File>
    </Appenders>
    <Loggers>
        <Root level="all" includeLocation="true">
            <AppenderRef ref="JsonFile"/>
        </Root>
    </Loggers>
</Configuration>

The output looks as follows:输出如下所示:

[
{
  "timeMillis" : 1503303149998,
  "thread" : "Restlet-343114711",
  "level" : "INFO",
  "loggerName" : "my.logger.name",
  "message" : "Import completed successfully",
  "endOfBatch" : false,
  "loggerFqcn" : "org.apache.logging.log4j.spi.AbstractLogger",
  "contextMap" : {
    "class" : "my.package.Class",
    "method" : "getProgress",
    "user" : "myUser"
  },
  "threadId" : 45,
  "threadPriority" : 5
}

Which is missing a closing square bracket.缺少一个右方括号。 How do I configure log4j2 to add the closing tag?如何配置 log4j2 以添加结束标记?

For others who might be facing this issue with console appenders, and would like log4j to append the ] when the execution is finisehd (in case of a CLI for example).对于可能在控制台附加程序中遇到此问题并希望 log4j 在执行完成时附加]的其他人(例如,在 CLI 的情况下)。

You can stop the appender which will make log4j add the closing ]您可以停止附加程序,这将使 log4j 添加关闭]

// Notice casting from spi.LoggerContext to core.LoggerContext    
LoggerContext logContext = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false);
Map<String, LoggerConfig> map = logContext.getConfiguration().getLoggers();
map.get(LOGGER_NAME).getAppenders().get(APPENDER_NAME).stop();

If you appender is attached to the root logger, use an empty string "" for the the variable LOGGER_NAME如果您的附加程序附加到根记录器,请为变量LOGGER_NAME使用空字符串""

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM