简体   繁体   中英

log4j2 using CsvParameterLayout, how to add timestamp in the cells?

I am using log4j2's CsvParameterLayout to log.

logger.info("unzipPhase logging", "Timestamp", "Level",  "RenamedPath","Instruction","Status", "Message", "Layer#", "Thread#");
logger.info("unzipPhase logging...", "????", "info",  newName.getAbsolutePath(),"unzip","success", "Message...", "Layer#",Thread.currentThread().getName());

Is there any simple and direct way to get Timestamp ?

You can use KeyValuePair option in JsonLayOut .

Please choice and use one among of 3 cases.

CASE 1. Using KeyValuePair

  1. add JsonLayout in your configuration file. I used log4j2.xml . The key is <KeyValuePair key="timestamp" value="$${date:yyyy-MM-dd'T'HH:mm:ss.SSSZ}" />
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
     <Appenders>

        <Routing name="RoutingAppender">
            <Routes pattern="${ctx:logFileName}">
                <Route>
                    <RollingFile name="Rolling-${ctx:logFileName}"
                                 fileName="./logs/${ctx:logFileName}.log"
                                 filePattern="./logs/${ctx:logFileName}.%i.log.gz">
                        <JsonLayout>
                            <KeyValuePair key="timestamp" value="$${date:yyyy-MM-dd'T'HH:mm:ss.SSSZ}" />
                        </JsonLayout>
                        <SizeBasedTriggeringPolicy size="512" />
                    </RollingFile>
                </Route>
            </Routes>
        </Routing>
    </Appenders>

    <Loggers>
        <Root level="all">
            <AppenderRef ref="RoutingAppender" />
        </Root>
    </Loggers>

</Configuration>
  1. Run!

  2. Check the output. The key is timestamp .

{
  "instant" : {
    "epochSecond" : 1588683107,
    "nanoOfSecond" : 556000000
  },
  "thread" : "main",
  "level" : "INFO",
  "loggerName" : "com.study.Stackoverflow",
  "message" : "log printed! - testFile",
  "endOfBatch" : false,
  "loggerFqcn" : "org.apache.logging.log4j.spi.AbstractLogger",
  "threadId" : 1,
  "threadPriority" : 5,
  "timestamp" : "2020-05-05T21:51:47.556+0900"
}

Reference: Property Substitution - date

CASE 2. Using JsonLayOut

  1. add JsonLayout in your configuration file. I used log4j2.xml . The key is <JsonLayout includeTimeMillis="true">
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
     <Appenders>

        <Routing name="RoutingAppender">
            <Routes pattern="${ctx:logFileName}">
                <Route>
                    <RollingFile name="Rolling-${ctx:logFileName}"
                                 fileName="./logs/${ctx:logFileName}.log"
                                 filePattern="./logs/${ctx:logFileName}.%i.log.gz">
                        <JsonLayout includeTimeMillis="true">
                        </JsonLayout>
                        <SizeBasedTriggeringPolicy size="512" />
                    </RollingFile>
                </Route>
            </Routes>
        </Routing>
    </Appenders>

    <Loggers>
        <Root level="all">
            <AppenderRef ref="RoutingAppender" />
        </Root>
    </Loggers>

</Configuration>
  1. Run!

  2. Check the output. The key is timeMillis .

{
  "timeMillis" : 1588688067619,
  "thread" : "main",
  "level" : "INFO",
  "loggerName" : "com.edu.test.Stackoverflow",
  "message" : "log printed! - testFile",
  "endOfBatch" : false,
  "loggerFqcn" : "org.apache.logging.log4j.spi.AbstractLogger",
  "threadId" : 1,
  "threadPriority" : 5,
  "test" : "11111"
}

Reference : [JSONLayout - includeTimeMillis](https://logging.apache.org/log4j/2.x/manual/layouts.html#JSONLayout)

CASE 3. Using System Property Sorry, I couldn't make working code. I don't know how could I adopt it in code.

Reference: System Properties - log4j2.simplelogShowdatetime

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