Log4J – Runtime variable substitution

混江龙づ霸主 提交于 2019-12-06 02:21:26

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.

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"));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!