How can I log processing a Message?
Camel provides many ways to log processing a message. Here is just some 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
Using log DSL
And 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
Which will construct a String message at runtime using the Simple language. The log message will by logged at
INFO level using the route id as the log name. By default a route is named
route-2 etc. But you can use the
routeId("myCoolRoute") to set a route name of choice.
Difference between log in the DSL and [Log] component
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.
Using 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's a Registry lookup performed to find single instance of
org.slf4j.Logger. If such 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.
Logging message body with streamed messages
The log DSL have overloaded methods to set the logging level and/or name as well.
and to set a logger name
Since Camel 2.12.4/2.13.1 the logger instance may be used as well:
For example you can use this to log the file name being processed if you consume files.
Using log DSL from Spring
In Spring DSL its also easy to use log DSL as shown below:
The log tag has attributes to set the
logName. For example:
Since Camel 2.12.4/2.13.1 it is possible to reference logger instance. For example:
Using slf4j Marker
Available as of Camel 2.9
You can specify a marker name in the DSL
Using log DSL in OSGi
Improvement as of Camel 2.12.4/2.13.1
When using log DSL inside OSGi (e.g., in Karaf), the underlying logging mechanisms are provided by PAX logging. It searches for a bundle which invokes
org.slf4j.LoggerFactory.getLogger() method and associates the bundle with the logger instance. Passing only logger name to log DSL results in associating
camel-core bundle with the logger instance created.
In some scenarios it is required that the bundle associated with logger should be the bundle which contains route definition. This is possible using provided logger instance both for Java DSL and Spring DSL (see the examples above).
Using This Pattern
If you would like to use this EIP Pattern then please read the Getting Started, you may also find the Architecture useful particularly the description of Endpoint and URIs. Then you could try out some of the Examples first before trying this pattern out.