Log4J –运行时变量替换

发布于 2024-08-12 07:34:26 字数 1917 浏览 6 评论 0原文

Log4J 似乎有一个烦人的限制——在运行时,变量替换不会出现去工作。

在此示例中

文件:Log4j.properties

file_pattern=%d{ISO8601} %-5p %m%n

log4j.rootLogger=DEBUG,FileAppender

log4j.appender.FileAppender=org.apache.log4j.FileAppender log4j.appender.FileAppender.layout=org.apache.log4j.PatternLayout log4j.appender.FileAppender.layout.ConversionPattern=${file_pattern} log4j.appender.FileAppender.File=log4jtest1.log

log4j.appender.FileAppender.Threshold=错误

log4j.properties 文件中配置的 FileAppender 会生成正确的输出:

文件:log4jtest1.log

错误错误消息示例 FATAL 致命消息示例

如果我尝试在运行时创建 FileAppender

import org.apache.log4j.FileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;

public class Main {
    static final Logger logger = Logger.getLogger(Main.class);

    public static void main(String[] args) throws Exception {

        FileAppender appender = new FileAppender();
        appender.setFile("log4test2.log");

        PatternLayout pl = new PatternLayout("${file_pattern}");

        appender.setLayout(pl);
        appender.setName("log4jtest2");
        appender.setThreshold(Level.ERROR);
        appender.activateOptions(); 
        logger.addAppender(appender);

        logger.trace("Sample trace message");
        logger.debug("Sample debug message");
        logger.info("Sample info message");
        logger.warn("Sample warn message");
        logger.error("Sample error message");
        logger.fatal("Sample fatal message");
    }
}

Te 输出为

File: log4jtest2.log

${file_pattern}${file_pattern}

谁能解释一下问题是什么以及如何解决?

相关问题:应用程序可以访问 ResourceBundle 以读取要替换的变量吗?

Log4J appears to have an annoying restriction – at runtime, variable substitution does not appear to work.

In this example

File: Log4j.properties

file_pattern=%d{ISO8601} %-5p %m%n

log4j.rootLogger=DEBUG, FileAppender

log4j.appender.FileAppender=org.apache.log4j.FileAppender
log4j.appender.FileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.FileAppender.layout.ConversionPattern=${file_pattern}
log4j.appender.FileAppender.File=log4jtest1.log

log4j.appender.FileAppender.Threshold=ERROR

The FileAppender configured in the log4j.properties file produces the correct output:

File: log4jtest1.log

ERROR Sample error message
FATAL Sample fatal message

If I attempt to create a FileAppender at runtime

import org.apache.log4j.FileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;

public class Main {
    static final Logger logger = Logger.getLogger(Main.class);

    public static void main(String[] args) throws Exception {

        FileAppender appender = new FileAppender();
        appender.setFile("log4test2.log");

        PatternLayout pl = new PatternLayout("${file_pattern}");

        appender.setLayout(pl);
        appender.setName("log4jtest2");
        appender.setThreshold(Level.ERROR);
        appender.activateOptions(); 
        logger.addAppender(appender);

        logger.trace("Sample trace message");
        logger.debug("Sample debug message");
        logger.info("Sample info message");
        logger.warn("Sample warn message");
        logger.error("Sample error message");
        logger.fatal("Sample fatal message");
    }
}

Te output is

File: log4jtest2.log

${file_pattern}${file_pattern}

Can anyone explain what is the problem and how can it be fixed?

Related question: Can an application access the ResourceBundle in order to read variables intended to be substituted?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不语却知心 2024-08-19 07:34:26

变量替换是 PropertyConfigurator 的一项功能,而不是的模式布局。如果你查看你的代码,你永远不会定义 file_pattern 应该是什么。但为什么需要在代码中进行变量替换呢?只需这样做:

 PatternLayout pl = new PatternLayout("%d{ISO8601} %-5p %m%n");

如果您想在其他地方引用该字符串,只需将其设为常量即可。

编辑:您必须读取属性对象,但 PropertyConfigurator 可以采用属性对象而不是文件,因此您可以加载该对象,执行您需要执行的操作并将其传递给 PropertiesConfigurator,因此您只有一个配置路径。

Variable substitution is a feature of PropertyConfigurator not of PatternLayout. If you look at your code, you never define what file_pattern should be. But why would you need variable substitution in code? Just do this:

 PatternLayout pl = new PatternLayout("%d{ISO8601} %-5p %m%n");

If you want to reference that string somewhere else, just make it a constant.

Edit: You will have to read the properties object, but PropertyConfigurator can take a properties object instead a file, so you could load that, do what you need to do and pass it on to the PropertiesConfigurator, so you only have one configuration path.

江心雾 2024-08-19 07:34:26

您可以手动加载它:

Properties props = new Properties();
InputStream fis = new FileInputStream(new File("/somewhere/log4j.properties")); 
props.load(fis);
fis.close();
PatternLayout layout = new PatternLayout(props.getProperty("file_pattern"));

You can load it manually:

Properties props = new Properties();
InputStream fis = new FileInputStream(new File("/somewhere/log4j.properties")); 
props.load(fis);
fis.close();
PatternLayout layout = new PatternLayout(props.getProperty("file_pattern"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文