Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4j : current time in milliseconds

Tags:

java

log4j

In log4j.properties I can set PatternLayout e.g. ("[%p] %c - %m - %d %n")

Is there any symbol (%something) which returns current time in milliseconds?

like image 440
alicjasalamon Avatar asked Aug 01 '12 10:08

alicjasalamon


People also ask

What is log level in log4j?

Logging levels are used to categorize the entries in your log file. But they categorize in a very specific way, i.e., by urgency. The level allows you to separate the following kinds of information: You can filter your log files during the search.

What is log4j format?

Apache log4j provides various Layout objects, each of which can format logging data according to various layouts. It is also possible to create a Layout object that formats logging data in an application-specific way. All Layout objects receive a LoggingEvent object from the Appender objects.

What is PatternLayout in Log4j2?

The PatternLayout class extends the abstract org. apache. log4j. Layout class and overrides the format() method to structure the logging information according to a supplied pattern.

How do I change the date format in log4j?

The most commonly used layout for Log4j is PatternLayout. A sample pattern is %d [%t] %-5p (%F: %L) – %m%n . The format strings for the pattern are as follows: Date – Full date until microseconds.


2 Answers

You can try this one.

log4j.appender.appender_name.layout=org.apache.log4j.PatternLayout
log4j.appender.appender_name.layout.ConversionPattern=%d %p [%c] - %m%n

Date params %d. For example : %d{HH:mm:ss,SSS}.

http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html

like image 153
turankonan Avatar answered Oct 10 '22 15:10

turankonan


There is no Log4J symbol that does exactly what you want.

%d returns the current date with a given pattern, defined by a SimpleDateFormat (the pattern you put between the brackets), but doesn't give you the time in millis.

%r gives the number of milliseconds since the start of execution.

One possible way of achieving what you want is to extend the behaviour of Log4j, it's quite a bit more complex, but if it's absolutely necessary... here you go:

Customize log4j (edit: no longer online?)
Customize log4j (edit: 2018 alternative)

Edit: Keep in mind that, from your comment, if you need to figure out time differences between executions in different machines, you have to make sure the clocks of the machines are synchronized, or it'll be leading you to wrong conclusions.

like image 27
pcalcao Avatar answered Oct 10 '22 15:10

pcalcao