Override Logback error output
Asked Answered
P

2

10

In my custom exception class I've overridden toString():

@Override
public String toString() {
    final String msg = getLocalizedMessage();

    // base
    String str = getClass().getName() + ": [" + code + "]";

    // message
    if (msg != null)
        str += " " + msg;

    // extra
    if (extra != null) {
        str += '\n' + extra.toString();
    }

    return str;
}

(yes, I'm aware I should use StringBuilder there)

However, when I log such an exception (via org.slf4j.Logger.warn(String msg, Throwable err)) the output is as for vanilla exceptions:

webersg.util.service.ServiceError: null
    at webersg.util.service.ServiceTools.decodeException(ServiceTools.java:39) ~[bin/:na]
    at tr.silvercar.rummikub.robot.LobbyConnection.sendRequestTo(LobbyConnection.java:143) ~[bin/:na]
    at tr.silvercar.rummikub.robot.LobbyConnection.sendRequest(LobbyConnection.java:98) ~[bin/:na]
    at tr.silvercar.rummikub.robot.Robot.<init>(Robot.java:32) ~[bin/:na]
    at tr.silvercar.rummikub.robot.RobotController.start(RobotController.java:81) ~[bin/:na]
    at tr.silvercar.rummikub.robot.runners.LocalAltinRobot.main(LocalSilverRobot.java:132) [bin/:na]

How can I get my extra data to appear? My log implementation is Logback.

Prelusive answered 13/4, 2011 at 14:45 Comment(0)
T
4

A quick and dirty way is this: logger.warn("your_message_here \n{}", err.toString());

A better way would be to write your own custom conversion specifier (see logback manual so that the custom error handling is abstracted from your application code, and you can configure it's use through a custom conversion word in your logback.xml.

Here's a simple example that will do what you're after:

package com.example.logging;

import ch.qos.logback.classic.pattern.ClassicConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.IThrowableProxy;
import ch.qos.logback.classic.spi.ThrowableProxy;

/**
 * Simple exception converter.
 *
 */
public class MyExceptionConverter extends ClassicConverter {
    /* (non-Javadoc)
     * @see ch.qos.logback.core.pattern.Converter#convert(java.lang.Object)
     */
    @Override
    public String convert(ILoggingEvent event) {
        IThrowableProxy itp = event.getThrowableProxy();
        if (itp instanceof ThrowableProxy) {
            ThrowableProxy tp = (ThrowableProxy)itp;
            return tp.getThrowable().toString();
        }

        return "";
    }
}

Then in your logback.xml you'll need to reference it like this:

  <conversionRule conversionWord="myCon"
              converterClass="com.example.logging.MyExceptionConverter" />

And then use %myCon in an encoder pattern like this:

<encoder>
    <pattern>%d{dd MMM yyyy HH:mm:ss.SSS} %logger{0}: %myCon %nopex%n</pattern>
</encoder>

Note that adding %nopex stops the usual exception handling

Twentyfour answered 18/7, 2011 at 23:17 Comment(2)
You should extend from ThrowableHandlingConverter (instead of extending from ClassicConverter). Then you also do not need the %nopex hack. And, in case you want to print out the stack trace, %myCon should go after %n.Fenny
I am getting IThrowableProxy as null for java.sql.SQLSyntaxErrorException with spring bootPainter
B
0

I faced the same problem.Logback.error calls the getMessage method of a exception. Overriding the method is another way.

@Override
public String getMessage() {
    return toString();
}
Bedside answered 27/3, 2021 at 1:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.