[
https://issues.apache.org/jira/browse/LOG4J2-2918?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17202299#comment-17202299
]
Geng Yuanzhe edited comment on LOG4J2-2918 at 9/25/20, 4:56 PM:
----------------------------------------------------------------
The *context-wide filter* may work, but it is not the standard solution. As I
said above, Log4j2 never guarantees that the filter will be called only once
when log.
There are several ways to implement your requirements
# Use [AOP|https://en.wikipedia.org/wiki/Aspect-oriented_programming], for
Spring project, follow the link
[https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#aop],
otherwise you can use
[HuTool|https://www.hutool.cn/docs/#/aop/%E5%88%87%E9%9D%A2%E4%BB%A3%E7%90%86%E5%B7%A5%E5%85%B7-ProxyUtil]
# Extend the log4j2 according to this
[doc|https://logging.apache.org/log4j/2.x/manual/extending.html], and provider
your custom Logger. Your custom Logger can extends class
org.apache.logging.log4j.spi.AbstractLogger and overwride the info method.
{code:java}
@Override
public void info(final String message) {
LogStatistic.increaseCount(); // your custom count method
logIfEnabled(FQCN, Level.INFO, null, message, (Throwable) null);
}
{code}
org.apache.logging.log4j.core.tools.GenerateExtendedLoggerTest is a good
exmaple of extending the Log4j with LogContextFactory.
was (Author: gengyuanzhe):
The *context-wide filter* may work, but it is not the standard solution. As I
said above, Log4j2 never guarantees that the filter will be called only once
when log.
There are many ways to implement your requirements.
# Use [AOP|https://en.wikipedia.org/wiki/Aspect-oriented_programming], for
Spring project, follow the link
[https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#aop],
otherwise you can use
[HuTool|https://www.hutool.cn/docs/#/aop/%E5%88%87%E9%9D%A2%E4%BB%A3%E7%90%86%E5%B7%A5%E5%85%B7-ProxyUtil]
# Extend the log4j2 according to this
[doc|https://logging.apache.org/log4j/2.x/manual/extending.html], and provider
your custom Logger. Your custom Logger can extends class
org.apache.logging.log4j.spi.AbstractLogger and overwride the info method.
{code:java}
@Override
public void info(final String message) {
LogStatistic.increaseCount(); // your custom count method
logIfEnabled(FQCN, Level.INFO, null, message, (Throwable) null);
}
{code}
org.apache.logging.log4j.core.tools.GenerateExtendedLoggerTest is a good
exmaple of extending the Log4j with LogContextFactory.
> NPE on reconfigure method call with async configuration
> -------------------------------------------------------
>
> Key: LOG4J2-2918
> URL: https://issues.apache.org/jira/browse/LOG4J2-2918
> Project: Log4j 2
> Issue Type: Bug
> Affects Versions: 2.13.3
> Reporter: Dennys Fredericci
> Priority: Major
>
> I am getting the error below trying to reconfigure log4j programmatically.
>
> {code:java}
> 2020-08-24 10:10:18,163 main ERROR Could not register mbeans
> java.lang.NullPointerException at
> org.apache.logging.log4j.core.async.AsyncLoggerConfigDisruptor.createRingBufferAdmin(AsyncLoggerConfigDisruptor.java:417)
> at
> org.apache.logging.log4j.core.async.AsyncLoggerConfig.createRingBufferAdmin(AsyncLoggerConfig.java:199)
> at
> org.apache.logging.log4j.core.jmx.Server.registerLoggerConfigs(Server.java:366)
> at
> org.apache.logging.log4j.core.jmx.Server.reregisterMBeansAfterReconfigure(Server.java:186)
> at
> org.apache.logging.log4j.core.jmx.Server.reregisterMBeansAfterReconfigure(Server.java:141)
> at
> org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:629)
> at
> org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:712)
> at
> org.apache.logging.log4j.core.config.Configurator.reconfigure(Configurator.java:239)
> at br.com.fredericci.Main.main(Main.java:28)
> {code}
>
> Seems the same or similar problem described here:
> https://issues.apache.org/jira/browse/LOG4J2-807
> Here is the project which reproduces this issue:
> [https://github.com/dennysfredericci/log4j2-filter-spike]
--
This message was sent by Atlassian Jira
(v8.3.4#803005)