简体   繁体   中英

Log4j2 JSONLayout not appending closing (]) square bracket

Here is my log4j2.xml config file:

<?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?

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).

You can stop the appender which will make log4j add the closing ]

// 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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