Logging meaningful steps within your route

Camel includes a Log EIP that is intended for logging interesting milestones within your integration routes. This is different from logging an error (see the Logging errors recipe in Chapter 7, Error Handling and Compensation) or logging most of the contents of the exchange (see the Debugging using logging recipe). This Log pattern gives you the ability to log a message intended for a human being in order to indicate that something meaningful has occurred within your route. This can include a message being routed a specific way, or that the message contains a value of particular interest.

This recipe will show you how to include explicit steps within your route to externally log significant milestones and data from your integrations.

Getting ready

The Java code for this recipe is located in the org.camelcookbook.monitoring.logeip package. The Spring XML files are located under src/main/resources/META-INF/spring and prefixed with logeip.

How to do it...

In the XML DSL, set the message attribute to a Simple Expression that will be evaluated and its result logged:

<route>
  <from uri="direct:start"/>
  <log message="Something happened - ${body}"/>
  <to uri="mock:result"/>
</route>

In the Java DSL, pass in as the single parameter a Simple Expression that will be evaluated and its result logged:

from("direct:start")
  .log("Something happened - ${body}")
  .to("mock:result");

When a message with a body of "Hello Camel" is passed in, the resulting log should look something like this:

INFO  route3              - Something happened - Hello Camel

How it works...

The Log EIP, by default, will evaluate the provided Simple Expression, and log the results using a logging level of INFO, and using the route ID as the log name. You can override those values, if you wish.

In the XML DSL, this is written as:

<route>
  <from uri="direct:start"/>
  <log loggingLevel="INFO"
       logName="MyLogName"
       message="Something happened - ${body}"/>
  <to uri="mock:result"/>
</route>

In the Java DSL, the same logic is expressed as:

from("direct:start")
  .log(LoggingLevel.INFO,
       "MyLogName",
       "Something happened - ${body}")
  .to("mock:result");

This should result in a log entry that looks like the following:

INFO  MyLogName              - Something happened - Hello Camel

Here is what the log looks like in hawtio. Notice that you can filter log entries by log name:

How it works...

There's more...

Be careful if you are streaming messages and you try to log the body of the message as this will consume the message stream, and the downstream steps will see an empty message. If you need to access the body of a streamed message multiple times, then look to enable stream caching.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.60.62