Log4J - 运行时变量替换 [英] Log4J – Runtime variable substitution

查看:333
本文介绍了Log4J - 运行时变量替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Log4J 似乎有一个恼人的限制 - 在运行时,变量替换似乎不起作用。

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

在此示例中

文件:Log4j.properties

File: Log4j.properties


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

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

log4j.rootLogger = DEBUG ,FileAppender

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=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

log4j.appender.FileAppender.Threshold=ERROR

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

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

文件:log4jtest1.log

File: log4jtest1.log


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

ERROR Sample error message FATAL Sample fatal message

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

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输出

文件:log4jtest2.log

File: log4jtest2.log


$ {file_pattern} $ {file_pattern}

${file_pattern}${file_pattern}

任何人都可以解释问题是什么以及如何解决?

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

相关问题:申请是否可以访问ResourceBundle以读取要替换的变量?

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

推荐答案

变量替换是 PropertyConfigurator 不是PatternLayout。如果查看代码,则永远不要定义file_pattern应该是什么。但为什么你需要代码中的变量替换?只需这样做:

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.

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

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.

这篇关于Log4J - 运行时变量替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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