为什么静态值不同于appender(log4j custom appender)?
Why is the static value different from appender(log4j custom appender)?
我在 spring.
中创建了这样的静态变量
public class A {
private static final Map<String, Session> listMap = new HashMap<>();
public static Map<String> getMap() { return this.listMap.values() }
public static void addMap(String name, Session s) { return this.listMap.put(name, s) }
}
我保存在服务层
@Slf4j
public class BService {
public void addSession(String name, Session s) {
A.addMap("a", s);
log.info("added!");
}
}
保存后,我在自定义appender中使用它。(https://www.baeldung.com/log4j2-custom-appender)
@Plugin(
name = "MapAppender",
category = Core.CATEGORY_NAME,
elementType = Appender.ELEMENT_TYPE)
public class MapAppender extends AbstractAppender {
private ConcurrentMap<String, LogEvent> eventMap = new ConcurrentHashMap<>();
protected MapAppender(String name, Filter filter) {
super(name, filter, null);
}
@PluginFactory
public static MapAppender createAppender(
@PluginAttribute("name") String name,
@PluginElement("Filter") Filter filter) {
return new MapAppender(name, filter);
}
@Override
public void append(LogEvent event) {
Map<> resultMap = A.getMap();
send()
}
}
然而,当appender的append()方法被执行时,A.getMap()
return nothing(size 0)。 (A.getMap() return 在服务层正确。)
为什么静态值不一样?..
listMap
在你调用addSession
时被加载所以它是空的当append()
方法被调用时
我在 spring.
中创建了这样的静态变量public class A {
private static final Map<String, Session> listMap = new HashMap<>();
public static Map<String> getMap() { return this.listMap.values() }
public static void addMap(String name, Session s) { return this.listMap.put(name, s) }
}
我保存在服务层
@Slf4j
public class BService {
public void addSession(String name, Session s) {
A.addMap("a", s);
log.info("added!");
}
}
保存后,我在自定义appender中使用它。(https://www.baeldung.com/log4j2-custom-appender)
@Plugin(
name = "MapAppender",
category = Core.CATEGORY_NAME,
elementType = Appender.ELEMENT_TYPE)
public class MapAppender extends AbstractAppender {
private ConcurrentMap<String, LogEvent> eventMap = new ConcurrentHashMap<>();
protected MapAppender(String name, Filter filter) {
super(name, filter, null);
}
@PluginFactory
public static MapAppender createAppender(
@PluginAttribute("name") String name,
@PluginElement("Filter") Filter filter) {
return new MapAppender(name, filter);
}
@Override
public void append(LogEvent event) {
Map<> resultMap = A.getMap();
send()
}
}
然而,当appender的append()方法被执行时,A.getMap()
return nothing(size 0)。 (A.getMap() return 在服务层正确。)
为什么静态值不一样?..
listMap
在你调用addSession
时被加载所以它是空的当append()
方法被调用时