gpt4 book ai didi

java - 如何自定义 log4j2 RollingFileAppender?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:05:30 24 4
gpt4 key购买 nike

我们使用 log4j 1.2.x 登录我们的产品,并希望在不久的将来迁移到 log4j 2.x。我们实现的功能之一是在生成的每个新滚动日志文件中记录系统信息和其他重要参数。我们在 log4j 1.2.x 中的实现方式是我们扩展了 log4j 的 RollingFileAppender 类并覆盖了 rollOver() 方法,下面是实现的部分片段

@Override
public void rollOver() {

super.rollOver(); //We are not modifying it's default functionality but as soon as rollOver happens we apply our logic

//
// Logic to log required system properties and important parameters.
//

}

现在,当我们想要迁移到 log4j2 时,我们正在寻找一种新的解决方案来实现相同的功能。但正如我看到的 log4j2 的源代码,它与旧的源代码有很大不同。 RollingFileAppender 类不包含 rollover() 方法,因为它已移至 RollingManagerhelper 并且已设置为 private以及。

开发一个完整的新包并从 log4j2 扩展/实现一些抽象/帮助类是我们可能的解决方案之一,但这需要大量编码/复制,因为我们不修改 RollingFileAppender 确实我们只需要对它进行小的扩展。有一个简单的解决方案吗?

更新

我根据答案中的建议创建了一个自定义查找,下面是我的创建方式;

@Plugin(name = "property", category = StrLookup.CATEGORY)
public class CustomLookup extends AbstractLookup {

private static AtomicLong aLong = new AtomicLong(0);

@Override
public String lookup(LogEvent event, String key) {

if (aLong.getAndIncrement() == 0) {
return "this was first call";
}
if (key.equalsIgnoreCase("customKey")) {
return getCustomHeader();
} else {
return "non existing key";
}
}

private static String getCustomHeader() {

// Implementation of custom header
return "custom header string";

}}

但这并没有像提到的那样工作;这总是在 header 中打印 this was first call 。我还尝试将断点放在第一个 if 条件上,我注意到它只被调用一次。所以我担心的是,当 log4j2 从 xml 配置初始化其属性时,customLookup 类只会在启动时被初始化。我不知道我还能如何实现这个自定义查找类。

更新 2

在上面的实现之后,我尝试了一些不同的方式,如下所示;

private static AtomicLong aLong = new AtomicLong(0);

@Override
public String lookup(LogEvent event, String key) {
return getCustomHeader(key);
}

private static String getCustomHeader(final String key) {

if (aLong.getAndIncrement() == 0) {
return "this was first call";
}
if (key.equalsIgnoreCase("customKey")) {
// Implementation for customKey
return "This is custom header";
} else {
return "non existing key";
}
}

但这也做同样的事情。 log4j2 在从其 xml 配置文件初始化时创建 header ,然后使用内存中的 header 。重写的 lookup() 方法的 return 值不能动态更改,因为它仅在初始化期间被调用。如有任何更多帮助,我们将不胜感激。

最佳答案

使用内置查找的替代方法是创建自定义查找。这可以使用 log4j2 插件在几行代码中完成。然后,您的自定义查找会提供您希望在每次滚动时在文件标题中显示的确切值。

插件代码看起来像这样:

package com.mycompany;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.lookup.AbstractLookup;
import org.apache.logging.log4j.core.lookup.StrLookup;

/**
* Looks up keys from a class SomeClass which has access to all
* information you want to provide in the log file header at rollover.
*/
@Plugin(name = "setu", category = StrLookup.CATEGORY)
public class SetuLookup extends AbstractLookup {

/**
* Looks up the value of the specified key by invoking a
* static method on SomeClass.
*
* @param event The current LogEvent (ignored by this StrLookup).
* @param key the key to be looked up, may be null
* @return The value of the specified key.
*/
@Override
public String lookup(final LogEvent event, final String key) {
return com.mycompany.SomeClass.getValue(key);
}
}

然后,在您的配置中,您可以使用模式布局的标题在每次翻转时输出:

<RollingFile name="RollingFile" fileName="logs/app.log"
filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}.log.gz">

<!-- use custom lookups to access arbitrary internal system info -->
<PatternLayout header="${setu:key1} ${setu:key2}">
<Pattern>%d %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
</RollingFile>

log4j2 手册有关于构建/部署的详细信息 custom plugins .简要总结:

最简单的方法是使用 Maven 构建 jar;这将导致 log4j2 注释处理器在 jar 中生成二进制索引文件,以便 log4j2 可以快速找到您的插件。

另一种方法是在 log4j2.xml 配置的 packages 属性中指定插件类的包名称:

<Configuration status="warn" packages="com.mycompany">
...

更新:请注意,在您的查找实现中,您可以根据需要发挥创意。例如:

package com.mycompany;

public class SomeClass {
private static AtomicLong count = new AtomicLong(0);

public static String getValue(final String key) {
if (count.getAndIncrement() == 0) { // is this the first call?
return ""; // don't output a value at system startup
}
if ("FULL".equals(key)) {
// returns info to shown on rollover, nicely formatted
return fullyFormattedHeader();
}
return singleValue(key);
}
....
}

关于java - 如何自定义 log4j2 RollingFileAppender?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27274467/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com