简体   繁体   中英

Jboss AS 7 custom logger issue

My application runs on Jboss AS 7.1.1.final. I needed logs to be written to DB so i wrote a custom handler: public class DataSourceHandler extends java.util.logging.Handler .

Everything works fine but i need to get the line number and the name of the class where this log was called from and also the stacktrace if there was an error.

Before we upgraded to Jboss 7 we used log4j and we managed to get it using:

org.apache.log4j.spi.LocationInfo locationInfo = new org.apache.log4j.spi.LocationInfo(event.getThrown(), event.getSourceClassName());
org.apache.log4j.spi.ThrowableInformation throwableInfo = new org.apache.log4j.spi.ThrowableInformation(event.getThrown());

if (locationInfo != null) {
    fileName = locationInfo.getFileName();
    lineNumber = locationInfo.getLineNumber();
}
if (throwableInfo != null) {
    String[] exceptionArray = throwableInfo.getThrowableStrRep();
    for (String line : exceptionArray) {
        exceptionBuffer.append(line).append(NEW_LINE);
}
info = extractInfo(exceptionBuffer);
}

How can i do it now?

That information is not provided by JUL If you don't mind having a dependency on jboss-logmanger you could extend org.jboss.logmanager.ExtHandler instead of the JUL handler. That will provide you with the a org.jboss.logmanager.ExtLogRecord that will provide that information.

You would just override the doPublish(ExtLogRecord) instead of the JUL publish method.

JBoss AS 7 uses JBoss Logging and JBoss Log Manager so that dependency would already be provided for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM