156

What variables do I have to set/pass as arguments to the JVM to get Log4j to run properly? And by properly I mean not complain and print to the console. Can I see a typical example?

Note: I need to avoid creating a log4j.properties file in the application.

informatik01
  • 16,038
  • 10
  • 74
  • 104
jconlin
  • 3,806
  • 6
  • 31
  • 32
  • 5
    Reader beware: important to notice that the question is asking about log4j, not log4j2. – neves Apr 12 '19 at 22:57

10 Answers10

195

Do you have a log4j configuration file ? Just reference it using

-Dlog4j.configuration={path to file}

where {path to file} should be prefixed with file:

Edit: If you are working with log4j2, you need to use

-Dlog4j.configurationFile={path to file}

Taken from answer https://stackoverflow.com/a/34001970/552525

FUD
  • 5,114
  • 7
  • 39
  • 61
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • Brian - not exactly what I was looking for, but I think it is a decent compromise between moving a file into my application and setting the properties via JVM args. – jconlin Apr 23 '09 at 14:21
  • I think that as your application becomes more complex (if it will) then the file becomes more manageable as you configure it more. However I confess I'm guessing as to your use case here. – Brian Agnew Apr 23 '09 at 15:35
  • In case of logback.xml, -Dlogback.configuration={path to file} is not working. Any suggestion? – Sai prateek Oct 22 '13 at 12:51
  • 33
    `{path to file}` needs to be prepended with `file:` for this to work. – O. R. Mapper Feb 27 '14 at 17:33
  • 2
    @O.R.Mapper - I've amended appropriately – Brian Agnew Feb 27 '14 at 17:35
  • Can you tell me how can we specify {path to file} in Linux eclipse – Kunal Apr 16 '15 at 08:25
  • 8
    Watch out if using **Log4j 2** as the property name and syntax changed. See [this answer](http://stackoverflow.com/a/34001970/2245051). – José Andias Aug 22 '16 at 11:12
  • @BrianAgnew, what is difference between `-J-Dlog4j.ConfigurationFile` and `-Dlog4j.ConfigurationFile`? – krishna_5c3 Apr 18 '19 at 16:23
169

The solution is using of the following JVM argument:

-Dlog4j.configuration={path to file}

If the file is NOT in the classpath (in WEB-INF/classes in case of Tomcat) but somewhere on you disk, use file:, like

-Dlog4j.configuration=file:C:\Users\me\log4j.xml

More information and examples here: http://logging.apache.org/log4j/1.2/manual.html

30thh
  • 10,861
  • 6
  • 32
  • 42
48

This configuration seems to have changed in Log4j 2:

-Dlog4j.configurationFile=file:C:\Users\me\log4j.xml

See: https://logging.apache.org/log4j/2.x/manual/configuration.html

informatik01
  • 16,038
  • 10
  • 74
  • 104
D. L.
  • 481
  • 4
  • 5
  • This gives me the following exception. C:\Development\Eclipses\EclipseMars\file:C:\Users\ABC\log4j2.xml How do I make the path absolute – Cybermonk Jun 26 '16 at 07:19
  • For Docker Containers, I used `JAVA_PARAMS="-Dlog4j.configurationFile=classpath:log4j2-container.xml"` if the Docker Image supports overriding the JAVA_PARAMS with env vars and the file is inside the Jar. – Marcello DeSales Sep 18 '18 at 20:49
21

I know this is already answered, but because you said, this isn't exactly what you are looking for, I would like to point out the following alternative:

You can also use a configuration class instead of the properties or xml file.

-Dlog4j.configuratorClass=com.foo.BarConfigurator

See http://logging.apache.org/log4j/1.2/manual.html for details.

Tim Büthe
  • 62,884
  • 17
  • 92
  • 129
  • 1
    This is the correct solution if someone wants to avoid creating a config file. Inside your own custom Configurator class you could call BasicConfigurator(), or create your own Loggers and Appenders however you want. But I would much prefer a config file! – skiphoppy Aug 30 '12 at 16:34
13

Late to the party as since 2015, Log4J 1.x has reached EOL.

Log4J 2.x onwards the JVM option should be -Dlog4j.configurationFile=<filename>


P.S. <filename> could be a file relative to the class path without the file: as suggested in the other answers.

Vineet Menon
  • 728
  • 2
  • 9
  • 25
11

Generally, as long as your log4j.properties file is on the classpath, Log4j should just automatically pick it up at JVM startup.

Natalia
  • 177
  • 3
  • 1
    This is true: the -Dlog4j.configuration is only needed if the file were not in the classpath or if you want to use a file that is not the first one found in the classpath – WestCoastProjects Nov 14 '14 at 02:03
2

Relative Path is also ok:

java -Dlog4j.configuration=file:".\log4j.properties" -jar com.your-1.0-SNAPSHOT.jar

or

java -Dlog4j.configuration=file:".\log4j.xml" -jar com.your-1.0-SNAPSHOT.jar
Ridox
  • 1,073
  • 13
  • 18
0

If you are using gradle. You can apply 'aplication' plugin and use the following command

  applicationDefaultJvmArgs = [
             "-Dlog4j.configurationFile=your.xml",
                              ]
emanuel07
  • 738
  • 12
  • 27
0

According to the page (Log4j2 - Automatic Configuration) Log4j2 needs:

-Dlog4j2.configurationFile=<path to file>

Log4j will inspect the "log4j2.configurationFile" system property and, if set, will attempt to load the configuration using the ConfigurationFactory that matches the file extension. Note that this is not restricted to a location on the local file system and may contain a URL.

Taken from: Log4j2 - Automatic Configuration

0

I used this two commands below with weblogic 12 and it works for log4j2.xml

-Dlog4j.configurationFile=<path to file>
-Dlog4j2.configurationFile=<path to file>
Adriaan
  • 17,741
  • 7
  • 42
  • 75