[logback-user] Filtering stacktraces by evaluating packages

Christopher BROWN brown at reflexe.fr
Mon Dec 5 12:44:25 CET 2011


Hello,

I would like to simplify stacktraces when errors occur in an application.
 For example, with a hypothetical stack such as:

com.xyz.Wombat.doSomething
javax.servlet.http.HttpServlet.service
org.appserver.JspCompiler.handleJsp
org.appserver.ServletEngine.handleServlet
java.lang.Thread.run


...going down from top-to-bottom, I'd like to detect that line
"org.appserver.JspCompiler.handleJsp" has been found, and exclude it -- and
all subsequent "lower down" stack frames -- from logging output.

Looking at http://logback.qos.ch/manual/layouts.html#Evaluators I assume it
must be possible with EventEvaluator because of the syntax
%xEx{evaluator-n} but I can't find an example.  Furthermore, as I set up
LogBack programmatically (and not via XML or any of bundled
"configurator"), I'd be happiest if I could be directed towards an
API-based example.

If it's of any use, here are two methods extracted from the class that I
use to configure LogBack (incomplete but should help see what I'm trying to
do, and how):

 private synchronized boolean maybeInitialize(final Logger logger, String
pattern)
 {
// will throw ClassCastException if our configuration is incorrect (or
accessed from wrong classloader)
 // that's OK, it's what should happen...
final ch.qos.logback.classic.Logger impl =
(ch.qos.logback.classic.Logger)logger;

// if there's an appender, then the logger seems to have been configured
 Iterator<Appender<ILoggingEvent>> it = impl.iteratorForAppenders();
 if (it.hasNext())
{
 return false;
}

String logFileName = impl.getName();
 if (logFileName.startsWith(LOG_FILE_STDPREFIX) && logFileName.length() >
LOG_FILE_STDPREFIX.length())
 {
logFileName = logFileName.substring(LOG_FILE_STDPREFIX.length());
 }
logFileName += LOG_FILE_EXTENSION;

Appender<ILoggingEvent> appender;
 if (CONFIG_VALUE_ROLL_TYPE_DATE.equals(_rollType))
{
 appender = createRollByDateFileAppender(logFileName, pattern != null ?
pattern : PATTERN_FOR_DATE);
 }
else if (CONFIG_VALUE_ROLL_TYPE_SIZE.equals(_rollType))
 {
appender = createRollBySizeFileAppender(logFileName, pattern);
 }
else
 {
appender = createFileAppender(logFileName, pattern);
 }

 appender.start();

 impl.addAppender(appender);
impl.setLevel(_level);
 impl.setAdditive(false);

 return true;
}

private synchronized boolean maybeInitialize(final Logger logger, String
pattern)
 {
// will throw ClassCastException if our configuration is incorrect (or
accessed from wrong classloader)
 // that's OK, it's what should happen...
final ch.qos.logback.classic.Logger impl =
(ch.qos.logback.classic.Logger)logger;

// if there's an appender, then the logger seems to have been configured
 Iterator<Appender<ILoggingEvent>> it = impl.iteratorForAppenders();
 if (it.hasNext())
{
 return false;
}

String logFileName = impl.getName();
 if (logFileName.startsWith(LOG_FILE_STDPREFIX) && logFileName.length() >
LOG_FILE_STDPREFIX.length())
 {
logFileName = logFileName.substring(LOG_FILE_STDPREFIX.length());
 }
logFileName += LOG_FILE_EXTENSION;

Appender<ILoggingEvent> appender;
 if (CONFIG_VALUE_ROLL_TYPE_DATE.equals(_rollType))
{
 appender = createRollByDateFileAppender(logFileName, pattern != null ?
pattern : PATTERN_FOR_DATE);
 }
else if (CONFIG_VALUE_ROLL_TYPE_SIZE.equals(_rollType))
 {
appender = createRollBySizeFileAppender(logFileName, pattern);
 }
else
 {
appender = createFileAppender(logFileName, pattern);
 }

 appender.start();

 impl.addAppender(appender);
impl.setLevel(_level);
 impl.setAdditive(false);

 return true;
}


Thanks,
Christopher
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.qos.ch/pipermail/logback-user/attachments/20111205/b4e31800/attachment-0001.html>


More information about the Logback-user mailing list