本文介绍了如何在log4j2中创建自定义Appender?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 如此链接所述:如何创建自己的Appender log4j?为了在log4j 1.x中创建自定义appender,我们必须扩展AppenderSkeleton类并实现其append方法。For creating a custom appender in log4j 1.x we have to extend the AppenderSkeleton class and implements its append method.类似我们如何在log4j2中创建自定义appender,因为我们没有扩展AppenderSkelton类,所有其他appender扩展AppenderBase类。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 .推荐答案这在log4j2中的作用与在log4j-1.2中的作用完全不同。This works quite differently in log4j2 than in log4j-1.2.在log4j2中,您将为此创建一个插件。本手册中有一个自定义appender示例的说明: http:// logging。 apache.org/log4j/2.x/manual/extending.html#AppendersIn 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扩展组织可能会很方便。 apache.logging.log4j.core.appender.AbstractAppender ,但这不是必需的。It may be convenient to extend org.apache.logging.log4j.core.appender.AbstractAppender, but this is not required.使用 @Plugin(name =MyCustomAppender,.... ,插件名称成为配置元素名称,因此使用自定义appender的配置将如下所示: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:<Configuration packages="com.yourcompany.yourcustomappenderpackage"> <Appenders> <MyCustomAppender name="ABC" otherAttribute="..."> ... </Appenders> <Loggers><Root><AppenderRef ref="ABC" /></Root></Loggers></Configuration>请注意,配置上的 packages 属性是包含自定义log4j2插件的所有包的逗号分隔列表。 Log4j2将在类路径中搜索使用@Plugin注释的类的这些包。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.这是一个打印到控制台的示例自定义appender: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); }}有关插件的更多详情: http://logging.apache.org/log4j/2.x/manual/plugins.htmlFor more details on plugins:http://logging.apache.org/log4j/2.x/manual/plugins.html如果手册不够,在log4j-core中查看内置appender的源代码可能会很有用。If the manual is not enough, it may be useful to look at the source code for the built-in appenders in log4j-core. 这篇关于如何在log4j2中创建自定义Appender?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-29 19:33
查看更多