2017-05-29 73 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也會在應用程序啓動過程中被發現。