I want to put all my config files in a /config
subfolder of my application directory. Log4j is expecting the log4j.properties file in the root folder of my application. Is there a way to tell log4j where to look for the properties file?

- 7,399
- 5
- 58
- 106

- 6,258
- 13
- 41
- 68
7 Answers
Yes, define log4j.configuration
property
java -Dlog4j.configuration=file:/path/to/log4j.properties myApp
Note, that property value must be a URL.
For more read section 'Default Initialization Procedure' in Log4j manual.

- 44,836
- 10
- 105
- 121
-
1Where do I put your code `ava -Dlog4j.configuration=file:/path/to/log4j.properties myApp` ? I am using eclipse. My `log4j.properties` file is located in `src/resources`. – Erran Morad Apr 25 '14 at 00:31
-
4@Borat. You should really read the manual for Eclipse, but I'll give you a hint. Run->Run Configurations->Arguments Tab -> VM Arguments panel on that tab. – Alexander Pogrebnyak Apr 28 '14 at 17:47
-
Yes. I get that. I have now done it programmatically. Is one method better than the other ? – Erran Morad Apr 28 '14 at 17:51
-
1@Borat. Doing it on the command line gives you more flexibility when setting up development(staging) and production environments, because logging configuration may differ drastically between two. In my opinion, the logger configuration should be written by installation script, not hardcoded into the source code. – Alexander Pogrebnyak Apr 28 '14 at 18:02
-
Thanks Alex. I am new to this and far from staging and prod. Can you please tell where I can read more about the `installation scripts`, staging and prod issues. I want make my own hands on for this. Chenqui. – Erran Morad Apr 28 '14 at 18:15
-
this is definitely the best way, since, for some reason, putting the path to log4j.properties in the classpath does *not* always work – Francois Jan 30 '15 at 13:25
-
Setting file:/path/to/log4j.properties worked for me. Before I didn't have "file:" which did not load the file successfully. – leeman24 Dec 06 '17 at 17:49
-
The FAQ leads you another way, but is for the XML file approach: https://logging.apache.org/log4j/2.x/faq.html#config_location. This is all much harder to setup than it should be. – Ryan Mar 02 '21 at 20:50
-
For windows you may specify paths in both formats: `-Dlog4j.configuration=file:C:/Users/MyUser/log4j.properties` or `-Dlog4j.configuration=file:C:\\Users\\MyUser\\log4j.properties` – Igor Golovin Jul 01 '21 at 11:21
You can use PropertyConfigurator
to load your log4j.properties wherever it is located in the disk.
Example:
Logger logger = Logger.getLogger(this.getClass());
String log4JPropertyFile = "C:/this/is/my/config/path/log4j.properties";
Properties p = new Properties();
try {
p.load(new FileInputStream(log4JPropertyFile));
PropertyConfigurator.configure(p);
logger.info("Wow! I'm configured!");
} catch (IOException e) {
//DAMN! I'm not....
}
If you have an XML Log4J configuration, use DOMConfigurator instead.

- 87,898
- 29
- 167
- 228
-
The advantage of this method is that you can specify the properties file location on a per-app basis, rather than at the server level. And if you read the location of the properties file from another file outside the app directory structure, you can use it to choose different prop file locations on different servers (e.g., dev, test, prod). Put the code in a load-on-startup servlet and you're good to go. – Gullbyrd Mar 05 '14 at 19:06
-
there is no DOMConfigurator class for log4j2(new version). i need this ability but i couldnt find any way in the newer version. – Fer Apr 28 '14 at 13:27
-
@Buhake Sindi...What you said is correct. But the number of lines of code is too much. if i have to write logs for 90 percent of classes in my web application then what is the best practice. My current work around is that I kept log4j.properties under WEBINF/CLASSESS folder , doing so I don't have to write code for file path, loading file and further calling configure method I just have to initialize a static logger instance variable. If there is much better way please do update here. – MKod May 20 '16 at 18:41
-
@Sindi can you please give working code after PropertyConfigurator.configure(p); because my logs are not printed specified path – pankaj Nov 08 '16 at 19:16
-
-
@Sindi Just as above because my log4j.properties file is in different module. I have to log my logs in same configuration. } – pankaj Nov 08 '16 at 19:30
Use the PropertyConfigurator: PropertyConfigurator.configure(configFileUrl);

- 24,433
- 12
- 63
- 94
Refer to this example taken from - http://www.dzone.com/tutorials/java/log4j/sample-log4j-properties-file-configuration-1.html
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class HelloWorld {
static final Logger logger = Logger.getLogger(HelloWorld.class);
static final String path = "src/resources/log4j.properties";
public static void main(String[] args) {
PropertyConfigurator.configure(path);
logger.debug("Sample debug message");
logger.info("Sample info message");
logger.warn("Sample warn message");
logger.error("Sample error message");
logger.fatal("Sample fatal message");
}
}
To change the logger levels - Logger.getRootLogger().setLevel(Level.INFO);

- 242,637
- 56
- 362
- 405

- 4,563
- 10
- 43
- 72
In Eclipse you can set a VM argument to:
-Dlog4j.configuration=file:///${workspace_loc:/MyProject/log4j-full-debug.properties}

- 31
- 1
- 5
This is my class : Path is fine and properties is loaded.
package com.fiserv.dl.idp.logging;
import java.io.File;
import java.io.FileInputStream;
import java.util.MissingResourceException;
import java.util.Properties;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class LoggingCapsule {
private static Logger logger = Logger.getLogger(LoggingCapsule.class);
public static void info(String message) {
try {
String configDir = System.getProperty("config.path");
if (configDir == null) {
throw new MissingResourceException("System property: config.path not set", "", "");
}
Properties properties = new Properties();
properties.load(new FileInputStream(configDir + File.separator + "log4j" + ".properties"));
PropertyConfigurator.configure(properties);
} catch (Exception e) {
e.printStackTrace();
}
logger.info(message);
}
public static void error(String message){
System.out.println(message);
}
}

- 553
- 5
- 17
You must use log4j.configuration
property like this:
java -Dlog4j.configuration=file:/path/to/log4j.properties myApp
If the file is under the class-path (inside ./src/main/resources/ folder), you can omit the file://
protocol:
java -Dlog4j.configuration=path/to/log4j.properties myApp

- 21,738
- 2
- 113
- 124