5

Before writing this query, I reviewed similar queries at:

I felt my query sounds similar but not the same.

Did you ever come across grouping of log messages on request entry and flush them on response close. i.e. the sequence of operations expected would be as below:

  1. configure the Logger to request scope.
  2. server receives request.
  3. logs written by each of the context beans be buffered by request scoped logger.
  4. response prepared, flushed and closed.
  5. logger gets notified that response is complete.
  6. now logger writes the buffered messages as a batch to associated appender and then clears its buffer.

I am looking for such a possibility with currently available log4j implementation.

Please share your opinions.

Community
  • 1
  • 1
Ravinder Reddy
  • 23,692
  • 6
  • 52
  • 82

2 Answers2

3

include request id into log (you may use log4j's NDC). sort log file by this id and by timestamp.

i think it's wrong, to record events in the order, different from their original order. This may lead to a a very big confusion.

aav
  • 2,514
  • 1
  • 19
  • 27
2

I usually don't group logs by request because a conversation is longer than a single request. So my solution is to add the user name to log4j's MDC.

That way, I can for example create one log file per user or see all the requests which were made by a user (plus the responses and all the code that was executed on the user's behalf outside of the requests, for example when background jobs are started).

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • I did use MDC earlier. Trying for other alternatives to write entire log message generated per request as a batch into log files. With MDC I could differentiate each log statement easily, but was painful to drill down the log content. – Ravinder Reddy Oct 11 '11 at 10:45
  • It was not by coding but feeling. It was on searching each logged statement for the MDC keyword. Wanted something like: `--- boundaryString :start` ` plus log statement from bean1` ` plus log statement from bean2` `...` `--- boundaryString :end` – Ravinder Reddy Oct 11 '11 at 11:53
  • The way to make this work is to use a `SiftingAppender`: http://stackoverflow.com/questions/840465/have-you-seen-an-appender-that-would-log-to-separate-files-based-on-ndc-in-log4j Boundaries can't work because any number of threads can try to log at the same time, so the boundaries will overlap. – Aaron Digulla Oct 11 '11 at 12:00