Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

W-11984725-logger-revamp-duke #2402

Open
wants to merge 15 commits into
base: v4.4
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 52 additions & 27 deletions modules/ROOT/pages/logger-component-reference.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -6,74 +6,99 @@ endif::[]

This Core component helps you monitor and debug your Mule application by logging important information such as error messages, status notifications, payloads, and so on. You can add a Logger anywhere in a flow, and you can configure it to log a string that you specify, the output of a DataWeave expression you write, or any combination of strings and expressions.

[WARNING]
--
Keep in mind that the Logger is one of the only components that supports mixing Strings and expressions within a value. DataWeave String interpolation or concatenation within a single expression should be used elsewhere.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved to reference

--
//TODO: IS THIS TRUE BEYOND ON-PREM INSTALLATIONS?
The configured messages are logged in a Mule application's log file, which is located in `MULE_HOME/logs/<app-name>.log` if no custom log file path is specified in the `log4j2.xml` file. For details, see xref:logging-and-debugging.adoc[].

The configured messages are logged to the app's log file, which is located in `MULE_HOME/logs/<app-name>.log` if no custom log file path is specified in the `log4j2.xml` file. +
In Studio, the logs show in the Console.
In Anypoint Studio, the logs print to the xref:studio::index.adoc#console[console].

The following example displays the message in Set Payload in a browser and also logs the message.
Copy link
Contributor Author

@dukesphere dukesphere Nov 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed bc "... in a browser": it's in the browser bc of the request to the listener, not directly relevant to logger

The following example logs the message `Hello MuleSoft!`.

image::logger-flow.png[]

.Example: XML Configuration of the Flow
[source,xml,linenums]
----
<http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8081"/>
<flow name="logger-example-Flow">
<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" >
<http:listener-connection host="0.0.0.0" port="8081" />
</http:listener-config><flow name="logger-example-Flow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/"/>
<set-payload value="Hello MuleSoft!"/>
<logger message="#[payload]" level="INFO"/>
</flow>
----

== Logger Configuration
In the Studio console, the logged message, `Hello MuleSoft!` looks like this:

[source,logs]
----
INFO 2022-11-10 19:39:18,222 [[MuleRuntime].uber.01: [w-logger].logger-example-Flow.CPU_LITE @6f862394]
[processor: logger-example-Flow/processors/1; event: 6bd2b350-6172-11ed-9f87-147ddaaf4f97]
org.mule.runtime.core.internal.processor.LoggerMessageProcessor: Hello MuleSoft!
----

* The configurable log level is `INFO`. See <<reference>>.
* A date-time stamp `2022-11-10 19:39:18,222` indicates when the Logger executes.
* CPU_LITE is the See xref:execution-engine.adoc#processing_types[processing type].
* `event: 6bd2b350-6172-11ed-9f87-147ddaaf4f97` identifies the Mule event.
* The message processor `org.mule.runtime.core.internal.processor.LoggerMessageProcessor` prints the message `Hello MuleSoft!`.

[[reference]]
== Reference

image::logger.png[]

[%header%autowidth.spread,cols="a,a,a,a"]
[%header%autowidth.spread,cols="1a,1a,1a,4a"]
|===
| Field | Value | Description | Example
| Field | Attribute | Value | Description

| Message | String or DataWeave expression | Specifies the Mule log message. By default, messages are logged to the application's log file. |
`message="Current payload is #[payload]"`
| *Message* | `message` | String or DataWeave expression | Specifies the Mule log message. By default, messages are logged to the application's log file. Example:
`message="Current payload is #[payload]"`. Note that the Logger is one of the only components that supports this syntax to embed an expression (such as `#[payload]` within a string. Messages in other Mule components require string interpolation or concatenation within a single DataWeave expression.

| Level |
Available options:
| Level | `level` | Available options:

* `DEBUG`
* `ERROR`
* `INFO`(Default)
* `TRACE`
* `WARN` |
Specifies the log level.

|
`level="ERROR"`
Specifies the log level. Example: `level="ERROR"`

| Category | String | Optional setting that specifies a category name that it adds to `log4j2.xml` file. For example, you might use a category to route your log messages based on your category, or you might set log levels based on category. |
| Category | `category` | String | Optional setting that specifies a category name that it adds to `log4j2.xml` file. For example, you might use a category to route your log messages based on your category, or you might set log levels based on category. Example:
`category="MyCustomCategory"`

|===

== Examples

This Logger is set to monitor message processing status, mixing Strings and expressions:
This Logger monitors message processing status, mixing strings with a DataWeave selector expressions:

[source,xml,linenums]
[source,xml]
----
<logger category="monitoring" message="Message #[payload.id] processed successfully" level="INFO"/>
----

This Logger set to record the processing time of a flow, using a single expression and DataWeave's String concatenation:
Assuming a payload such as `{"id" : "100", "some-key" : "some value" }`, the Logger prints the message `Message 100 processed successfully`, for example:

[source,xml,linenums]
[source,xml]
----
<logger category="performance" message="#['Message ' ++ payload.id ++ ' took ' ++ vars.processingTime ++ ' milliseconds to process']" level="INFO"/>
INFO 2022-11-10 21:11:07,791 [[MuleRuntime].uber.09: [w-logger].logger-example-Flow.CPU_LITE @600dcddb]
[processor: logger-example-Flow/processors/1; event: 3fd13710-617f-11ed-900f-147ddaaf4f97]
org.mule.runtime.core.internal.processor.LoggerMessageProcessor: Message 100 processed successfully
----

This Logger records the processing time of a flow, using a single expression and DataWeave concatenation:

//TODO: Need to show how to get processing time or use a better example that doesn't throw an error.
[source,xml]
----
<logger category="performance" message="#['Message ' ++ payload.id ++ ' took ' ++ vars.processingTime ++ ' milliseconds to process']" level="INFO"/>
----

== Streamed Data

//TODO: say something about how a logger handles streamed content


== See also

xref:dataweave::dataweave-types.adoc#dw_type_string[DataWeave String Type]
* xref:dataweave::dataweave-types.adoc#dw_type_string[DataWeave String Type]