Over a million developers have joined DZone.

Logger Injection With Spring’s InjectionPoint

DZone's Guide to

Logger Injection With Spring’s InjectionPoint

If you have a tendency to use Logger instances, see how you can set up a way to inject them into your features at runtime.

· Java Zone
Free Resource

Managing a MongoDB deployment? Take a load off and live migrate to MongoDB Atlas, the official automated service, with little to no downtime.

You should all be familiar with the boiler plate code you include on top of your classes to get a Logger instance for use in your classes at runtime.

Logger logger = LoggerFactory.getLogger(WorkOrderController.class);

This is the classic way of obtaining a logger instance at runtime with the classname that you want to be displayed in your log messages like below.

2016-11-22 14:48:37.025 ERROR --- org.orwashere.WorkOrderController : My Service got the request!!

The fully qualified class name org.orwashere.WorkOrderController is retrieved and logged from the class parameter you pass to LoggerFactories getLogger class.

With Spring Framework’s new InjectionPoint feature you can get rid of these boiler plate code in your classes. All you need to do is declaring a prototype scoped Logger bean in one of your configuration classes like below. And from that point, you have access to the class’s properties that the Logger bean is being injected to. As we should be creating a new Logger instance for each Spring Bean that we want to enable logging, it is important to define the Logger bean as prototype scoped.

Logger logger(InjectionPoint injectionPoint){
    return LoggerFactory.getLogger(injectionPoint.getMethodParameter().getContainingClass());

In your class target bean in which the logger will actually be used all you need to do is injecting the Logger bean as a classic Spring Bean and voila your logger is configured and ready to be used in your class like below.

public class WorkOrderController {

    private static Logger logger;

    public WorkOrderController(Logger logger) {
        this.logger = logger;

    public void myControllerMethod() {
        logger.error("My service got the request!");

Even though we are talking about Logger injection in this post, with InjectionPoint support many other opportunities arise before injecting any bean into your class. Basically you can make any kind of configuration on your injected bean depending on the target bean.

Hope it helps you in your future development!

Happy coding!

MongoDB Atlas is the easiest way to run the fastest-growing database for modern applications — no installation, setup, or configuration required. Easily live migrate an existing workload or start with 512MB of storage for free.

java ,spring boot ,spring framework ,logger

Published at DZone with permission of Fatih Dirlikli. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}