简体   繁体   中英

adding microsecond level to log4j

referring to this

How can I add microsecond level logging, how to create custom pattern formatter for this?

I have also tried log4j2, but it ends up giving

12:42:05,972803200 [main] DEBUG Log4j2Test - Debug Message Logged !!!
12:42:05,975803200 [main] INFO  Log4j2Test - Info Message Logged !!!
12:42:05,975803200 [main] ERROR Log4j2Test - Error Message Logged !!!

as you can see it gives 803200 microseconds and nanoseconds always.

My log4j2.xml looks like:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
    <Console name="console" target="SYSTEM_OUT">
    <PatternLayout pattern="%d{ABSOLUTE_NANOS} [%t] %-5level %logger{36} - %msg%n"/>
       <!--  <PatternLayout
            pattern="[%-5level] %d{ABSOLUTE_NANOS} [%t] %c{1} - %msg%n" /> -->
    </Console>
</Appenders>
<Loggers>
    <Root level="debug" additivity="false">
        <AppenderRef ref="console" />
    </Root>
</Loggers> 

It depends on what log4j you happen to be using. This feature is only supported if you use JDK9 and new(er) version of log4j ( 2.x).

The format you happen to be searching for is this one: %d{HH:mm:ss,nnnnnn}

Please also notice what official documentation says, because there are lots of "catches and gotchas". Ie:

Log4j 2.11 adds limited support for timestamps more precise than milliseconds when running on Java 9. Note that not all DateTimeFormatter formats are supported. Only timestamps in the formats mentioned in the table above may use the "nano-of-second" pattern letter n instead of the "fraction-of-second" pattern letter S. Users may revert back to a millisecond-precision clock when running on Java 9 by setting system property log4j2.Clock to SystemMillisClock

More info on following link: Log4j 2.x layouts

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