...
Log
...
Component
...
The
...
log:
...
component
...
logs
...
message
...
exchanges
...
to
...
the
...
underlying
...
logging
...
mechanism.
Include Page | ||||
---|---|---|---|---|
|
URI format
Code Block |
---|
{include:Uses Commons Logging} h3. URI format {code} log:loggingCategory[?options] {code} Where *loggingCategory* is the name of the logging category to use. You can append query options to the URI in the following format, {{ |
Where loggingCategory
is the name of the logging category to use. You can append query options to the URI in the following format, ?option=value&option=value&...
Info | ||
---|---|---|
| ||
As of Camel 2.12.4/2.13.1, if there's single instance of |
For example, a log endpoint typically specifies the logging level using the level
option, as follows:
Code Block |
---|
.}} For example, a log endpoint typically specifies the logging level using the {{level}} option, as follows: {code} log:org.apache.camel.example?level=DEBUG {code} |
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.
Tip | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
| =
|
|
|
|
| |||||||
There is also a |
Options
Div | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||||||||||||||||||||||||
|
Tip |
---|
|
Formatting
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:
Div | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Tip | ||
---|---|---|
| ||
For older versions of Camel that do not support the
|
Regular Logger Example
In the route below we log the incoming orders at DEBUG
level before the order is processed:
Code Block | ||||
---|---|---|---|---|
| ||||
{code} {tip} h3. Regular logger sample In the route below we log the incoming orders at {{DEBUG}} level before the order is processed: {code:java} from("activemq:orders") .to("log:com.mycompany.order?level=DEBUG") .to("bean:processOrder"); {code} |
Or
...
using
...
Spring
...
XML:
Code Block | ||||
---|---|---|---|---|
| ||||
to define the route: {code:xml} <route> <from uri="activemq:orders"/> <to uri="log:com.mycompany.order?level=DEBUG"/> <to uri="bean:processOrder"/> </route> {code} h3. Regular logger with formatter sample In the route below we log the incoming orders at {{INFO}} level before the order is processed. {code:java} |
Regular Logger with Formatter
In the route below we log the incoming orders at INFO
level before the order is processed.
Code Block | ||||
---|---|---|---|---|
| ||||
from("activemq:orders"). .to("log:com.mycompany.order?showAll=true&multiline=true") .to("bean:processOrder"); {code} h3. Throughput logger with groupSize sample In the route below we log the throughput of the incoming orders at {{DEBUG}} level grouped by 10 messages. {code:java} |
Throughput Logger With groupSize
In the route below we log the throughput of the incoming orders at DEBUG
level grouped by 10 messages.
Code Block | ||||
---|---|---|---|---|
| ||||
from("activemq:orders"). .to("log:com.mycompany.order?level=DEBUG&groupSize=10") .to("bean:processOrder"); {code} h3. 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. {code:java} |
Throughput Logger With groupInterval
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.
Code Block | ||||
---|---|---|---|---|
| ||||
from("activemq:orders") . to("log:com.mycompany.order?level=DEBUG&groupInterval=10000&groupDelay=60000&groupActiveOnly=false") .to("bean:processOrder"); {code} |
The
...
following
...
will
...
be
...
logged:
Code Block |
---|
} "Received: 1000 new messages, with total 2000 so far. Last group took: 10000 millis which is: 100 messages per second. average: 100" {code} h3. Full customization of 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: {code} |
Full Customization of the Logged 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:
Code Block |
---|
Exchange[Id:ID-machine-local-50656-1234567901234-1-2, ExchangePattern:InOut, Properties:{CamelToEndpoint=log://org.apache.camel.component.log.TEST?showAll=true, CamelCreatedTimestamp=Thu Mar 28 00:00:00 WET 2013}, Headers:{breadcrumbId=ID-machine-local-50656-1234567901234-1-1}, BodyType:String, Body:Hello World, Out: null] {code} |
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 one of two ways:
Explicitly instantiating the LogComponent
in your Registry
Code Block | ||
---|---|---|
| ||
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}}|http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/spi/ExchangeFormatter.html] 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:* {code} <bean name="log" class="org.apache.camel.component.log.LogComponent"> <property name="exchangeFormatter" ref="myCustomFormatter" /> </bean> {code} *Configuration by convention:* Simply by registering a bean with the name {{logFormatter}}; the Log Component is intelligent enough to pick it up automatically. {code} |
Convention Over Configuration
Simply by registering a bean with the name logFormatter
; the Log Component is intelligent enough to pick it up automatically.
Code Block | ||
---|---|---|
| ||
<bean name="logFormatter" class="com.xyz.MyCustomExchangeFormatter" /> {code} NOTE: the {{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. {include:Endpoint See Also} * [Tracer] * [How do I use log4j] * [How do I use Java 1.4 logging] * [LogEIP] for using {{log}} directly in the DSL for human logs. |
Tip |
---|
The ExchangeFormatter gets applied to all Log endpoints within that Camel Context. If you need a different ExchangeFormatter for each endpoint, 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: 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.
Example:
Code Block | ||
---|---|---|
| ||
<bean name="logFormatter" class="com.xyz.MyCustomExchangeFormatter" scope="prototype"/>
|
And then we can have Camel routes using the log URI with different options:
Code Block | ||
---|---|---|
| ||
<to uri="log:foo?param1=foo&param2=100"/>
<!-- ... -->
<to uri="log:bar?param1=bar&param2=200"/>
|
Using Log Component in OSGi
Improvements from 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 camel-core
bundle.
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 a single instance of org.slf4j.Logger
in the Registry or reference it using logger
URI parameter.
Include Page | ||||
---|---|---|---|---|
|
- Tracer
- How do I use log4j
- How do I use Java 1.4 logging
- LogEIP for using
log
directly in the DSL for human logs.