- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我们使用 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/
不同的 LogCat 方法是: Log.v(); // Verbose Log.d(); // Debug Log.i(); // Info Log.w(); // Warning Log.e();
在android群里,经常会有人问我,android log是怎么用的,今天我就把从网上以及sdk里东拼西凑过来,让大家先一睹为快,希望对大家入门android log有一定的帮助. android
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 4 年前。 社区 12
我正在尝试使用 sonarlint 检查代码质量.上面的问题概要,我不明白为什么它要说要大写。但是 this discussion与上述建议相反。哪一个应该被认为是正确的? 最佳答案 这没有正确答案,
随着 n 变大,log*(log n) 和 log(log* n) 这两个函数会更快吗? 这里,log* 函数是迭代对数,定义如下: 我怀疑它们是相同的,只是写法不同,但它们之间有什么区别吗? 最佳答
作为家庭作业,我被要求在 O(log(n)) 中编写一个算法,我可以计算出我编写的算法的复杂度为 O(log(n) + log(n/2) + log(n/4) + log(n/8) + ... + l
我正在使用 Tomee。日志文件夹包含这样的文件 localhost_access_log.2016-12-02.txt localhost.2016-12-02.log catalina.2016-
Android Log.v、Log.d、Log.i、Log.e 等的 ios 等效项是什么?同样在 android 上,我使用 Android 设备监视器和 logcat 来访问我的手机日志,我需要在
我认为下面的代码是 O(log log n) 因为它里面有 i*i 但我对 log n 感到困惑> 和 log (log n)。 for (i=2; i*i<=number; i++) { if
我正在修改 kvm 模块,并在内核代码中添加了 printk 语句。运行虚拟机后,printk 为我提供了错误地址和有关 guest 操作系统的其他信息。 我需要从这个信息中生成统计信息。当我使用 d
我有一个部署为 Windows Azure Web 角色的 WCF 服务。 我正在使用 Enterprise Library 进行异常处理,并且在我的本地 Development Fabric 中,似
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 9 年前。 Improv
在 Go 的生产中使用 log.SetFlags(log.LstdFlags | log.Lshortfile) 是好的做法(至少是一般做法)吗?我想知道在生产中这样做是否存在性能或安全问题。因为它不
我想知道什么更快: double value = Math.log(a) - Math.log(b); 或 double value = Math.log(a/b); 我计算值的方式是否会对性能产生影
我有数百个子例程使用 log.Println() 写入日志文件 我正在使用 log.Println 写入 error.log 文件。 func main() { e, err := os.Open
我将 Nuxt 与 SSR 一起使用,并希望有类似于 apaches 的 access.log 和 error.log 的东西 我特别感兴趣的是每次调用的响应时间。 我在 nuxt 文档中找不到任何内
我知道以前有人问过这个问题,但我相信这是一个不同的问题。 Nginx 在 www-data 下运行: $ ps -eo "%U %G %a" | grep nginx root root
我在我的日志文件中发现了一个非常奇怪的条目 Jan 29 01:35:30 vs-proj-handy sshd[5316]: Received disconnect from 130.207.203
对于我正在开发的应用程序,我希望在开发过程中和发布时简化故障排除。我希望能够检索到对 Log 的调用,以了解在 USB 调试中没有连接手机的情况下运行应用程序时的调用,以便可以检索并发送给我。例如,当
我试图捕获 panic 并记录错误: func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloRep
我是一名优秀的程序员,十分优秀!