apiVersion: gateway.mulesoft.com/v1beta1
kind: PolicyBinding
metadata:
name: outbound-message-logging
spec:
targetRef:
kind: Service
name: upstream
policyRef:
name: message-logging-outbound-flex
config:
loggingConfiguration: <array>
- itemName: <string> // REQUIRED
itemData: //REQUIRED
message: <dataweave> // REQUIRED
conditional: <dataweave> // OPTIONAL
category: <string> // OPTIONAL
level: <string> // REQUIRED
firstSection: <bool> // OPTIONAL
secondSection: <bool> // OPTIONAL
Outbound Message Logging Policy
Policy name |
Outbound Message Logging |
Summary |
Logs custom messages from outbound requests sent to upstream services, responses from upstream services, or information from other outbound policies applied to the same upstream service |
Category |
Troubleshooting |
First Flex Gateway version available |
v1.11.0 |
Returned Status Codes |
No return codes exist for this policy |
Summary
The Outbound Message Logging policy logs custom messages from outbound requests sent to upstream services, responses from upstream services, or information from other outbound policies applied to the same upstream service. This policy is applied at the upstream level.
Configuring Policy Parameters
Flex Gateway Local Mode
In Local Mode, you apply the policy to your API via declarative configuration files. Refer to the following policy definition and table of parameters:
| Parameter | Required or Optional | Default Value | Description |
|---|---|---|---|
|
Required |
N/A |
Array of configurations. |
|
Required |
N/A |
Configuration name. |
|
Required |
N/A |
DataWeave expression that resolves to the message log. To learn more about supported DataWeave expressions, see DataWeave Expressions Support. |
|
Optional |
|
DataWeave expression that resolves to whether the message should be logged. To learn more about supported DataWeave expressions, see DataWeave Expressions Support. |
|
Optional |
Default category |
Log message prefix that indicates the package and class where the log is executed. |
|
Required |
N/A |
Defines the level of message to log: |
|
Optional |
|
Boolean that indicates if messages are logged before the outbound request is sent to the upstream service, taking into account the order in which this policy is applied. |
|
Optional |
|
Boolean that indicates if messages are logged after receiving the response from the upstream service, taking into account the order in which this policy is applied. |
Resource Configuration Example
apiVersion: gateway.mulesoft.com/v1beta1
kind: PolicyBinding
metadata:
name: outbound-message-logging
spec:
targetRef:
kind: Service
name: my-upstream
policyRef:
name: message-logging-outbound-flex
config:
loggingConfiguration:
- itemName: "Outbound Request Log"
itemData:
message: "#[payload]"
conditional: "#[attributes.queryParams['debug']=='true']"
level: "INFO"
firstSection: true
- itemName: "Outbound Response Log"
itemData:
message: "#['Upstream response: ' ++ attributes.statusCode as String]"
level: "INFO"
secondSection: true
Managed Flex Gateway and Flex Gateway Connected Mode
When you apply the policy to your API instance from the UI, the following parameters are displayed:
| Parameter | Description | Required or Optional |
|---|---|---|
Message |
A DataWeave expression for extracting the message to be logged from outbound requests or upstream responses. To learn more about supported DataWeave expressions, see DataWeave Expressions Support. |
Required |
Conditional |
A DataWeave Boolean expression used to specify whether a message is to be logged. To learn more about supported DataWeave expressions, see DataWeave Expressions Support. |
Optional |
Category |
A prefix in the log message that indicates the package and class where the log is executed. |
Optional |
Level |
The level of message to log: |
Required |
Before Sending Request |
Set to cause your logging policy to log messages before the outbound request is sent to the upstream service, taking into account the order in which this policy is applied. |
Optional |
After Receiving Response |
Set to cause your logging policy to log messages after receiving the response from the upstream service, taking into account the order in which this policy is applied. |
Optional |
DataWeave Expressions Support
The Outbound Message Logging policy supports the standard DataWeave Expressions supported by Flex Gateway policies and the following additional vars DataWeave expressions. Format the following expressions [vars.<expressions>], for example [vars.orgId]:
-
orgId -
masterOrgId -
envId -
apiId -
apiName -
apiInstanceName -
apiVersion -
receivedTs(rfc3339) -
repliedTs(rfc3339, only available in response context) -
runtimeVersion(For example:v1.7.0) -
hostId(For example:my-host) -
reqId(Referred to astraceIdin Flex Gateway logs) -
requestDisposition(PROCESSEDorVIOLATION, only available in request context) -
request.remoteAddress -
request.method -
request.requestPath -
request.headers(all request headers) -
upstream.uri(URI of the upstream service) -
upstream.headers(headers sent to upstream service) -
upstream.statusCode(status code from upstream response, only available in response context) -
violation.clientId(Only available in response context) -
violation.clientName(Only available in response context) -
violation.policyName(Only available in response context) -
violation.violationType(VIOLATIONorERROR, only available in response context)
How This Policy Works
Logging an outbound policy consists of the following sequence:
-
An outbound request is prepared to be sent to an upstream service.
-
The message is logged if these conditions are
true:-
The level and category of the log defined in the policy are included in the logger defined in the configuration file.
-
Either the conditional field of the policy is not set, or is set and the condition is met.
-
The
firstSectionflag is enabled (if logging before sending the request).
-
-
The request is sent to the upstream service.
-
The upstream service responds.
-
The message is logged if these conditions are
true:-
The level and category of the log defined in the policy are included in the logger defined in the configuration file.
-
Either the conditional field of the policy is not set, or is set and the condition is met.
-
The
secondSectionflag is enabled (if logging after receiving the response).
-
-
The response is returned to the client. Outbound Message Logging Policy does not interfere with the execution of any policy nor its flow.
-
The message appears in the application log.
Logging Format
A message log uses this format:
<date> [flex-gateway-envoy][<level>] wasm log <policy-name>.default.<api-name>.default.svc main: [req: <request uuid>] [accessLog] <message>
-
policy-name: displays the name of the policy binding. -
api-name: displays the name of the api instance. -
request uuid: displays a unique identifier for the request that triggered the message.



