如何在log4j2中实现多文件appender

时间:2015-10-05 13:05:15

标签: java java-ee log4j log4j2

我正在将我的应用程序从log4j迁移到log4j2。请在下面找到从应用程序当前线程获取输入的类(例如,模式为mode1)并将其附加到日志文件名(例如log_mode1.log)并创建appender并更新日志文件。 / p>

类别:

public class MultiFileAppender extends AppenderSkeleton {
    ...

@Override
    protected void append(LoggingEvent event) {
     ...
}
@Override
    public void close() {
}
@Override
    public boolean requiresLayout() {
     ...
 }
}

log4j.xml中的Appender内容:

<appender name="TEST_MULTIFILE" class="com.test.it.logging.MultiFileAppender">
<param name="File" value="${LOGS}/test/test_%id%.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="500KB"/>
<param name="MaxBackupIndex" value="5"/>
<param name="Encoding" value="UTF-8"/>
</appender>

如何在java中获取这些参数并创建多文件appender,如log4j2中的上述配置?请帮忙。感谢。

1 个答案:

答案 0 :(得分:1)

采取以下方案:

为每个实例使用不同的id多次实例化一个类。记录相应的ID应该转到它自己的文件。每个id一个文件。编写代码时,id的值是未知的(当然是在本例中编写Main类时)。

public class Test implements Runnable {
    private static final Logger LOG = LogManager.getLogger();
    private final String id;

    public Test(String id) {
        this.id = id;
    }

    @Override
    public void run() {
        ThreadContext.put("id", id);    //org.apache.logging.log4j.ThreadContext
        while (true) {
            try { Thread.sleep(10000); } catch (InterruptedException e) { break; }
            LOG.info("{}: I have been sleeping for 10 seconds", id);
        }
    }
}
public class Main {
    private static final Logger LOG = LogManager.getLogger();

    public static void main(String[] args) {
        LOG.info("Starting thread 1");
        Thread t1 = new Thread(new Test("mode1"));
        t1.start();
        LOG.info("Starting thread 2");
        Thread t2 = new Thread(new Test("mode2"));
        t2.start();
        LOG.info("Starting thread 3");
        Thread t3 = new Thread(new Test("mode3"));
        t3.start();
    }
}

以下是log4j2的配置。

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
    <Appenders>
        <Routing name="Routing">
            <Routes pattern="$${ctx:id}">
                <!-- If a special id should be treated differently
                <Route key="mymode">
                    ...
                </Route> -->
                <!--  Threads that don't have the id value set -->
                <Route key="$${ctx:id}">
                    <File name="File"
                            fileName="normal.log">
                        <PatternLayout pattern="%d{HH:mm:ss} [%p] %c %msg%n" />
                    </File>
                </Route>
                <!-- Threads with the id value set, and is not one of the special ones above -->
                <Route>
                    <File name="File-${ctx:id}"
                            fileName="id-${ctx:id}.log">
                        <PatternLayout pattern="%d{HH:mm:ss} [%p] %c %msg%n" />
                    </File>
                </Route>
            </Routes>
        </Routing>
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="Routing" />
        </Root>
    </Loggers>
</Configuration>

这个想法取自log4j2 FAQ。另请参阅Lookups

如果您仍然需要让自定义appender与log4j2一起使用,那么this question with answer可能会为您提供一些有关如何继续的好提示。