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文件? –