Log4j ConversionPattern microsecond timestamp

Possible duplicate:
Enable microsecond timestamps in log4j log?

I want to add microseconds to the timestamp of each record of my log files generated with log4j, is this possible?

I searched in the official documentation, but the unit is not mentioned below milliseconds.

Now I have a conversion template, for example:

%d{dd/MM/yyyy HH\:mm\:ss,SSS} %-5p [%t] - %m%n 

in the date conversion pattern ( % d ) I want to add microseconds after the millisecond value ( SSS ), is there a way to do this?

+6
source share
3 answers

If you want to display microseconds, you need to add it yourself. This can be done using a special Formatter (instead of PatternFormatter)

+5
source

it is based on SimpleDateFormat and does not support formats in milliseconds, so it is impossible to report microseconds.

+2
source

Starting with Java 9 and log4j 2.11.0 , you can get a timestamp even with nanoseconds.

Here are the special predefined patterns for getting a date or time with microseconds or nanoseconds:

  • Output: %d{ABSOLUTE_MICROS} Example output: 14:34:02,123456
  • Output: %d{ABSOLUTE_NANOS} Example output: 14:34:02,123456789
  • Sample: %d{DEFAULT_MICROS} Example output: 2012-11-02 14:34:02,123456
  • Sample: %d{DEFAULT_NANOS} Example output: 2012-11-02 14:34:02,123456789

If you need a more specific model, use the letter "nano-of-second" of pattern n instead of the letter "fractional part" S , for example %d{dd MMM yyyy HH:mm:ss,nnnn} to %d{dd MMM yyyy HH:mm:ss,nnnnnnnnn} , will give 02 Nov 2012 14:34:02,1234 to 02 Nov 2012 14:34:02,123456789 .

In your case, since you need milliseconds and microseconds, your template might be something like %d{dd/MM/yyyy HH:mm:ss,nnnnnn}

Log4j 2.11 adds limited timestamp support more accurately than milliseconds when running Java 9.

See https://logging.apache.org/log4j/2.x/manual/layouts.html for details

+1
source

Source: https://habr.com/ru/post/903051/


All Articles