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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Java
  4. Think Again Before Adopting Log4j JMSAppenders

Think Again Before Adopting Log4j JMSAppenders

Ming Yuan user avatar by
Ming Yuan
·
Dec. 08, 10 · Interview
Like (1)
Save
Tweet
Share
18.06K Views

Join the DZone community and get the full member experience.

Join For Free

Log4j is a powerful and popular logging framework. In addition to providing good performance at run time, the framework also offers the flexibility to create your own logging components by implementing Log4j interfaces.

Appenders, in the terminology of log4j, are responsible for handling the output and transport of logging data. Customized Log4j appenders are often developed to meet the specific requirements of an application or an organization. More specifically, JMSAppenders are created to send logging messages to JMS queue. In fact, it is really easy to use such JMSAppenders in your application – just several lines of configuration definitions in your log4j.xml is sufficient enough to see your messages being sent to the pre-defined JMS destinations. Due to such simplicity, JMSAppenders are over used in many non-relevant circumstances, so be judicious in evaluating your need to have a JMSappender.

JMSAppenders are normally implemented in two ways.
1) The implmentation in the log4j Companion package, where JMSConnection, JMSSession and JMSSender objects are re-used through the whole life time of a JMSAppender.
2) A sample JMSAppender from IBM. It opens, and then closes, all JMS objects with the append method. Obviously, this type of implementations has some performance impact during runtime.

During testing, i found the first implementation performed 600 times faster than the second one. I don't recommand the second implementation. However, the first implementation may cause unexpected issues during the following situations. So think again before using any JMSAppenders.

When JMSAppender is associated to a “global” logger definition
You may create a root logger for your whole application, or you may invoke a singleton-based logging help class within your application.
When you associate your JMSAppender with a logger definition in your log4j.xml, Log4j instantiates only one JMSAppender object for the logger. In case, the logger can be utilized anywhere in your application, it is true that your whole application depends on a single JMSConnection to transport all messages.

When on heavy Loads.
“Heavy load” includes two situations: the volume of log messages is high, and/or, the size of each message is big. Log4j is designed as a thread-safe framework. It does a good job to dispatch images. However, under heavy load, things starts to change. In a test case which creates 10 threads and each of threads can invoke a JMSAppender 600 times. The testing results really surprised us.

Result 1 – We ran the test case; but commented out the line invoking the JMSAppender.

[LogThread[8]: Total time passed since start of application: 6031]
[LogThread[0]: Total time passed since start of application: 6094]
[LogThread[1]: Total time passed since start of application: 6156]
[LogThread[5]: Total time passed since start of application: 6188]
[LogThread[4]: Total time passed since start of application: 6266]
[LogThread[3]: Total time passed since start of application: 6313]
[LogThread[6]: Total time passed since start of application: 6391]
[LogThread[9]: Total time passed since start of application: 6406]
[LogThread[7]: Total time passed since start of application: 6469]
[LogThread[2]: Total time passed since start of application: 6485]


Result 2 – We built a timer within threads. Every 100 milliseconds, our code invokes the JMSAppender.

[LogThread[6]: Total time passed since start of application: 71430]
[LogThread[0]: Total time passed since start of application: 71477]
[LogThread[7]: Total time passed since start of application: 71477]
[LogThread[3]: Total time passed since start of application: 71492]
[LogThread[2]: Total time passed since start of application: 71571]
[LogThread[1]: Total time passed since start of application: 71586]
[LogThread[5]: Total time passed since start of application: 71649]
[LogThread[9]: Total time passed since start of application: 71664]
[LogThread[4]: Total time passed since start of application: 71805]
[LogThread[8]: Total time passed since start of application: 71820]


Result 3 – our testing code simply invokes the JMSAppender 600 times without any intervals. This case can test log4j’s behavior under heavy load.

[LogThread[1]: Total time passed since start of application: 761142]
[LogThread[9]: Total time passed since start of application: 800422]
[LogThread[3]: Total time passed since start of application: 802078]
[LogThread[5]: Total time passed since start of application: 804562]
[LogThread[7]: Total time passed since start of application: 812843]
[LogThread[6]: Total time passed since start of application: 815249]
[LogThread[8]: Total time passed since start of application: 817374]
[LogThread[2]: Total time passed since start of application: 817484]
[LogThread[4]: Total time passed since start of application: 817515]
[LogThread[0]: Total time passed since start of application: 817515]


From three test cases above, we can see the Log4j JMSAppender took much longer time during heavy load.

When true asynchronization is required
By default, Log4j appenders behave synchronously unless explicitly using its AsynAppender. In a JMSAppender, the time it takes to format and transport your messages is part of the same process in your application. Too many logging statements in your code can eventually slow down your application’s performance.

When content is critical to business processes
Log4j JMSAppenders are managed by the Log4j framework. The design principle is simply for logging purposes. This type of utility classes should not be used to send messages/events critical to your core business functions. It also should not be used when fail-over is required. The reason is simple. For applications with well defined logging strategy, the volume of logging messages should not be significantly changed during its peak time and normal time. This is a foundational assumption in logging frameworks. Using JMSAppender for business events breaks this assumption.

 

So what can we do when leveraging a JMSAppender?

  • Keep in mind logging utility classes should be used only for logging purposes.
  • Avoid binding JMSAppenders to any global loggers. Replace a global logger with multiple loggers for different packages, and associate JMSAppenders to each of these loggers with smaller scopes.
  • Use the service activator pattern when asynchronized behavior is required or sending business critical events. In some circumstances, you can use the pattern twice: use it once to de-couple message transprotation time; and use it again to decouple the time of message processing.
application Log4j

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 10 Most Popular Frameworks for Building RESTful APIs
  • Running Databases on Kubernetes
  • Microservices 101: Transactional Outbox and Inbox
  • Seamless Integration of Azure Functions With SQL Server: A Developer's Perspective

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: