为什么静态值与appender(log4j自定义appender)不同?

huangapple go评论72阅读模式
英文:

Why is the static value different from appender(log4j custom appender)?

问题

我在Spring中像这样创建静态变量

```java
public class A {
  private static final Map<String, Session> listMap = new HashMap<>();
  public static Map<String, Session> 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()返回为空(大小为0)。(在服务层中,A.getMap()返回正确。)
为什么静态值会不同?...

英文:

I make static variable like this in spring.

public class A {
  private static final Map&lt;String, Session&gt; listMap = new HashMap&lt;&gt;();
  public static Map&lt;String&gt; getMap() { return this.listMap.values() }
  public static void addMap(String name, Session s) { return this.listMap.put(name, s) }
}

I save in service layer.

@Slf4j
public class BService {
  public void addSession(String name, Session s) { 
    A.addMap(&quot;a&quot;, s);
    log.info(&quot;added!&quot;);
  }
}

After saving it, I used it in custom appender.(https://www.baeldung.com/log4j2-custom-appender)

@Plugin(
  name = &quot;MapAppender&quot;, 
  category = Core.CATEGORY_NAME, 
  elementType = Appender.ELEMENT_TYPE)
public class MapAppender extends AbstractAppender {
 
    private ConcurrentMap&lt;String, LogEvent&gt; eventMap = new ConcurrentHashMap&lt;&gt;();
 
    protected MapAppender(String name, Filter filter) {
        super(name, filter, null);
    }
 
    @PluginFactory
    public static MapAppender createAppender(
      @PluginAttribute(&quot;name&quot;) String name, 
      @PluginElement(&quot;Filter&quot;) Filter filter) {
        return new MapAppender(name, filter);
    }
 
    @Override
    public void append(LogEvent event) {
        Map&lt;&gt; resultMap = A.getMap();
        send()
    }
}

However, when the appender's append() method is executed, A.getMap() return nothing(size 0). (A.getMap() return correctly in service layer.)
Why is the static value different?..

答案1

得分: 1

listMap 在调用 addSession 时被加载,所以当调用 append() 方法时它是空的。

英文:

listMap is loaded when you call addSessionso it is empty When append() method is called

huangapple
  • 本文由 发表于 2020年4月9日 18:16:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/61118878.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定