Using Camel Log component

In this tutorial, we will go through some examples of using the Camel Log component to log messages in your Camel routes.

Setting up the project

To use the Camel Log component in your project, you will need to add the following dependency to your project’s POM file:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-log</artifactId>
    <version>x.x.x</version>
</dependency>

To log a message in a Camel route, you can use the log method and specify the message to log. For example:

from("direct:start")
    .log("Received message: ${body}")
    .to("mock:end");

In the following example, we will check out to use the Log Component in combination with a Timer Component:

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]

By default, the log method will log the message at the INFO level. You can also specify a different log level by using the level option. For example:

from("direct:start")
    .log(LoggingLevel.ERROR, "Error: ${body}")
    .to("mock:end");

This route will log the message as an error.

Logging exchange information

In addition to logging the message body, you can also log other information about the exchange such as the exchange id, the from endpoint, and the to endpoint. To do this, you can use the ${exchange} placeholder in your log message. For example:

from("direct:start")
    .log("Exchange information: ${exchange}")
    .to("mock:end");

This route will log the exchange id, from endpoint, and to endpoint of the exchange.

Customizing the log message

You can also customize the log message by using placeholders in the log message. The following placeholders are available:

  • ${id}: the exchange id
  • ${routeId}: the route id
  • ${exchangeId}: the exchange id
  • ${threadName}: the thread name
  • ${body}: the message body
  • ${headers}: the message headers
  • ${properties}: the exchange properties

For example:

from("direct:start")
    .log("Exchange id: ${id}, message body: ${body}")
    .to("mock:end");

This route will log the exchange id and the message body.

To use a different logger for the Apache Camel Log component, you can use the loggerName option and specify the name of the logger to use. For example:

from("direct:start")
    .log(LoggingLevel.ERROR, "Error: ${body}")
    .to("mock:end")
    .log("Message body: ${body}")
    .loggerName("com.example.CustomLogger")
    .to("mock:end");

This route will log the message as an error using the LoggingLevel.ERROR level and then log the message body using the logger with the name “com.example.CustomLogger”.

Found the article helpful? if so please follow us on Socials
Twitter Icon       Facebook Icon       LinkedIn Icon       Mastodon Icon