How to Avoid Printing Exception Stack Traces

This article explains how to avoid printing exception stack traces when running APIs that are configured with Mule EE runtime and Mule API gateway runtime.

Printing exception stack traces is not a new concept in the software world. However, it's still important to remember that if the stack traces are not handled properly, they will end up accumulating in log files. This can create issues for the Operations team when they want to manage higher environments.

We will discuss here some steps for both Mule EE and API gateway runtimes. The latest Mule EE runtime is 3.8.2. Older versions (< 3.8.X) have Mule EE runtime and API gateway runtime separately.

API gateway runtime is a feature to support the sharing of connectors across the projects deployed in the domain. However, there's a lot more to it than that. To know more about API gateway runtime, check out this documentation from MuleSoft. Mule EE 3.8.2 runtime has unified both API gateway and Mule core runtime.

Avoid Exception Stack Traces With Mule 3.8 Runtime

Configure the logException attribute as false in the catch-exception-strategy block. By default, logException is set as true, which enables Mule to print an exception stack trace. 

Here's an example snippet:

<catch-exception-strategy doc:name="Catch Exception Strategy" logException="false">
 <logger level="ERROR" doc:name="exceptionLogger" message="Exception message is ... #[exception.message]"/>
  <set-variable variableName="exceptionMessage" value="#[exception.message]" doc:name="Set exceptionMessage"/>
  <set-payload value="{  errors: {     errorCode: #[message.inboundProperties.'http.status'],     errorMessage: #[flowVars.exceptionMessage]  }  }" doc:name="Set Exception Payload"/>
</catch-exception-strategy>

Avoid Exception Stack Traces With Mule API Gateway Runtime

Mule API gateway runtime coupled with 3.7.x or Mule runtime (< 3.8.x) does not have the logException attribute defined in its implementation. The solution to avoid stack trace is below.

Define a logger inside the catch-exception-strategy and set the category, along with setting the level to ERROR.

Here's an example snippet:

<catch-exception-strategy doc:name="Catch Exception Strategy">
 <logger level="ERROR" doc:name="exceptionLogger" message="Exception message is ... #[exception.message]" category="com.project.stacktrace"/>
  <set-variable variableName="exceptionMessage" value="#[exception.message]" doc:name="Set exceptionMessage"/>
  <set-payload value="{  errors: {     errorCode: #[message.inboundProperties.'http.status'],     errorMessage: #[flowVars.exceptionMessage]  }  }" doc:name="Set Exception Payload"/>
</catch-exception-strategy>

Configure the below log4j settings in the project's log4j2.xml file as shown below:

<AsyncLogger name="org.mule.exception.CatchMessagingExceptionStrategy" level="FATAL"/>
<AsyncLogger name="com.project.stacktrace" level="ERROR"/>

I have demonstrated the above steps this a video tutorial.

We will continue expanding this example with error handling on Anypoint Studio in the next article.

 

 

 

 

Top