The log: component logs message exchanges to the underlying logging mechanism.
Camel uses sfl4j which allows you to configure logging via, among others:
Where loggingCategory is the name of the logging category to use. You can append query options to the URI in the following format,
For example, a log endpoint typically specifies the logging level using the
level option, as follows:
The default logger logs every exchange (regular logging). But Camel also ships with the
Throughput logger, which is used whenever the
groupSize option is specified.
Logging level to use. Possible values:
Camel 2.9: An optional Marker name to use.
An integer that specifies a group size for throughput logging.
If specified will group message stats by this time interval (in millis)
Set the initial delay for stats (in millis)
If true, will hide stats when no new messages have been received for a time interval, if false, show stats regardless of message traffic
Camel 2.12.4/2.13.1: An optional reference to org.slf4j.Logger from Registry to use.
note: groupDelay and groupActiveOnly are only applicable when using groupInterval
The log formats the execution of exchanges to log lines.
By default, the log uses
LogFormatter to format the log output, where
LogFormatter has the following options:
Quick option for turning all options on. (multiline, maxChars has to be manually set if to be used)
Show the unique exchange ID.
Shows the Message Exchange Pattern (or MEP for short).
Show the exchange properties.
Show the In message headers.
Camel 2.12.2: Whether to skip line separators when logging the message body. This allows to log the message body in one line, setting this option to
false will preserve any line separators from the body, which then will log the body as is.
Show the In body Java type.
Show the In body.
If the exchange has an Out message, show the Out message.
If the exchange has an exception, show the exception message (no stack trace).
If the exchange has a caught exception, show the exception message (no stack trace). A caught exception is stored as a property on the exchange (using the key
Exchange.EXCEPTION_CAUGHT) and for instance a
doCatch can catch exceptions. See Try Catch Finally.
Show the stack trace, if an exchange has an exception. Only effective if one of
showCaughtException are enabled.
Camel 2.9: Whether Camel should show file bodies or not (eg such as java.io.File).
Whether Camel should show
java.util.concurrent.Future bodies or not. If enabled Camel could potentially wait until the
Future task is done. Will by default not wait.
Camel 2.8: Whether Camel should show stream bodies or not (eg such as java.io.InputStream). Beware if you enable this option then you may not be able later to access the message body as the stream have already been read by this logger. To remedy this you will have to use Stream caching.
true, each piece of information is logged on a new line.
Limits the number of characters logged per line. The default value is
10000 from Camel 2.9 onwards.
Regular logger sample
In the route below we log the incoming orders at
DEBUG level before the order is processed:
Or using Spring XML to define the route:
Regular logger with formatter sample
In the route below we log the incoming orders at
INFO level before the order is processed.
Throughput logger with groupSize sample
In the route below we log the throughput of the incoming orders at
DEBUG level grouped by 10 messages.
Throughput logger with groupInterval sample
This route will result in message stats logged every 10s, with an initial 60s delay and stats should be displayed even if there isn't any message traffic.
The following will be logged:
Full customization of the logging output
Available as of Camel 2.11
With the options outlined in the #Formatting section, you can control much of the output of the logger. However, log lines will always follow this structure:
This format is unsuitable in some cases, perhaps because you need to...
- ... filter the headers and properties that are printed, to strike a balance between insight and verbosity.
- ... adjust the log message to whatever you deem most readable.
- ... tailor log messages for digestion by log mining systems, e.g. Splunk.
- ... print specific body types differently.
- ... etc.
Whenever you require absolute customization, you can create a class that implements the
ExchangeFormatter interface. Within the
format(Exchange) method you have access to the full Exchange, so you can select and extract the precise information you need, format it in a custom manner and return it. The return value will become the final log message.
You can have the Log component pick up your custom
ExchangeFormatter in either of two ways:
Explicitly instantiating the LogComponent in your Registry:
Convention over configuration:
Simply by registering a bean with the name
logFormatter; the Log Component is intelligent enough to pick it up automatically.
ExchangeFormatter gets applied to all Log endpoints within that Camel Context. If you need different ExchangeFormatters for different endpoints, just instantiate the LogComponent as many times as needed, and use the relevant bean name as the endpoint prefix.
From Camel 2.11.2/2.12 onwards when using a custom log formatter, you can specify parameters in the log uri, which gets configured on the custom log formatter. Though when you do that you should define the "logFormatter" as prototype scoped so its not shared if you have different parameters, eg:
And then we can have Camel routes using the log uri with different options:
Using Log component in OSGi
Improvement as of Camel 2.12.4/2.13.1
When using Log component 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. Without specifying custom
org.sfl4j.Logger instance, the logger created by Log component is associated with
In some scenarios it is required that the bundle associated with logger should be the bundle which contains route definition. To do this, either register single instance of
org.slf4j.Logger in the Registry or reference it using
logger URI parameter.