0

我有一点问题,我会尽力解释

我正在尝试接管org.slf4j.LoggerFactory我在项目中创建的包结构并创建一个名为LoggerFactory并将原始实现复制到它的类。

然后我添加了我自己的扩展如下:

/***************************************************************
 * This section is not a part of SLF4J Implemetation!!!!!!!!!!!!!
 ****************************************************************/
private static ApplicationContext context;
private static boolean isAdvanced = true;
private static final List<Logger> list = Collections.synchronizedList(new ArrayList<Logger>());
private static Logger getLogger(Logger nativeLogger){
    if (context != null){
        ApplicationLogger al = (ApplicationLogger) context.getBean(ApplicationLogger.class);
        al.setNativelogger(nativeLogger);
        return al;
    }else{
        saveLogger(nativeLogger);
        return nativeLogger;
    }
}
private static void saveLogger(Logger nativeLogger){
    if (isAdvanced){
        list.add(nativeLogger);
    }
}
public static void mergeWithApplicationLogger(ApplicationContext acontext){
    context = acontext;
    while (!list.isEmpty()) {
        Logger storedlogger = list.get(0);
        ApplicationLogger al = context.getBean(ApplicationLogger.class);
        al.setNativelogger(storedlogger);
        storedlogger = al;
        list.remove(0);
    }
}
public static void deactivateAdvancedLogging(){
    LoggerFactory.isAdvanced = false;
    list.clear();
}

并将 getLogger 函数更改为此

public static Logger getLogger(String name) {
ILoggerFactory iLoggerFactory = getILoggerFactory();
return getLogger(iLoggerFactory.getLogger(name));

}

解释:

发生的情况是,应用程序中您调用的每个地方LoggerFactory.getLogger都会:

  1. 如果mergeWithApplicationLogger尚未调用,则保存指向该记录器的指针
  2. mergeWithApplicationLogger被调用时,将所有指针更改为新的ApplicationLogger(它完全实现了记录器接口)并且对于从这里开始的每个请求返回ApplicationLogger

问题

好吧,问题仅出现在 1-2 个拒绝将其记录器指针更改为新应用程序记录器的类上。我不知道为什么会发生这种情况,因为在大多数系统上,一切都按预期发生,不合作的班级没有什么特别的。这是该类的一些代码

@Component
 public class ResourceManager extends EntityCacheManager<ResourcePermission,Integer>{
private Logger logger = LoggerFactory.getLogger(ResourceManager.class);  ...}

 public abstract class EntityCacheManager<ValueType extends Serializable,KeyType> extends CachedItemsManager{
@Autowired
protected ICache cache;
private Logger logger = LoggerFactory.getLogger(EntityCacheManager.class);

这个类总是有 logback 记录器而不是ApplicationLogger

我已经尝试了我所知道的一切......一切!我希望有人能帮助我找到一个新的方向来让它发挥作用。

谢谢。

4

0 回答 0