Using Camel Log component

Camel includes out of the box a Log Component that is useful for debugging, making easy to trace the content of the messages flowing through your routes. Typically this component is used just temporarily when developing your routes and it’s expected to be removed in production.

Let’s see an example of how to use it to log a Timer action:

package com.sample;

 
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.main.Main;

 
public class Logger {

    public static void main(String[] args) throws Exception {
        Main main = new Main();
        main.enableHangupSupport();
        main.addRouteBuilder(new RouteLogger());
        main.run(args);
    }
}

class RouteLogger extends RouteBuilder {

    @Override
    public void configure() throws Exception {

       from("timer://simpleTimer?period=1000")
                            .setBody(simple("Hello from timer at ${header.firedTime}"))
                                     .to("log:myLog");

    }
}

The resulting output will be:

[hread #0 - timer://simpleTimer] myLog       INFO  Exchange[ExchangePattern: InOnly, BodyType: String, Body: Hello from timer at Wed Aug 19 15:43:31 CEST 2015]
[hread #0 - timer://simpleTimer] myLog       INFO  Exchange[ExchangePattern: InOnly, BodyType: String, Body: Hello from timer at Wed Aug 19 15:43:32 CEST 2015]
[hread #0 - timer://simpleTimer] myLog       INFO  Exchange[ExchangePattern: InOnly, BodyType: String, Body: Hello from timer at Wed Aug 19 15:43:33 CEST 2015]
. . . . . .
[hread #0 - timer://simpleTimer] myLog       INFO  Exchange[ExchangePattern: InOnly, BodyType: String, Body: Hello from timer at Wed Aug 19 15:44:02 CEST 2015]

Please note that the Log Component, by default, will log at the INFO logging level,

Found the article helpful? if so please follow us on Socials