Log4J动态更改文件路径 [英] Log4J change File path dynamically
问题描述
我已经阅读了很多页面,几乎每个人都告诉我应该使用系统属性如下:
如何动态更改log4j日志文件?
所以我的log4j.properties文件如下所示:
log4j.logger.JDBC_LOGGER = INFO,jdbcTests
log4j.additivity.JDBC_LOGGER = false
log4j.appender.jdbcTests = org.apache.log4j.FileAppender
log4j .appender.jdbcTests.File = $ {my.log}
log4j.appender.jdbcTests.layout = org.apache.log4j.PatternLayout
log4j.appender.jdbcTests.append = false
log4j .appender.jdbcTests.layout.ConversionPattern =%d {yyyy mm dd HH:mm:ss}%5p%C:行%L - %m%n
在我的主要方法中,我将设置我的新系统属性:
System.setProperty({my.log,C:/ l ogfile.log);
但是我只是收到一个错误:
log4j:ERROR setFile(null,false)调用失败。
java.io.FileNotFoundException:
java.io.FileOutputStream.open(Native Method)....
当我尝试读取我的系统属性:
System.out.println (System.getProperty( my.log));
它返回null。
我做错了什么?
我觉得你的意思是my.log不是{my.log
System.setProperty(my.log,C:/logfile.log);
我不会想像一旦记录开始就可以改变,所以你需要设置为
BTW:您可以使用FileAppender子类使其以任何您喜欢的方式运行。
I want to change the path and file name of my log4j logfile dynamically.
I have read a lot of pages and nearly every tell me that I should use system properties like here: how to change the log4j log file dynamically?
So my log4j.properties file looks like this:
log4j.logger.JDBC_LOGGER=INFO,jdbcTests
log4j.additivity.JDBC_LOGGER = false
log4j.appender.jdbcTests=org.apache.log4j.FileAppender
log4j.appender.jdbcTests.File=${my.log}
log4j.appender.jdbcTests.layout=org.apache.log4j.PatternLayout
log4j.appender.jdbcTests.append = false
log4j.appender.jdbcTests.layout.ConversionPattern=%d{yyyy mm dd HH:mm:ss} %5p %C:Line %L - %m%n
In my main method I am going to set my new system property:
System.setProperty("{my.log", "C:/logfile.log");
But I just get an error:
log4j:ERROR setFile(null,false) call failed.
java.io.FileNotFoundException:
at java.io.FileOutputStream.open(Native Method)....
And when I try to read my set system property with:
System.out.println(System.getProperty("my.log"));
it return null. What do I do wrong?
I think you meant "my.log" not "{my.log"
System.setProperty("my.log", "C:/logfile.log");
I wouldn't imagine you can change this once the logging has started so you need to set this as early in your program as possible.
BTW: You can sub-class FileAppender to make it behave any way you like.
这篇关于Log4J动态更改文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!