我在log4j配置文件中有多个RollingFileAppender
,以便管理不同的日志类型。
这是我的 LoggerFactory 类:
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图标,它将我链接到了producer方法。但我仍然无法找到问题!
的问题:
使用CDI实现多个记录器类型的最佳实践是什么?
答案 0 :(得分:0)
将您的制作人类LoggerFactory
制作成CDI Bean。在您的示例中,它是一个没有ani CDI注释的简单类。例如,您可以将其设为@ApplicationScoped或使用任何其他合适的范围。
或者,您可以将bean发现模式设置为" all"在beans.xml中。这样,即使在应用程序启动期间也会发现未注释的bean。