Specifying a different config file for Log4J

Steve Neal Java Programming 3 Comments

Generally, adding a log4j.properties file to the classpath is all that needed to bootstrap your Log4J runtime. In certain cases though you might want to override this behaviour.

I needed to do this recently and it took me a while to find out how to do it in the Log4J docs, so I though I’d make a note of how to do it here.

To specify a file that Log4J will use instead of the default, pass its details as a system property when starting the JVM as follows:


java -Dlog4j.configuration=resources/log4j_dev.properties

The above example will load the log4j_dev.properties file from within the resources folder in the classpath. A file URL may also be specified:


java -Dlog4j.configuration=file:/resources/log4j_dev.properties

This will cause the log4j_dev.properties file to be loaded from the resources folder at the root of the file system.

Comments 3

  1. Will Handley

    The question is, how do you then confirm that it picked up the file you wanted it to pick up, and not the default one? There must be some way to have it tell you which file it is using for configuring itself, but I have yet to find it.

Leave a Reply

Your email address will not be published. Required fields are marked *