Apache camel bindy - how to log or debug

2019-08-10 08:15发布

问题:

I'm trying to get some log informations from camel bindy. I had a working setup using an BindyCsvDataFormat bindyProduct with an annotated product-bean and an csv file as source.

Now I changed the CSV file and the annotated bean. The processing seams to get stuck within the bindy processor, but I do not get any Informations/logs. My debugProcessor is not reached at all. If I put it before the unmarshal step, then it logs some stuff and I can debug into it. I wonder why the new files do not fit / match any more and why there are no logs OR exceptions or whatever would be of an help.

        from("file:csv-testdata")
        .unmarshal(bindyProduct)    
        .process(debugProcessor)

Thanks in advance AJ

回答1:

Logging of Exceptions in apache camel

When I asked the question, I was at the very beginning of my camel riding experience. In the meantime I found out some great ways to to achieve, what I was looking for. So if someone else is searching for an answer to this question:

1. First approach:

Enable logging for your project, configure it properly and and process logging by adding logging URLs to your route.

1.1 Add dependencies for logging to your pom.xml e.g

<groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.5</version>
</dependency>

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.5</version>
</dependency>

1.2 Add log4j.properties file to your src/main/resources folder (if you want to use log4j). Define loglevel, appenders etc. there

1.3 add the logging to your route

from("...")
.unmarshal(bindyProduct)
.to("log:com.your.package.YourChosenName?level=ERROR&multiline=true&showCaughtException=true")
.to(...);

a list of all options can be found here http://camel.apache.org/log.html you can use

&showAll=true 

to log all Informations like headers, properties, body etc

Keep in mind, that the severity of the loglevel of the URLs "level=" option must be equal or higher than the one, you defined in your log4j.properties file

2. Second approach

Define a DebugProcessor that logs the Exception for you and put it in your route right after the processor, that behaves mysteriously

2.1 write an DebugProcessor The Processor has to implement public void process(Exchange exchange) throws Exception {

if your wondering why you can't find any Exception in

exchange.getException()

try retrieving it by using

Exception exception = (Exception) exchange.getProperty(Exchange.EXCEPTION_CAUGHT);

do what ever you like with this exception (log to sysout, ...)

2.2 Wire your debugger into your route within your RouteBuilder Java-Class (or elsewhere, Spring, XML, ...)

DebugProcessor debugProcessor1 = new DebugProcessor();

from("...")
.unmarshal(bindyProduct)
.process(debugProcessor1)
.to(...);

3. Third approach

Use camels .doTry()/.doCatch() or (maybe even better) use an onException() route

(http://camel.apache.org/exception-clause.html / camel.apache.org/try-catch-finally.html)

3.1 Build an onException() route separated from the route you want to debug

The following onException route aggregates exceptions and writes them down a nice, human readable way (via velocity template) , every 5 seconds or 10 exeptions:

onException(Exception.class) // OR a special excepion (io, etc)
        .aggregate(header("CamelFileParent"),
                new ExceptionAggregationStrategy())
                .completionSize(10).completionTimeout(5000)
        .to("velocity:velocity/errors.log.vm")
        .to("file:camel/xml-out?fileName=errors-${file:name.noext}-${date:now:yyyy-MM-dd_HH-mm-ss-SSS}.log");

This is just an example, you can do what ever you want and camel is able to, e.g. send them as mail, put them in a DB etc.

You don't need any extra processors or logging stuff in your route now:

from("...")
.unmarshal(bindyProduct)
.to(...);