Skip to content

Commit

Permalink
CAMEL-20459: documentation fixes for the log EIP.
Browse files Browse the repository at this point in the history
Signed-off-by: Otavio R. Piske <[email protected]>
  • Loading branch information
orpiske committed Feb 25, 2024
1 parent 452e960 commit d778e5f
Showing 1 changed file with 62 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ How can I log the processing of a xref:message.adoc[Message]?
Camel provides many ways to log the fact that you are processing a message. Here are just a few examples:

* You can use the xref:ROOT:log-component.adoc[Log] component which logs the Message content.
* You can use the xref:manual::tracer.adoc[Tracer] which trace logs message flow.
* You can use the xref:manual::tracer.adoc[Tracer] that traces logs message flow.
* You can also use a xref:manual::processor.adoc[Processor] or xref:manual::bean-binding.adoc[Bean] and log from Java code.
* You can use this log EIP.
Expand All @@ -32,7 +32,7 @@ include::partial$eip-exchangeProperties.adoc[]
This log EIP is much lighter and meant for logging human logs such as `Starting to do ...` etc.
It can only log a message based on the xref:languages:simple-language.adoc[Simple] language.

The xref:ROOT:log-component.adoc[log] component is meant for logging the message content (body, headers, etc.).
The xref:ROOT:log-component.adoc[log] component is meant for logging the message content (body, headers, etc).
There are many options on the log component to configure what content to log.

== Example
Expand All @@ -41,15 +41,21 @@ You can use the log EIP which allows you to use xref:languages:simple-language.a

For example, you can do

[tabs]
====
Java::
+
[source,java]
----
from("direct:start")
.log("Processing ${id}")
.to("bean:foo");
----
And in XML:

XML::
+
[source,xml]
----
<route>
Expand All @@ -59,14 +65,16 @@ And in XML:
</route>
----
====

This will be evaluated using the xref:languages:simple-language.adoc[Simple]
to construct the `String` containg the message to be logged.
to construct the `String` containing the message to be logged.

=== Logging message body with streaming

If the message body is stream based, then logging the message body, may cause the message body to be empty afterwards. See this xref:manual:faq:why-is-my-message-body-empty.adoc[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.
If the message body is stream based, then logging the message body may cause the message body to be empty afterward. See this xref:manual:faq:why-is-my-message-body-empty.adoc[FAQ]. For streamed messages, you can use Stream caching to allow logging the message body and be able to read the message body afterward again.

The log DSL have overloaded methods to set the logging level and/or name as well.
The log DSL has overloaded methods to set the logging level and/or name as well.
[source,java]
----
from("direct:start")
Expand All @@ -90,15 +98,15 @@ from("direct:start")
.to("bean:foo");
----

For example you can use this to log the file name being processed if you consume files.
For example, you can use this to log the file name being processed if you consume files.
[source,java]
----
from("file://target/files")
.log(LoggingLevel.DEBUG, "Processing file ${file:name}")
.to("bean:foo");
----

In XML DSL it is also easy to use log DSL as shown below:
In XML DSL, it is also easy to use log DSL as shown below:
[source,xml]
----
<route id="foo">
Expand Down Expand Up @@ -142,14 +150,20 @@ So for example, if you have not assigned an id to the route, then Camel will use

To use "fooRoute" as the route id, you can do:

[tabs]
====
Java::
+
[source,java]
----
from("direct:start").routeId("fooRoute")
.log("Processing ${id}")
.to("bean:foo");
----
And in XML:
XML::
[source,xml]
----
Expand All @@ -160,31 +174,39 @@ And in XML:
</route>
----
====

TIP: If you enable `sourceLocationEnabled=true` on `CamelContext` then Camel will use source file:line as logger name,
instead of the route id. This is for example what `camel-jbang` do, to make it easy to see where in the source code the log is located.

==== Using custom logger from the Registry

If the Log EIP has not been configured with a specific logger to use,
then Camel will will lookup in the xref:manual::registry.adoc[Registry]
then Camel will look up in the xref:manual::registry.adoc[Registry]
if there is a single instance of `org.slf4j.Logger`.

If such an instance exists then this logger is used,
If such an instance exists, then this logger is used
if not the behavior defaults to creating a new instance of logger.

=== Configuring logging name globally

You can configure a global log name that is used instead of the route id,
by setting the global option on the `CamelContext`.

In Java you can do:
In Java, you can do:

[tabs]
====
Java::
+
[source,java]
----
camelContext.getGlobalOptions().put(Exchange.LOG_EIP_NAME, "com.foo.myapp");
----
And in XML:
XML::
[source,xml]
----
Expand All @@ -195,28 +217,45 @@ And in XML:
</camelContext>
----
====

== Masking sensitive information like password

You can enable security masking for logging by setting `logMask` flag to `true`.
Note that this option also affects xref:ROOT:log-component.adoc[Log] component.
Note that this option also affects the xref:ROOT:log-component.adoc[Log] component.

To enable mask in Java DSL at CamelContext level:

[tabs]
====
Java::
+
[source,java]
----
camelContext.setLogMask(true);
----
And in XML you set the option on `<camelContext>`:
XML::
+
.And in XML you set the option on `<camelContext>`:
[source,xml]
----
<camelContext logMask="true">
</camelContext>
----
====

You can also turn it on|off at route level. To enable mask in Java DSL at route level:
You can also turn it on|off at route level. To enable mask in at route level:

[tabs]
====
Java::
+
[source,java]
----
Expand All @@ -225,24 +264,26 @@ from("direct:start").logMask()
.to("bean:foo");
----
And in XML:

XML::
+
[source,xml]
----
<route logMask="true">
</route>
----
====

=== Using custom masking formatter

`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`.

The know set of keywords to mask is gathered from all the different component options, that are marked as secret.
The know set of keywords to mask is gathered from all the different component options that are marked as secret.
The list is generated into the source code in `org.apache.camel.util.SensitiveUtils`.
At this time of writing there is more than 65 different keywords.
At this time of writing, there are more than 65 different keywords.

Custom keywords can be added as shown:

Expand Down

0 comments on commit d778e5f

Please sign in to comment.