1
votes

I have a Spring web app. I would like to change its logging behavior to always prepend log lines with the name of the logged in user which is stored in a session-scoped bean. We are using log4j. There is a lot of classes which are logging but are not Spring-managed. While we do use some AOP, it is all Spring AOP, not AspectJ, so I can't modify log4j behavior globally (and introducing AspectJ just for a silly logging improvement doesn't sound like a good idea).

I can't think of any sane (or even acceptable insane) way of doing this.

1

1 Answers

1
votes

In your servlet, you could use the nested diagnostic context of log4j do to so:

NDC.push(getUsername());
try {
    ....
}
finally {
    NDC.pop();
}

If you are using the spring dispatcher servlet, you could wrap the dispatcher servlet in your own servlet, delegate all calls to the dispatcher servlet and add the NDC-stuff there.