使用Log4j2 2.8.1在运行时动态添加文件日志记录 [英] Add file logging dynamically at runtime with Log4j2 2.8.1

查看:161
本文介绍了使用Log4j2 2.8.1在运行时动态添加文件日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以编程方式添加文件记录,并动态生成文件名。

I am in need of adding programmatically a file logging, with file name generated dynamically.

我的代码是这样的:

private static final Logger LOGGER = LogManager.getLogger(Archiver.class);

public static void openLogfile(String folder) {
    String dateTime = "TODO";
    String fileName = folder + "upload" + dateTime + ".log";
    LOGGER.info("Opening " + fileName + " for logging.");
    // setting up a FileAppender dynamically...
    final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    final Configuration config = ctx.getConfiguration();
    Layout layout = PatternLayout.createLayout(PatternLayout.SIMPLE_CONVERSION_PATTERN, null, config, null,
            null, true, true, null, null);
    Appender appender = FileAppender.createAppender(fileName, "false", "false", "File", "true",
            "false", "false", "4000", layout, null, "false", null, config);
    appender.start();
    config.addAppender(appender);
    AppenderRef ref = AppenderRef.createAppenderRef("File", null, null);
    AppenderRef[] refs = new AppenderRef[]{ref};

    LoggerConfig loggerConfig = LoggerConfig.createLogger(true, Level.DEBUG, "org.apache.logging.log4j", "", refs,
            null, config, null);

    loggerConfig.addAppender(appender, null, null);
    config.addLogger("org.apache.logging.log4j", loggerConfig);
    ctx.updateLoggers();

}

我看了一下配方如何以编程方式在运行时添加Log4J2 appender? http://logging.apache.org/log4j/2.x/manual/customconfig。 html#AddingToCurrent

我的问题是:


  • 上面说的log4j2 doc上的例子不会编译,它已经过时了,不得不添加几个参数而不知道它们是什么,通常我添加了空值。

  • 现在不推荐使用文档中使用的方法;

  • 正在创建文件但是虽然在控制台上输出了,但是没有日志出现,程序退出后,文件中也没有任何内容被刷新。

请有人请提供一个带有最新API的下降示例log4j2动态添加文件记录?我使用org.apache.logging.log4j 2.8.1。

Could somebody please provide a descent example with a most recent API to log4j2 to dynamically add file logging? I use org.apache.logging.log4j 2.8.1.

推荐答案

根据评论中的其他信息,这是我的建议。我不认为你需要以你在问题中提到的所有原因进行编程。

Based on additional information in the comments, here is my suggestion. I don't think you need to do this programmatically for all the reasons you mentioned in your question.

相反,你可以使用类似下面的例子来配置log4j2系统。请注意,您不一定需要我只是用于测试的控制台appender。

Instead you can configure the log4j2 system using something like the following example. Note that you do not necessarily need the console appender I simply used that for testing.

log4j2.xml内容:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="STDOUT" target="SYSTEM_OUT">
            <PatternLayout pattern="[%date{ISO8601}][%-5level][%t] %m%n" />
        </Console>
        <File
            fileName="logs/${ctx:fileName}.txt"
            name="logFile">
            <PatternLayout>
                <Pattern>[%date{ISO8601}][%-5level][%t] %m%n</Pattern>
            </PatternLayout>
        </File>
    </Appenders>
    <Loggers>
        <Logger name="example" level="TRACE" additivity="false">
            <AppenderRef ref="STDOUT" />
            <AppenderRef ref="logFile" />
        </Logger>
        <Root level="WARN">
            <AppenderRef ref="STDOUT" />
        </Root>
    </Loggers>
</Configuration>

Java代码:

package example;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.ThreadContext;

public class LogFileNameBasedOnArg0Main {

    public static void main(String[] args) {
        ThreadContext.put("fileName", args[0]);
        Logger log = LogManager.getLogger();
        log.info("Here's some info!");
    }

}

输出:

我使用myFile的程序参数生成了文件:logs / myFile.txt,其中包含以下内容:

I used a program argument of "myFile" which generated the file: logs/myFile.txt with the following content:

[2017-05-03T11:20:37,653][INFO ][main] Here's some info!

您应该可以修改此示例以满足您的需求,您无需执行任何操作程序化配置,从而避免了你提到的问题。

You should be able to modify this example to meet your needs and you won't have to do any programmatic configuration thus avoiding the issues you mentioned.

这篇关于使用Log4j2 2.8.1在运行时动态添加文件日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆