0

How can I pass the logger to the parent class using Lombok?

This is how I would do it creating loggers by myself:

public abstract class Parent
{
   private final Logger log = LoggerFactory.getLogger(getClass());

   public void execute()
   {
       log.info("Do something");
   }
}

public class Child extends Parent
{        

}

new Child().execute();

Log output:

2021-01-16 INFO  [main] c.a.b.c.d.Child: Do it

Using lombok

@Slf4j
public abstract class Parent
{
   public void execute()
   {
       log.info("Do something");
   }
}

@Slf4j
public class Child extends Parent
{        

}

new Child().execute();

Log output:

2021-01-16 INFO  [main] c.a.b.c.d.Parent: Do something
  • 3
    Don't. Each class is supposed to have its own *static* logger. If you need the name of a subclass in a log line from your parent class, add it to the statement explicitly. – Jorn Jul 06 '23 at 12:50

0 Answers0