Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: wording

Log

How can I log the processing of a Message?

Camel provides many ways to log the fact that you are processing a message. Here is are just some a few examples:

  • You can use the Log component which logs the Message content.
  • You can use the Tracer which trace logs message flow.
  • You can also use a Processor or Bean and log from Java code.
  • You can use the log DSL.

Using log DSL

And in In Camel 2.2 you can use the log DSL which allows you to use Simple language to construct a dynamic message which gets logged.
For example you can do

Code Block
languagejava
from("direct:start").log("Processing ${id}").to("bean:foo");

...

Info
titleDifference between log in the DSL and [Log] component

The log DSL is much lighter and meant for logging human logs such as Starting to do ... etc. It can only log a message based on the Simple language. On the other hand Log component is a full fledged component which involves using endpoints and etc. The Log component is meant for logging the Message itself and you have many URI options to control what you would like to be logged.

Info
titleUsing Logger instance from the the Registry

As of Camel 2.12.4/2.13.1, if no logger name or logger instance is passed to log DSL, there is a Registry lookup performed to find single instance of org.slf4j.Logger. If such an instance is found, it is used instead of creating a new logger instance. If more instances are found, the behavior defaults to creating a new instance of logger.

Tip
titleLogging message body with streamed messages

If the message body is stream based, then logging the message body, may cause the message body to be empty afterwards. See this FAQ. For streamed messages you can use Stream caching to allow logging the message body and be able to read the message body afterwards again.

...

Using log DSL from Spring

In Spring DSL its it is also easy to use log DSL as shown below:

...

Code Block
java
java
        <bean id="myLogger" class="org.slf4j.LoggerFactory" factory-method="getLogger" xmlns="http://www.springframework.org/schema/beans">
            <constructor-arg value="com.mycompany.mylogger" />
        </bean>

        <route id="moo" xmlns="http://camel.apache.org/schema/spring">
            <from uri="direct:moo"/>
            <log message="Me Got ${body}" loggingLevel="INFO" loggerRef="myLogger"/>
            <to uri="mock:baz"/>
        </route>

Configuring log name globally

Available as of Camel 2.17

By default the log name is the route id. If you want to use a different log name, you would need to configure the logName option. However if you have many logs and you want all of them to use the same log name, then you would need to set that logName option on all of them.

With Camel 2.17 onwards you can configure a global log name that is used instead of the route id, eg

Code Block
CamelContext context = ...
context.getProperties().put(Exchange.LOG_EIP_NAME, "com.foo.myapp");

And in XML

Code Block
xml
xml
<camelContext ...>
  <properties>
    <property key="CamelLogEipName" value="com.foo.myapp"/>
  </properties>

 

Using slf4j Marker

Available as of Camel 2.9

...