2017-05-29 69 views
1


我的log4j配置文件中有多个RollingFileAppender以管理不同的日志类型。
这里是我的的LoggerFactory类:
在java中使用CDI注入多个log4j记录器类型

public class LoggerFactory { 

    static { 
     BasicConfigurator.configure(); 
    } 

    @Produces 
    public Logger produceLog(InjectionPoint ip) { 
     return Logger.getLogger(ip.getMember().getDeclaringClass().getName()); 
    } 

    @Produces 
    @LogType(loggerType = LogType.LoggerType.CRITICAL) 
    public Logger produceCriticalLogger(InjectionPoint injectionPoint) { 
     return Logger.getLogger("critical"); 
    } 

    @Produces 
    @LogType(loggerType = LogType.LoggerType.SERVICE_TIMING) 
    public Logger produceServiceTimingLogger(InjectionPoint injectionPoint) { 
     return Logger.getLogger("serviceTiming"); 
    } 

    @Produces 
    @LogType(loggerType = LogType.LoggerType.HEALTH) 
    public Logger produceHealthLogger(InjectionPoint injectionPoint) { 
     return Logger.getLogger("health"); 
    } 

    @Produces 
    @LogType(loggerType = LogType.LoggerType.HTTP_HEADER) 
    public Logger produceHttpLogger(InjectionPoint injectionPoint) { 
     return Logger.getLogger("httpHeader"); 
    } 

    @Produces 
    @LogType(loggerType = LogType.LoggerType.POSTED_REQUEST) 
    public Logger producePostRequestLogger(InjectionPoint injectionPoint) { 
     System.out.println(" ********* getAnnotated" + injectionPoint.getAnnotated()); 
     System.out.println(injectionPoint.toString()); 

     return Logger.getLogger("postedRequest"); 
    } 

    @Produces 
    @LogType(loggerType = LogType.LoggerType.DB_TIMING) 
    public Logger produceDbTimingLogger(InjectionPoint injectionPoint) { 
     return Logger.getLogger("dbTiming"); 
    } 

    @Produces 
    @LogType(loggerType = LogType.LoggerType.STACK_TRACE) 
    public Logger produceStackTraceLogger(InjectionPoint injectionPoint) { 
     return Logger.getLogger("stackTrace"); 
    } 

    @Produces 
    @LogType(loggerType = LogType.LoggerType.TRANSACTIONAL) 
    public Logger produceTransactionsLogger(InjectionPoint injectionPoint) { 
     return Logger.getLogger("transactions"); 
    } 
} 

,这里是我的注释

@Qualifier 
@Retention(RetentionPolicy.RUNTIME) 
@Target({FIELD, METHOD, TYPE}) 
public @interface LogType { 

    LoggerType loggerType(); 

    public enum LoggerType { 
     CRITICAL, 
     DB_TIMING, 
     HEALTH, 
     HTTP_HEADER, 
     POSTED_REQUEST, 
     SERVICE_TIMING, 
     STACK_TRACE, 
     TRANSACTIONAL 
    } 
} 

,这里是我的注射点

@Inject 
    @LogType(loggerType = LogType.LoggerType.HEALTH) 
    public transient Logger logger; 

我得到这个错误:

Unsatisfied dependencies for type Logger with qualifiers @LogType 
    at injection point [BackedAnnotatedField] @Inject @LogType public transient myclass.logger 

信息
有在我的项目更多的注入点和他们的工作很好,我肯定WELD库状况。我看到我的IDE中的bean图标将我链接到生产者方法。但我仍然无法找到问题!
问题
使用CDI实现多种记录器类型的最佳做法是什么?

+0

没有显示的代码定义或引用'@ Loggers' –

+0

@ steve-c,它是从'@ Loggers'重构为'@ LogType' –

+0

为什么你的应用程序的结构?它是一个简单的WAR还是EAR文件? –

回答

0

制作您的制作人类LoggerFactory一个CDI Bean。在你的例子中,它是一个没有ani CDI注解的简单类。例如,你可以使@ApplicationScoped或使用任何其他合适的范围。

或者,您可以在beans.xml中将bean发现模式设置为“all”。这样,即使未注释的bean也会在应用程序启动过程中被发现。