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

后端 未结 8 2008
太阳男子
太阳男子 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:41

    I am trying to get this feature into official SLF4J API. Please support/vote/contribute: https://issues.jboss.org/browse/JBLOGGING-62

    (this feature is already implemented by JBoss Logging + Seam Solder, see http://docs.jboss.org/seam/3/latest/reference/en-US/html/solder-logging.html)

    11.4. Native logger API

    You can also inject a "plain old" Logger (from the JBoss Logging API):

    import javax.inject.Inject;
    import org.jboss.logging.Logger;
    
    public class LogService {
    
        @Inject
        private Logger log;
    
        public void logMessage() {
            log.info("Hey sysadmins!");
        }
    
    }
    

    Log messages created from this Logger will have a category (logger name) equal to the fully-qualified class name of the bean implementation class. You can specify a category explicitly using an annotation.

    @Inject @Category("billing")
    private Logger log;
    

    You can also specify a category using a reference to a type:

    @Inject @TypedCategory(BillingService.class)
    private Logger log;
    

    Sorry for not providing a relevant answer.

提交回复
热议问题