Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until end-of-day today:

>> GET ACCESS NOW

November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until end-of-day today:

>> GET ACCESS NOW

1. Overview

The Log4j 2 library has added support for Java 8 lambda expressions since version 2.4. These expressions can be used by the Logger interface to enable lazy logging.

Let's see a quick example of how we can make use of this feature.

For more information on Log4j 2, also check out our introductory article.

2. Lazy Logging With Lambda Expressions

A potential performance improvement for applications that use logging can result from avoiding the calculation of log messages if the corresponding log level is not enabled.

First, let's see a simple log statement at TRACE level:

logger.trace("Number is {}", getRandomNumber());

In this example, the getRandomNumber() method is called to substitute the log message parameter regardless of whether TRACE statements are displayed or not. For example, if the log level is set to DEBUG, log4j 2 will not log the message, but the getRandomNumber() method still runs.

In other words, the execution of this method may be unnecessary.

Before the addition of support for lambda expressions, we could avoid constructing messages which are not logged by explicitly checking the log level before executing the log statement:

if (logger.isTraceEnabled()) {
    logger.trace("Number is {}", getRandomNumer());
}

In this case, the getRandomNumber() method is only called if the TRACE log level is enabled. This can improve performance depending on how expensive the execution of methods used to substitute parameters is.

By using lambda expressions, we can further simplify the code above:

logger.trace("Number is {}", () -> getRandomNumber());

The lambda expression is only evaluated if the corresponding log level is enabled. This is referred to as lazy logging.

We can also use multiple lambda expressions for a log message:

logger.trace("Name is {} and age is {}", () -> getName(), () -> getRandomNumber());

3. Conclusion

In this quick tutorial, we have seen how we can use lambda expressions with Log4j 2 loggers.

As always, the full source code of the example can be found over on GitHub.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until end-of-day today:

>> GET ACCESS NOW

Generic footer banner
Comments are closed on this article!