tags:

views:

262

answers:

2

log4j is logging errors based on level. Is there a way to log errors based on timestamp?

+1  A: 

log4j can be configured to log with the timestamp in the line. At least that's what I think you're asking.

Use %d in your PatternLayout.

Now I understand: log4j is asynchronous, so you may have time stamps out of order.

If you want synchronous logging, I believe that is possible, but it is slower.

Nathan
Thanks Nathan.2009-02-19 14:47:01,288 DEBUG [com.catalystwms.core.persistence.TransactionContext] 2009-02-19 14:54:27,429 INFO [com.catalystwms.tms.services.background.purge2009-02-19 14:47:01,288 DEBUG.....My question is Can we log the message based on timestamps priority?
Log entries are executed in order of entry (this would make the timestamps ordered.) How do you prioritize time stamps exactly?
Nathan
Can we make 1.2009-02-19 14:47:01,288 DEBUG2.2009-02-19 14:47:01,288 DEBUG3.2009-02-19 14:54:27,429 INFO [com.catalystwms.tms.services.background.purge]
If you're looking for a way to see at what times a specific action occurred, it can be done, but not with log4j. It will not modify a previously entered log entry. You could throw this stuff into a database and group by the log description ([com.cata....purge])
Nathan
Thanks.Tasks are executing based on sequential timestamp.I do n't have idea why timestamps are not in sequence.
My question why it is not executed timestamp order like (1)14:47:01 (2)14:47:01 (3)14:54:27.I am expecting sequential order
Got it. It's due to log4j being asynchronous.
Nathan
Yes. Is there a way to get it synchronous?
You'd probably be better off inserting this in a DB and then sorting on the date entered. You don't want your logging code blocking the execution of your business logic.
Nathan
A: 

If you are logging to file with log4j, the timestamp is created when the logging event is created. So when the (debug, info, ...) method is invoked. If you have multiple threads running it could be that the messages are not written written to file in the same order as the timestamp.

If you require the log-lines to be sorted in order, use a JDBC Appender and log to the database. Then you can query the database and sort on timestamp.

Edwin