开发者

Is there a way to trigger a stacktrace whenever a particular logger is used?

I'm currently trying to track d开发者_Go百科own 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.


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>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜