从Log4j 1.x迁移到Log4j 2.x

dxxyhpgq  于 2022-11-06  发布在  其他
关注(0)|答案(3)|浏览(251)

我正在尝试转换一个类,这个类是以前在log4j 1.x中使用AppenderSkeleton编写的,现在我正在尝试将我的服务迁移到log4j 2.6。
代码:

package com.amazon.digital.music.purchasing.scheduler.test;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggingEvent;

/**
 * A log appender which collects the log messages.
 * Automatically appends itself to the root logger.
 * 
 */
 class LogMessagesCollector extends AppenderSkeleton {
 private final List<String> messages = new ArrayList<String>();

 public LogMessagesCollector() {
     final Logger logger = Logger.getRootLogger();
     logger.addAppender(this);
 }

 @Override
 public boolean requiresLayout() {
     return false;
 }

 @Override
 protected void append(final LoggingEvent loggingEvent) {
     messages.add(loggingEvent.getMessage().toString());
 }

 @Override
 public void close() {
     final Logger logger = Logger.getRootLogger();
     logger.removeAppender(this);
 }

 public List<String> getMessages() {
     return messages;
 }

我循着链接:How to Create a Custom Appender in log4j2?,以了解自定义追加器是如何在log4j 2中编写的,但无法编写一个类来替换这个类。
任何帮助都将是伟大的。提前感谢!

xtupzzrd

xtupzzrd1#

请看一下log4j-core模块的单元测试中的ListAppender。它类似于在列表中收集日志事件。它有一些其他的特性来简化测试,但它可以作为自定义appender的一个很好的模板。

0wi1tuuw

0wi1tuuw2#

遇到同样的问题。这里是自定义解决方案的模板
log4j2.properties

name = PropertiesConfig
property.filename = logs
appenders =  basic

appender.basic.type = Basic
appender.basic.name = Basic
appender.basic.layout.type= PatternLayout
appender.basic.layout.pattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

rootLogger.level = debug
rootLogger.appenderRefs = flume
rootLogger.appenderRef.flume.ref = Basic

BasicAppender.java

import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
import org.apache.logging.log4j.core.config.plugins.PluginElement;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import org.apache.logging.log4j.core.layout.PatternLayout;

import java.io.Serializable;

@Plugin(name = "Basic", category = "Core", elementType = "appender", printObject = true)
public class BasicAppender extends AbstractAppender {

    private static volatile BasicAppender instance;

    public BasicAppender(final String name, final Filter filter, final Layout<? extends Serializable> layout){
        super(name, filter, layout);
    }

    @PluginFactory
    public static BasicAppender createAppender(@PluginAttribute("name") String name,
                                               @PluginElement("Layout") Layout layout,
                                               @PluginElement("Filters") Filter filter){
        if (layout == null){
            layout = PatternLayout.createDefaultLayout();
        }
        return new BasicAppender(name, filter, layout);
    }

    public static BasicAppender getInstance(){
        return instance;
    }

    @Override
    public void append(final LogEvent event){
        //custom actions
    }
}
p4tfgftt

p4tfgftt3#

这是你唯一能做的事

import org.apache.logging.log4j.core.*;
import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.config.Property;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
import org.apache.logging.log4j.core.config.plugins.PluginElement;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import org.apache.logging.log4j.core.layout.PatternLayout;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Plugin(name = "LogMessagesCollector", category = Core.CATEGORY_NAME, elementType = Appender.ELEMENT_TYPE, printObject = true)
public class LogMessagesCollector extends AbstractAppender {

    private static List<String> messages = new ArrayList<String>();

    private static volatile CustomAppender instance;

    public LogMessagesCollector(final String name, final Filter filter, final Layout<? extends Serializable> layout, final boolean ignoreExceptions, final Property[] properties) {
        super(name, filter, layout, ignoreExceptions, properties);
    }

    @PluginFactory
    public static LogMessagesCollector createAppender(@PluginAttribute("name") String name,
                                                     @PluginAttribute("ignoreExceptions") boolean ignoreExceptions,
                                                     @PluginElement("Layout") Layout layout,
                                                     @PluginElement("Filters") Filter filter,
                                                     @PluginElement("properties") Property[] properties) {
        if (layout == null) {
            layout = PatternLayout.createDefaultLayout();
        }

        instance = new LogMessagesCollector(name, filter, layout, ignoreExceptions, properties);
        return instance;
    }

    public static LogMessagesCollector getInstance() {
        return instance;
    }

    @Override
    public void append(LogEvent event) {
        messages.add(event.getMessage().getFormattedMessage());
    }

    public static List<String> getMessages() {
        return messages;
    }

    public static void clear() {
        messages.clear();
    }

}

上述类的配置可以是log4j2.xml,将其放在资源文件夹中。

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="TRACE" packages="com.example.demo">
    <Appenders>
        <LogMessagesCollector name="CustomAppender" >
            <PatternLayout pattern="=== %d [%.4t] %-5p %c{1} - %m%n" />
        </LogMessagesCollector>
    </Appenders>

    <Logger name="com.example.demo" level="INFO">
        <appender-ref ref="CustomAppender" level="INFO"/>
    </Logger>

</Configuration>

相关问题