13

Possible Duplicate:
Dynamically Changing log4j log level

I have a jar file with a log4j properties file included. I tried to use

-Dlog4j.configuration=file:[filename]

to change the properties file at runtime using java, and it didn't work. I need to do this in a sh file Can anyone please tell me how to this? I also tried adding

-Dlog4j.defaultInitOverride=true
Community
  • 1
  • 1
user1741202
  • 121
  • 1
  • 1
  • 6
  • No, I need to do this using a sh file, and VM arguments. There seems to be a way to do this, I just can't get it to work – user1741202 Jan 25 '13 at 18:13
  • Actually, it is not a "runtime" it is "startup time". Have you tried path without `file:` prefix? Are you sure the file exists? According to the [doc](http://logging.apache.org/log4j/1.2/manual.html#Default_Initialization_Procedure), your steps are valid.. – madhead Jan 25 '13 at 18:24

2 Answers2

1

It's possible to override the log4j properties file at runtime. You may fall into this situation if you cann't :

  1. In your jar or somewhere in the code, the log4j system is initialised manually, e.g.

     URL url=loader.getResource(LOGGER_CONFIG_FILE)
     DOMConfigurator.configure(url);
    
Jintian DENG
  • 3,152
  • 20
  • 22
  • This helped! I had to use the default Java classloader instead of the log4j Loader thought. The latter wouldn't work for some reason. – BugInTheCode Aug 01 '17 at 15:35
0

Here's a good,step-by-step tutorial (with good screenshots!) that shows a couple of different alternatives for changing your log4j settings at runtime:

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • Nice tutorial. However, it seems specific to webapps—please correct me if I'm wrong. I think the OP wants to launch their application from a shell script. – Michael Scheper Jun 26 '13 at 08:34
  • I use log4j all the time in standalone Java Swing and console-mode apps. Exactly the same principles apply: just edit an appropriate log4j.properties file :) – paulsm4 Jun 26 '13 at 19:44
  • Good old Swing. :-) So, you're talking about 'just' extracting the appropriate log4j.properties file from the jar, and then either rebuilding the jar, or running the exploded jar? That's a lot more work and mess than just setting a property in a script. – Michael Scheper Jun 28 '13 at 08:41