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實現多種記錄器類型的最佳做法是什麼?
沒有顯示的代碼定義或引用'@ Loggers' –
@ steve-c,它是從'@ Loggers'重構爲'@ LogType' –
爲什麼你的應用程序的結構?它是一個簡單的WAR還是EAR文件? –