Spring constructor injection of SLF4J logger - how to get injection target class?

后端 未结 8 2009
太阳男子
太阳男子 2020-12-25 15:20

I\'m trying to use Spring to inject a SLF4J logger into a class like so:

@Component
public class Example {

  private final Logger logger;

  @Autowired
  pu         


        
8条回答
  •  南方客
    南方客 (楼主)
    2020-12-25 15:38

    1. Why are you creating a new logger for each instance? The typical pattern is to have one logger per class (as a private static member).

    2. If you really do want to do it that way: Maybe you can write a logger factory class, and inject that? Something like:

      @Singleton 
      public class LogFactory { 
          public Logger getLogger(Object o) {  
              return LoggerFactory.getLogger(o.getClass());  
          }  
      }
      

提交回复
热议问题