appender log4j 扩展_如何在log4j2中创建自定义Appender?

本文介绍了在Log4j 1.x和2.x中创建自定义Appender的方法。Log4j 1.x需扩展AppenderSkeleton类并实现append方法;Log4j 2.x则要创建插件,可扩展AbstractAppender类,还给出了打印到控制台的自定义Appender示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

For creating a custom appender in log4j 1.x we have to extend the AppenderSkeleton class and implements its append method.

Similarly How we can create a custom appender in log4j2 as we dont have AppenderSkelton class to extend and all other appender extend AppenderBase class .

解决方案

This works quite differently in log4j2 than in log4j-1.2.

In log4j2, you would create a plugin for this. The manual has an explanation with an example for a custom appender here: http://logging.apache.org/log4j/2.x/manual/extending.html#Appenders

It may be convenient to extend org.apache.logging.log4j.core.appender.AbstractAppender, but this is not required.

When you annotate your custom Appender class with @Plugin(name="MyCustomAppender", ...., the plugin name becomes the configuration element name, so a configuration with your custom appender would then look like this:

...

Note that the packages attribute on the configuration is a comma-separated list of all the packages with custom log4j2 plugins. Log4j2 will search these packages in the classpath for classes annotated with @Plugin.

Here is a sample custom appender that prints to the console:

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);

}

}

If the manual is not enough, it may be useful to look at the source code for the built-in appenders in log4j-core.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值