Log4J –运行时变量替换
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.loglog4j.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
变量替换是 PropertyConfigurator 的一项功能,而不是的模式布局。如果你查看你的代码,你永远不会定义 file_pattern 应该是什么。但为什么需要在代码中进行变量替换呢?只需这样做:
如果您想在其他地方引用该字符串,只需将其设为常量即可。
编辑:您必须读取属性对象,但 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:
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.
您可以手动加载它:
You can load it manually: