13

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?

Community
  • 1
  • 1
Metalhead89
  • 1,740
  • 9
  • 30
  • 58

3 Answers3

9

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.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Ok sorry for my mistake. I changed it to `System.setProperty("my.log", "C:/logfile.log");` I set this in the first line of my main method but I still get the same error – Metalhead89 Aug 07 '12 at 13:12
  • Just to add: `System.out.println(System.getProperty("my.log"));` works fine now. But I still cant change the log path dynamically – Metalhead89 Aug 07 '12 at 13:14
  • `log4j:ERROR setFile(null,false) call failed. java.io.FileNotFoundException: C:\logfile.log (Zugriff verweigert) at java.io.FileOutputStream.open(Native Method)` Zugriff verweigert = access denied – Metalhead89 Aug 07 '12 at 13:16
  • Can you change it to a directory you can access? If you have a `static Log` in the class for your main, this will be called first. You may need to add a static block at the very start of the class which contains your `main()` – Peter Lawrey Aug 07 '12 at 13:20
  • 1
    Thank you I also just got it. I used: `private static Logger logger = null;` and in the Main i wrote: `System.setProperty("my.log", "C:/Workspace/logfile.log"); logger = Logger.getLogger(Main.class.getName());` Now everything works very well:) – Metalhead89 Aug 07 '12 at 13:27
  • 3
    You can do `static { System.setProperty("my.log", "C:/Workspace/logfile.log"); } private static final Logger LOGGER = Logger.getLogger(Main.class.getName());` at the very start. – Peter Lawrey Aug 07 '12 at 13:34
1

You have a misspelling: "{my.log" instead of "my.log"

0

Just set property before instantiating logger, once you initilize the logger setting the property will not worth. just like below:

System.setProperty("my.log", "C:\\src\\com\\web\\automation\\logs\\Application.log");
PP_LOGS = Logger.getLogger("devpinoyLogger");
Jan Doggen
  • 8,799
  • 13
  • 70
  • 144
Niket
  • 1