KI-NAM BANG
KI-NAM BANG

Reputation: 295

how to print custom stacktrace in java logback?

Current logback.xml

<appender name="FILEOUT3" class="ch.qos.logback.core.FileAppender">
    <file>D:/${byDay}.log</file>
    <append>true</append>
    <encoder>
        <Pattern>%d{HH:mm:ss} %-5level %msg%replace(%xException){"\n", ">> "}%nopex%n</Pattern>
    </encoder>
</appender>

Current result :

play.api.Configuration$$anon$1: Configuration error[Cannot connect to database [default]]
>> at play.api.Configuration$.play$api$Configuration$$configError(Configuration.scala:92) ~[play_2.10-2.2.0.jar:2.2.0]
>> at play.api.Configuration.reportError(Configuration.scala:570) ~[play_2.10-2.2.0.jar:2.2.0]
>> at play.api.db.BoneCPPlugin$$anonfun$onStart$1.apply(DB.scala:252) ~[play-jdbc_2.10-2.2.0.jar:2.2.0]

I want result :

[12:43:16.454] play.api.Configuration$$anon$1: Configuration error[Cannot connect to database [default]]
[12:43:16.454]    at play.api.Configuration$.play$api$Configuration$$configError(Configuration.scala:92) ~[play_2.10-2.2.0.jar:2.2.0]
[12:43:16.454]    at play.api.Configuration.reportError(Configuration.scala:570) ~[play_2.10-2.2.0.jar:2.2.0]
[12:43:16.454]    at play.api.db.BoneCPPlugin$$anonfun$onStart$1.apply(DB.scala:252) ~[play-jdbc_2.10-2.2.0.jar:2.2.0]
:
:
more 40 lines
:
[12:43:16.454]    at play.api.db.BoneCPPlugin$$anonfun$onStart$1.apply(DB.scala:252) ~[play-jdbc_2.10-2.2.0.jar:2.2.0]

IMPORTANT!
Same Time Print

I want know.
How to change logback.xml?

Upvotes: 1

Views: 2704

Answers (1)

Evgenii Beschastnov
Evgenii Beschastnov

Reputation: 430

You cannot do this by changing only logback.xml, because ReplacingCompositeConverter, which is called by %replace, uses only static strings as replacements (no dates or any other conversion words of PatternLayout).

To reach your goal you should create custom converter (if you'll use mine, notice that it should be placed in ch.qos.logback.core.pattern package)

package ch.qos.logback.core.pattern;

import ch.qos.logback.classic.PatternLayout;

import ch.qos.logback.core.pattern.parser.Node;
import ch.qos.logback.core.pattern.parser.Parser;
import ch.qos.logback.core.spi.ScanException;

public class ReplacingAndParsingCompositeConverter<E> extends ReplacingCompositeConverter<E> {

    @Override
    protected String transform(E event, String in) {
        if (!started) {
            return in;
        }

        String parsedReplacement;

        try {
            Parser<E> p = new Parser<E>(replacement);
            p.setContext(getContext());
            Node t = p.parse();
            Converter<E> c = p.compile(t, PatternLayout.defaultConverterMap);
            ConverterUtil.setContextForConverters(getContext(), c);
            ConverterUtil.startConverters(c);
            StringBuilder buf = new StringBuilder();
            while (c != null) {
                c.write(buf, event);
                c = c.getNext();
            }
            parsedReplacement = buf.toString();
        } catch (ScanException e) {
            e.printStackTrace();
            parsedReplacement = replacement;
        }
        return pattern.matcher(in).replaceAll(parsedReplacement);
    }
}

Then you should declare this converter in logback.xml with <conversionRule/> and use it instead of old %replace.

<configuration ...>
    <conversionRule conversionWord="replaceAndParse" converterClass="ch.qos.logback.core.pattern.ReplacingAndParsingCompositeConverter" />

    <appender name="FILEOUT3" class="ch.qos.logback.core.FileAppender">
        <file>D:/${byDay}.log</file>
        <append>true</append>
        <encoder>
            <Pattern>[%d{HH:mm:ss.SSS}] %-5level %msg%replaceAndParse(%xException){"(\r?\n)", "$1[%d{HH:mm:ss.SSS}]"}%nopex%n</Pattern>
        </encoder>
    </appender>
    ....
</configuration>

Upvotes: 2

Related Questions