views:

67

answers:

2

I'm currently trying to track down the source of some lazy loading calls in hibernate, and the easiest way to do so would be to turn on hibernate SQL logging whenever the lazy loading is going to occur and then ideally trigger a stack trace output whenever the logger is used. Right now I'm using Hibernate 3.5.2 which uses SLF4j and using Log4j as my logging implementation.

I suppose I could use AOP to surround every logging call and check if its a call to the SQL logger, but this seems kind of heavy handed and I wanted to know if there was a simpler approach that I was missing before I went down that road.

A: 

You could write your own SLF4j bridge and put that in the classpath instead of the Log4j one. You could then delegate to Log4j but intercept the calls as you see fit, without AOP. It seems simpler to instrument, and doesn't suffer from any additional challenges over and above the AOP in terms of figuring out the right logger and when lazy loading is happening.

Yishai
+5  A: 

You can extend one of the log4j appenders and then use that in your log4j.xml.

public class StackPrintingFileAppender extends FileAppender {
    protected void subAppend(LoggingEvent event) {
        new Exception().printStackTrace(new PrintWriter(qw));
        super.subAppend();
    }
}

and then in log4j.xml:

<appender name="logger" class="StackPrintingFileAppender">
    ...
</appender>
krock
Thanks, thats exactly the kind of thing I want. it doesn't actually do what I wanted (only print stack traces for the category I want, but I can easily examine the LoggingEvent to test if its the one I want to print stack traces for.
Jherico
Narrow it down so the appender is only active for what you Want to see.
Thorbjørn Ravn Andersen