Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logback configuration to have exceptions on a single line?

My logs are extracted, piped and consolidated into elasticsearch. Multiline events are hard to track and diagnose.

Instead of playing with collectors and regular expressions to group exception lines in a single record, is there a way with logback configuration to have Exception stacktrace on a single line ?

like image 839
kheraud Avatar asked Dec 12 '17 11:12

kheraud


People also ask

What is additivity in Logback?

1.2 Logback Additivity Appenders are added to the loggers. One logger may include more than one appenders. Thus, its log messages are written more than one desired destination systems. Additivity is exactly about this point. The output of a log statement of logger A will go to all the appenders in A and its ancestors.

What is Appender in Logback?

Appenders place log messages in their final destinations. A Logger can have more than one Appender. We generally think of Appenders as being attached to text files, but Logback is much more potent than that. Layout prepares messages for outputting.

What is Logback configuration?

Logback executes an async appender in a separate thread to decouple the logging overhead from the thread executing your code. Using the async appender is incredibly easy. Refer the appender that should be asynchronously invoked within an <appender> element.

How do I use Logback with no custom configuration?

If no custom configuration is defined, Logback provides a simple, automatic configuration on its own. By default, this ensures that log statements are printed to the console at DEBUG level. Consequently, you can now obtain a Logger instance and start writing log messages using the default, basic config.

What configuration file does Logback use to print log messages?

In the previous examples, we were using the 11-line configuration file we created in section 4 to print log messages to the console. This is Logback's default behavior; if it can't find a configuration file, it creates a ConsoleAppender and associates it with the root logger. 6.1.

When to use Logback to print the stack trace?

Also, when an Exception is passed as the last argument to a logging method, Logback will print the stack trace for us. 6. Detailed Configuration In the previous examples, we were using the 11-line configuration file we created in section 4 to print log messages to the console.

What is Logback architecture in Salesforce?

The Logback architecture is comprised of three classes: Logger, Appender, and Layout. A Logger is a context for log messages. This is the class that applications interact with to create log messages. Appenders place log messages in their final destinations.


1 Answers

You can declare a conversion rule in your logback.xml for the %ex symbolic like so:

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
    <conversionRule conversionWord="ex" converterClass="com.foo.CompressedStackTraceConverter" /> 

    ...

</configuration>

Then declare CompressedStackTraceConverter like so:

import ch.qos.logback.classic.pattern.ThrowableProxyConverter;
import ch.qos.logback.classic.spi.IThrowableProxy;

public class CompressedStackTraceConverter extends ThrowableProxyConverter {
    @Override
    protected String throwableProxyToString(IThrowableProxy tp) {
        String original = super.throwableProxyToString(tp);

        // replace the new line characters with something, 
        // use your own replacement value here
        return original.replaceAll("\n", " ~~ ");
    }
}

This custom conversion specifier will work as long as your logging pattern contains the %ex symbolic. For example, a pattern like this:

<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
    <pattern>%d{yyyy-MM-dd HH:mm:ss}|%-5level|%logger{36}|%msg %ex %n</pattern>
</encoder>

If your pattern does not include the %ex symbolic then the stacktrace is part of the %msg in which case you would declare the conversion rule like this ...

<conversionRule conversionWord="msg" converterClass="com.foo.CompressedStackTraceConverter" /> 

... though this would have the effect of applying the CompressedStackTraceConverter to your entire log message, not just to the stack trace portion of it.

like image 198
glytching Avatar answered Oct 23 '22 08:10

glytching