How can you set the logger (log category / log name) in Camel's onException clause?

1.3k Views Asked by At

I have the following route:

            onException(Exception.class)
                .logExhausted(true)
                .logStackTrace(true)
                .logExhaustedMessageHistory(true)
                .process(new MyErrorProcessor());

            from(uri)
            .process(new MyProcessor());

When an error occurs the logs are printed in the org.apache.camel.processor.DefaultErrorHandler log category. Is there a way to change this to a custom log category? .errorHandler() allows you to set a log category but .onException() doesn't seem to allow it.

Thanks.

3

There are 3 best solutions below

1
Sergey On

You can try

.onException(Exception.class)
            .to("log:logger_name?level=ERROR&multiline=true&showCaughtException=true&showStackTrace=true")
...
0
error1009 On

You can try something like this:

onException(Exception.class)
                .handled(true)
                .log(LoggingLevel.ERROR, this.getClass().getSimpleName(),
                        "${exception.stacktrace} ${messageHistory(false)}");

You can also look up for more variables here: https://camel.apache.org/manual/latest/simple-language.html

0
Jimothy On

By default, the route ID is used as the logger category. Therefore, set the route ID to class name, and you can then configure logging in the usual way:

from(uri)
    .id(this.getClass().getName())
    .process(new MyProcessor());

(id may have been named routeId in Camel 2; the above works for Camel 3).