Camel Components

Log Data

Since Camel 1.1

Only producer is supported

The Log component logs message exchanges to the underlying logging mechanism.

Camel uses SLF4J, which allows you to configure logging via, among others:

  • Log4j

  • Logback

  • Java Util Logging

URI format

log:loggingCategory[?options]

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&…​

Using Logger instance from the Registry

If there’s single instance of org.slf4j.Logger found in the Registry, the loggingCategory is no longer used to create logger instance. The registered instance is used instead. Also, it is possible to reference particular Logger instance using ?logger=#myLogger URI parameter. Eventually, if there’s no registered and URI logger parameter, the logger instance is created using loggingCategory.

For example, a log endpoint typically specifies the logging level using the level option, as follows:

log:org.apache.camel.example?level=DEBUG

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.

There is also a log directly in the DSL, but it has a different purpose. It’s meant for lightweight and human logs. See more details at LogEIP.

Configuring Options

Camel components are configured on two separate levels:

  • component level

  • endpoint level

Configuring Component Options

At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.

For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.

Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.

You can configure components using:

  • the Component DSL.

  • in a configuration file (application.properties, *.yaml files, etc).

  • directly in the Java code.

Configuring Endpoint Options

You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.

Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.

A good practice when configuring options is to use Property Placeholders.

Property placeholders provide a few benefits:

  • They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.

  • They allow externalizing the configuration from the code.

  • They help the code to become more flexible and reusable.

The following two sections list all the options, firstly for the component followed by the endpoint.

Component Options

The Log Data component supports the following options which are listed below.

Name Description Default Type

lazyStartProducer (producer)

Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing.

false

boolean

sourceLocationLoggerName (producer)

Whether the log endpoints use the source location of the endpoint in the route, the file or class name and the line number, as the logger name instead of loggerName. The sourceLocationLoggerName option of an endpoint replaces this option.

false

boolean

autowiredEnabled (advanced)

Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc.

true

boolean

exchangeFormatter (advanced)

Autowired A custom org.apache.camel.spi.ExchangeFormatter for all log endpoints of this component. When set, the formatting options of the endpoints are not used. A bean named logFormatter in the registry takes precedence, and the exchangeFormatter option of an endpoint replaces both.

ExchangeFormatter

Endpoint Options

The Log Data endpoint is configured using URI syntax:

log:loggerName

With the following path and query parameters:

Path Parameters

Name Description Default Type

loggerName (producer)

Required The name of the SLF4J logger, also called the logging category. It is not used when an org.slf4j.Logger is given, by the logger URI parameter or as the only Logger bean in the registry, or when sourceLocationLoggerName resolves a name.

String

Query Parameters

Name Description Default Type

groupActiveOnly (producer)

Whether the groupInterval throughput logging skips an interval in which no new exchanges arrived. If false, it logs the statistics every interval.

true

boolean

groupDelay (producer)

The delay, in milliseconds, before the groupInterval throughput logging logs its first statistics. If not set, 1000 is used.

Long

groupInterval (producer)

Turns on throughput logging: instead of each exchange, the endpoint logs statistics (the number of new and total exchanges and the messages per second) every interval, in milliseconds, from a background thread. It is not used when groupSize is set.

Long

groupSize (producer)

Turns on throughput logging: instead of each exchange, the endpoint logs statistics (the number of exchanges and the messages per second) after every groupSize exchanges. It takes precedence over groupInterval, and the formatting options are not used.

Integer

level (producer)

The logging level of the log messages. OFF logs nothing.

Enum values:

  • TRACE

  • DEBUG

  • INFO

  • WARN

  • ERROR

  • OFF

INFO

String

logMask (producer)

Whether to mask sensitive values, such as passwords and passphrases, in the log message. If not set, the logMask setting of the route, or of the CamelContext, is used. A org.apache.camel.spi.MaskingFormatter bean named CamelCustomLogMask in the registry replaces the default masking.

Boolean

marker (producer)

The name of an SLF4J marker to add to each log message.

String

plain (producer)

Whether to log only the body, without the Exchange[…​] wrapper and the other parts. The stream, file and maxChars options still apply to the body.

false

boolean

sourceLocationLoggerName (producer)

Whether to use the source location of the log endpoint in the route, the file or class name and the line number, as the logger name instead of loggerName. If the source location is not known, for example because source location is not enabled, loggerName is used.

false

boolean

lazyStartProducer (producer (advanced))

Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing.

false

boolean

exchangeFormatter (advanced)

A custom org.apache.camel.spi.ExchangeFormatter that turns the exchange into the log message. When set, the formatting options of the endpoint are not used. If not set, the exchangeFormatter of the component, or a bean named logFormatter in the registry, is used.

ExchangeFormatter

maxChars (formatting)

The maximum number of characters to log. A longer body is cut at this length, and so is a longer log message, or each line of it with multiline. The global option CamelLogDebugBodyMaxChars, when set, replaces this limit for the body.

10000

int

multiline (formatting)

Whether to log each part of the exchange on its own line.

false

boolean

showAll (formatting)

Show all parts of the exchange: the ID, the route group and route ID, the exchange pattern, all properties, the variables, the headers, the body type and body, the exception or caught exception, and its stack trace. It does not change multiline, maxChars, or the stream, file and Future options.

false

boolean

showAllProperties (formatting)

Show all of the exchange properties (both internal and custom).

false

boolean

showBody (formatting)

Show the message body.

true

boolean

showBodyType (formatting)

Show the body Java type.

true

boolean

showCachedStreams (formatting)

Whether to log a body that is an org.apache.camel.StreamCache. The stream cache is reset after it is read, so the body can be read again later. If false, a placeholder is logged instead, unless showStreams is true.

true

boolean

showCaughtException (formatting)

Show the type and message of the exception of the exchange or, if it has none, of the caught exception (not the stack trace). Camel stores a caught exception in the CamelExceptionCaught exchange property, for example when doCatch catches it or onException handles it.

false

boolean

showException (formatting)

If the exchange has an exception, show the type and message of the exception (not the stack trace).

false

boolean

showExchangeId (formatting)

Show the unique exchange ID.

false

boolean

showExchangePattern (formatting)

Show the exchange pattern (InOnly or InOut). When none of the formatting options is set, the endpoint uses the default output of the component, which shows the exchange pattern anyway.

false

boolean

showFiles (formatting)

Whether to log the content of a body that is a file, such as a java.io.File, a java.nio.file.Path or a file of the file or ftp components. If false, a placeholder with the file name is logged instead.

false

boolean

showFuture (formatting)

Whether to wait for a java.util.concurrent.Future body to complete and log its result. If false, the toString() of the Future is logged.

false

boolean

showHeaders (formatting)

Show the message headers.

false

boolean

showProperties (formatting)

Show the exchange properties (only custom). Use showAllProperties to show both internal and custom properties.

false

boolean

showRouteGroup (formatting)

Show the group of the route, if the route has one.

false

boolean

showRouteId (formatting)

Show route ID.

false

boolean

showStackTrace (formatting)

Show the stack trace of the exception that showException, showCaughtException or showAll shows. It has no effect without one of them.

false

boolean

showStreams (formatting)

Whether to log a body that is a stream, such as a java.io.InputStream or a java.io.Reader, or a stream cache. If false, a placeholder is logged instead. Beware that logging reads the stream, so later steps may not be able to read the body; use stream caching to avoid this.

false

boolean

showVariables (formatting)

Show the variables of the exchange, if it has any.

false

boolean

skipBodyLineSeparator (formatting)

Whether to remove the line separators of the operating system from the body, so the body is logged on one line. If false, the body is logged as is.

true

boolean

style (formatting)

The layout of the parts of the log message. Default separates them with a comma, as Label: value. Tab separates them with a tab, as Label: value. Fixed puts each value after a tab and the label padded to 20 characters.

Enum values:

  • Default

  • Tab

  • Fixed

Default

OutputStyle

Message Body

The message body at each side of the Log Data component: Consumer is the body of the messages the consumer creates, Consumer reply the bodies a consumer that answers its caller can reply with, Producer input the bodies the producer can send or write, and Producer output the body after the producer.

Side Body Description

Producer input

any

Producer output

unchanged

The bodies are the ones with the default options. A side that is not listed is not known, and tooling does not guess it. The carriers, the sides and the tools that use them are described in Message Body Types.

Examples

Regular logger example

In the route below we log the incoming orders at DEBUG level before the order is processed:

  • Java

  • XML

  • YAML

from("activemq:orders")
    .to("log:com.mycompany.order?level=DEBUG")
    .to("bean:processOrder");
<route>
  <from uri="activemq:orders"/>
  <to uri="log:com.mycompany.order?level=DEBUG"/>
  <to uri="bean:processOrder"/>
</route>
- route:
    from:
      uri: activemq:orders
      steps:
        - to:
            uri: log:com.mycompany.order
            parameters:
              level: DEBUG
        - to:
            uri: bean:processOrder

Regular logger with formatter example

In the route below we log the incoming orders at INFO level before the order is processed.

  • Java

  • XML

  • YAML

from("activemq:orders").
    to("log:com.mycompany.order?showAll=true&multiline=true").to("bean:processOrder");
<route>
  <from uri="activemq:orders"/>
  <to uri="log:com.mycompany.order?showAll=true&amp;multiline=true"/>
  <to uri="bean:processOrder"/>
</route>
- route:
    from:
      uri: activemq:orders
      steps:
        - to:
            uri: log:com.mycompany.order
            parameters:
              showAll: true
              multiline: true
        - to:
            uri: bean:processOrder

Throughput logger with groupSize example

In the route below we log the throughput of the incoming orders at DEBUG level grouped by 10 messages.

  • Java

  • XML

  • YAML

from("activemq:orders").
    to("log:com.mycompany.order?level=DEBUG&groupSize=10").to("bean:processOrder");
<route>
  <from uri="activemq:orders"/>
  <to uri="log:com.mycompany.order?level=DEBUG&amp;groupSize=10"/>
  <to uri="bean:processOrder"/>
</route>
- route:
    from:
      uri: activemq:orders
      steps:
        - to:
            uri: log:com.mycompany.order
            parameters:
              level: DEBUG
              groupSize: 10
        - to:
            uri: bean:processOrder

Throughput logger with groupInterval example

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.

  • Java

  • XML

  • YAML

from("activemq:orders").
    to("log:com.mycompany.order?level=DEBUG&groupInterval=10000&groupDelay=60000&groupActiveOnly=false").to("bean:processOrder");
<route>
  <from uri="activemq:orders"/>
  <to uri="log:com.mycompany.order?level=DEBUG&amp;groupInterval=10000&amp;groupDelay=60000&amp;groupActiveOnly=false"/>
  <to uri="bean:processOrder"/>
</route>
- route:
    from:
      uri: activemq:orders
      steps:
        - to:
            uri: log:com.mycompany.order
            parameters:
              level: DEBUG
              groupInterval: 10000
              groupDelay: 60000
              groupActiveOnly: false
        - to:
            uri: bean:processOrder

The following will be logged:

"Received: 1000 new messages, with total 2000 so far. Last group took: 10000 millis which is: 100 messages per second. average: 100"

Masking sensitive information like password

You can enable security masking for logging by setting logMask flag to true. Note that this option also affects Log EIP.

To enable mask in Java DSL at CamelContext level:

Java-only: enabling log masking on CamelContext
camelContext.setLogMask(true);

And in XML:

XML-only: enabling log masking on `CamelContext`
<camelContext logMask="true">

You can also turn it on|off at endpoint level. To enable mask in Java DSL at endpoint level, add logMask=true option in the URI for the log endpoint:

  • Java

  • XML

  • YAML

from("direct:start").to("log:foo?logMask=true");
<route>
  <from uri="direct:start"/>
  <to uri="log:foo?logMask=true"/>
</route>
- route:
    from:
      uri: direct:start
      steps:
        - to:
            uri: log:foo
            parameters:
              logMask: true

org.apache.camel.support.processor.DefaultMaskingFormatter is used for the masking by default. If you want to use a custom masking formatter, put it into registry with the name CamelCustomLogMask. Note that the masking formatter must implement org.apache.camel.spi.MaskingFormatter.

Full customization of the logging output

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:

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]

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:

XML-only: Spring bean declaration for custom LogComponent with `ExchangeFormatter`
<bean name="log" class="org.apache.camel.component.log.LogComponent">
   <property name="exchangeFormatter" ref="myCustomFormatter" />
</bean>

Convention over configuration:*

Simply by registering a bean with the name logFormatter; the Log Component is intelligent enough to pick it up automatically.

XML-only: Spring bean registration for auto-detected `ExchangeFormatter`
<bean name="logFormatter" class="com.xyz.MyCustomExchangeFormatter" />

The ExchangeFormatter gets applied to all Log endpoints within that Camel Context. If you need different ExchangeFormatters for different endpoints, instantiate the LogComponent as many times as needed, and use the relevant bean name as the endpoint prefix.

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 it’s not shared if you have different parameters, e.g.:

XML-only: Spring bean with prototype scope for per-endpoint `ExchangeFormatter`
<bean name="logFormatter" class="com.xyz.MyCustomExchangeFormatter" scope="prototype"/>

And then we can have Camel routes using the log uri with different options:

XML-only: example <to> elements showing different log endpoint URIs
<to uri="log:foo?param1=foo&amp;param2=100"/>

<to uri="log:bar?param1=bar&amp;param2=200"/>