DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Databases
  4. How to Avoid Printing Exception Stack Traces

How to Avoid Printing Exception Stack Traces

Stack traces have been around in the software development world for quite some time now, but it can't be ignored that they can create major issues for Operations teams.

Alagappan Veerappan user avatar by
Alagappan Veerappan
·
Feb. 12, 17 · Tutorial
Like (2)
Save
Tweet
Share
12.85K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Printing API

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Getting Started With JMS-ActiveMQ: Explained in a Simple Way
  • Distributed Stateful Edge Platforms
  • Architecture and Code Design, Pt. 2: Polyglot Persistence Insights To Use Today and in the Upcoming Years
  • How To Create a Stub in 5 Minutes

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: