- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在从 log4j
迁移 EAR 应用程序至log4j2
。我有一些类在不同的 EAR jar 中扩展附加器、过滤器、布局,现在,我已将它们转换为插件。这意味着我在多个 jar 中有自定义插件(假设有 3 个 jar)。
我没有使用packages
log4j2.xml
中的属性并使用 Dlog4j.configurationFile
初始化日志系统JVM 参数指向 log4j2.xml
位置在META-INF
耳朵。
在所有三个 jar 项目中添加以下插件不起作用。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>log4j-plugin-processor</id>
<goals>
<goal>compile</goal>
</goals>
<phase>process-classes</phase>
<configuration>
<proc>only</proc>
<annotationProcessors>
<annotationProcessor>org.apache.logging.log4j.core.config.plugins.processor.PluginProcessor</annotationProcessor>
</annotationProcessors>
</configuration>
</execution>
</executions>
</plugin>
图案布局:在下面的图案布局中,e
是自定义模式,其中编写了自定义模式转换器插件来转换此字符串。
<Pattern>%d %-5p [%c{1}] [EventId: %e] [%t] %m%n</Pattern>
上述模式布局的自定义转换器插件(在 jar1 中):
jar1 有 Log4J2Plugins.dat
文件位于META-INF
下在org.apache..
文件夹。
@Plugin(name = "EventPatternConverter", category = "Converter")
@ConverterKeys({"e"})
public class EventPatternConverter extends LogEventPatternConverter {
protected EventPatternConverter(String name, String style) {
super(name, style);
}
public static EventPatternConverter newInstance(String[] options) {
return new EventPatternConverter("e", "e");
}
@Override
public void format(LogEvent event, StringBuilder toAppendTo) {
String eventId= "";
// Append empty string (OR) value
toAppendTo.append(eventId);
}
}
但是,我收到以下错误
ERROR Unrecognized format specifier [e]
甚至,没有一个自定义插件被识别为我得到 invalid element
对于其余的自定义插件,它们都在 jar2、jar3 中可用,并且它们都有 Log4J2Plugins.dat
文件。
ERROR File contains an invalid element or attribute "TestFilter"
我正在使用log4j-api-2.4.jar
, log4j-core-2.4.jar
, log4j-jcl-2.4.jar
, log4j-web-2.4.1.jar
, commons-logging-1.1.1.jar
EAR 中的 jar 。
我已经定义了一个自定义模式转换器插件,并期望该转换器应用于所有模式布局,包括使用<patternlayout>
定义的默认模式布局。 。这是对的吗?
如果是的话,如果有人遇到这个问题,请提供帮助,如果我在定义自定义插件时出错,请指导我,因为它们都没有从 EAR 中的 jar 中检测到。
最佳答案
编译自定义Plugin时,Log4J pom.xml定义了一个插件,该插件会自动在文件META-INF/org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat中生成缓存数据您可以在 Maven 项目中的目标/类下看到它。
log4j-core-2.x.x.jar 还包含定义其缓存数据的 Log4j2Plugins.dat。
问题是使用 ShrinkWrap 测试 EAR 时会创建一个 JAR,并且通常会将 log4j-core-2.x.x.jar Log4j2Plugins.dat 添加到测试 JAR 中,因为它很可能位于类路径中的第一个。
这意味着您的自定义插件缓存丢失。
使用 ShrinkWrap 的解决方案是创建一个新的 Log4j2Plugins.dat,将任何所需的自定义插件缓存文件与核心合并,然后将其添加到 JAR 中。
以下函数实现了...
private static void mergeLog4J2Log4j2PluginsFile(JavaArchive ja, Class... uniqueJARClasses) {
// @Author: Johnathan Ingram <jingram@rogueware.org>
// Log4J2 uses /META-INF/org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat within a JAR to define custom plugins
// This is automatically generated by the plugin defined in the log4j-core-2.x.x pom.xml when compiling your custom plugin
// The problem with shrinkwrap is that the JAR is not preserved and only a single
// /META-INF/org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat
// file can exist as JAR files cannot be added to a JAR file as a library.
// This is normally the default contained in log4j-core-2.x.x.jar which does not expose any custom plugins
// To rectify, both the core and the custom plugin JAR file Log4j2Plugins.dat need to be merged into a single Log4j2Plugins.dat
try {
// List of a unique class in each JAR containing a Log4j2Plugins.dat requiring merging
Vector<URL> datUrls = new Vector<URL>();
for (Class klass : uniqueJARClasses) {
// Find the JAR the class belongs to
URL classLoc = klass.getProtectionDomain().getCodeSource().getLocation();
URL resourceURL = classLoc.toString().endsWith(".jar")
? new URL("jar:" + URLDecoder.decode(classLoc.toString(), "UTF-8") + "!/META-INF/org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat")
: new URL(URLDecoder.decode(classLoc.toString(), "UTF-8") + "/META-INF/org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat");
datUrls.add(resourceURL);
}
// Use the Log4J2 PluginCache to build a merged Log4j2Plugins.dat
File mergedDatFile = new File("target/Log4j2Plugins.dat");
try (FileOutputStream fo = new FileOutputStream(mergedDatFile)) {
org.apache.logging.log4j.core.config.plugins.processor.PluginCache pc = new org.apache.logging.log4j.core.config.plugins.processor.PluginCache();
pc.loadCacheFiles(datUrls.elements());
pc.writeCache(fo);
}
// Replace the default Log4j2Plugins.dat if present
ja.delete("/META-INF/org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat");
ja.addAsManifestResource(mergedDatFile, "org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat");
} catch (Exception ex) {
ex.printStackTrace(System.err);
}
}
使用您的示例运行:
JavaArchive ja = ShrinkWrap.create(JavaArchive.class, "my-test.jar");
...
mergeLog4J2Log4j2PluginsFile(ja, org.apache.logging.log4j.core.config.plugins.processor.PluginCache.class, EventPatternConverter.class);
关于java - 使用 log4j2 API 在 EAR 中未检测到自定义插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33214821/
不同的 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
我是一名优秀的程序员,十分优秀!