gpt4 book ai didi

java - 如何在 log4j2 中创建自定义 Appender?

转载 作者:IT老高 更新时间:2023-10-28 20:39:56 26 4
gpt4 key购买 nike

正如此链接中所讨论的:How to create a own Appender in log4j?

为了在 log4j 1.x 中创建自定义 appender,我们必须扩展 AppenderSkeleton 类并实现其 append 方法。

类似地,我们如何在 log4j2 中创建自定义附加程序,因为我们没有要扩展的 AppenderSkelton 类和所有其他附加程序扩展 AppenderBase 类。

最佳答案

这在 log4j2 中的工作方式与在 log4j-1.2 中完全不同。

在 log4j2 中,您将为此创建一个插件。该手册在此处提供了自定义附加程序示例的说明:http://logging.apache.org/log4j/2.x/manual/extending.html#Appenders

扩展 org.apache.logging.log4j.core.appender.AbstractAppender 可能很方便,但这不是必需的。

当您使用 @Plugin(name="MyCustomAppender", .... 注释您的自定义 Appender 类时,插件名称将成为配置元素名称,因此使用您的自定义附加程序的配置将看起来像这样:

<Configuration packages="com.yourcompany.yourcustomappenderpackage">
<Appenders>
<MyCustomAppender name="ABC" otherAttribute="...">
...
</Appenders>
<Loggers><Root><AppenderRef ref="ABC" /></Root></Loggers>
</Configuration>

请注意,配置中的 packages 属性是以逗号分隔的所有带有自定义 log4j2 插件的包的列表。 Log4j2 将在类路径中搜索这些包以查找带有 @Plugin 注释的类。

这是一个打印到控制台的示例自定义附加程序:

package com.yourcompany.yourcustomappenderpackage;

import java.io.Serializable;
import java.util.concurrent.locks.*;
import org.apache.logging.log4j.core.*;
import org.apache.logging.log4j.core.config.plugins.*;
import org.apache.logging.log4j.core.layout.PatternLayout;

// note: class name need not match the @Plugin name.
@Plugin(name="MyCustomAppender", category="Core", elementType="appender", printObject=true)
public final class MyCustomAppenderImpl extends AbstractAppender {

private final ReadWriteLock rwLock = new ReentrantReadWriteLock();
private final Lock readLock = rwLock.readLock();

protected MyCustomAppenderImpl(String name, Filter filter,
Layout<? extends Serializable> layout, final boolean ignoreExceptions) {
super(name, filter, layout, ignoreExceptions);
}

// The append method is where the appender does the work.
// Given a log event, you are free to do with it what you want.
// This example demonstrates:
// 1. Concurrency: this method may be called by multiple threads concurrently
// 2. How to use layouts
// 3. Error handling
@Override
public void append(LogEvent event) {
readLock.lock();
try {
final byte[] bytes = getLayout().toByteArray(event);
System.out.write(bytes);
} catch (Exception ex) {
if (!ignoreExceptions()) {
throw new AppenderLoggingException(ex);
}
} finally {
readLock.unlock();
}
}

// Your custom appender needs to declare a factory method
// annotated with `@PluginFactory`. Log4j will parse the configuration
// and call this factory method to construct an appender instance with
// the configured attributes.
@PluginFactory
public static MyCustomAppenderImpl createAppender(
@PluginAttribute("name") String name,
@PluginElement("Layout") Layout<? extends Serializable> layout,
@PluginElement("Filter") final Filter filter,
@PluginAttribute("otherAttribute") String otherAttribute) {
if (name == null) {
LOGGER.error("No name provided for MyCustomAppenderImpl");
return null;
}
if (layout == null) {
layout = PatternLayout.createDefaultLayout();
}
return new MyCustomAppenderImpl(name, filter, layout, true);
}
}

有关插件的更多详细信息: http://logging.apache.org/log4j/2.x/manual/plugins.html

如果手册不够用,可以看看log4j-core中内置appender的源码。

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

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