How to group Java log entries?

I would like to group my java log entries to search among them more easily. I mean the following log entries:

14-Mar-2012 14:29:07 com.acme.service.AcmeService log
INFO: Start ACME operation
14-Mar-2012 14:29:07 com.acme.service.AcmeService log
INFO: step 1 - do something
14-Mar-2012 14:29:07 com.acme.service.AcmeService log
INFO: End ACME operation


These entries are relative to each other as they are workflow steps (for example, I would like to track a web service process). Is there a good design pattern for storing these entries that I can easily find in the web service request logs? (for example, to start all ID log entries)? Is there a tool that can display these logs by grouping them automatically? like a log4j chainsaw but grouping together with that id? Thank!


source to share

4 answers

Why don't you expand the level and add your own level (s). All your webservice statements can be done at this level.

Once you have your own level, you can customize how it is logged, including logging to another file, etc.



Are you using sessions in your webapp? If so, you can use the session ID to distinguish between requests.

Sorry, I just realized by looking at your example that I think you are using java.util.Logging. Below will only work log4j. I am curious to see if anyone has a fix that I would like to know. In my experience with both, I've found log4j much, much easier to use and configure.

Log4j solution

Pass all requests through a filter and then use NDC.push( id )

. Then use %x

in your template to output to a file.



If you are working in Spring technologies, you can easily configure the Spring integration module to receive logs in a pipe, separate them by type using a router, and store them in a database using an outbound adapter. A lot of conductors fail, you only need to do minimal coding.



You can use Chainsaw to parse a regular text log file, no matter which logging API was used to create it. See Sample LogFilePatternReceiver in the sample receiver xml configuration available on the Welcome tab.

In your example, once you include the session in the log output, you can add the session as a property and then color / sort and filter with the session. You can also use this session id as a "logger" and take advantage of the log tree tree to focus on individual loggers, basically giving you the "grouping" behavior.



All Articles