I have an Eclipse Java Project which uses log4j. I can't set the log4j configuration file to be accessed by file path. I have to export and run the project in a jar.
Here is how i trying:
public class Wita {
static Logger logger;
public static void main(String[] args) {
System.setProperty("log4j.configuration", new File("").getCanonicalPath()+File.separatorChar+"resources"+File.separatorChar+"log4j.xml" );
// System.out.println( System.getProperty("log4j.configuration") );
logger = Logger.getLogger(Wita.class.getName());
}
}
System out prints the C:\Users\roncsak\eclipse_workspace\WITA\resources\log4j.xml which is good. WITA is the base folder of the project. But running the project with -Dlog4j.debug argument the following returns also:
log4j: Trying to find [C:\Users\roncsak\eclipse_workspace\WITA\resources\log4j.xml] using context classloader sun.misc.Launcher$AppClassLoader@18e3e60.
log4j: Trying to find [C:\Users\roncsak\eclipse_workspace\WITA\resources\log4j.xml] using sun.misc.Launcher$AppClassLoader@18e3e60 class loader.
log4j: Trying to find [C:\Users\roncsak\eclipse_workspace\WITA\resources\log4j.xml] using ClassLoader.getSystemResource().
log4j: Could not find resource: [C:\Users\roncsak\eclipse_workspace\WITA\resources\log4j.xml].
I would like to change the log4j.xml over time, without building another jar file. How can I do that?
From "Default Initialization Procedure" at http://logging.apache.org/log4j/1.2/manual.html:
- Set the resource string variable to the value of the
log4j.configuration system property. The preferred way to specify the
default initialization file is through the log4j.configuration system
property. In case the system property log4j.configuration is not
defined, then set the string variable resource to its default value
"log4j.properties".
- Attempt to convert the resource variable to a URL.
- If the resource variable cannot be converted to a URL, for example due
to a MalformedURLException, then search for the resource from the
classpath by calling
org.apache.log4j.helpers.Loader.getResource(resource, Logger.class)
which returns a URL. Note that the string "log4j.properties"
constitutes a malformed URL. See Loader.getResource(java.lang.String)
for the list of searched locations.
So you need to prepend file:
to log4j.configuration
property value in order that it can be treated as URL.
See https://stackoverflow.com/a/7927278/603516.
Even better code:
System.setProperty("log4j.configuration", new File("resources", "log4j.xml").toURL());
You can set VM argument : -Dlog4j.configuration='path_to_log4j.xml'
or programatically :
String logFilePath = new File(<path_to_log4j.xml>);
if (logFilePath == null || "".equalsIgnoreCase(logFilePath)) {
URL file = this.getClass().getResource(DEFAULT_CONF);
DOMConfigurator.configure(file);
} else {
DOMConfigurator.configure(<default_config_file>);
}
All these above answer didn't satisfy so I post here for someone who might needs this in the future:
- You need to specify where your property is. Either by program or command option is fine.
Programatically:
private static void setupLog4J(){
try {
System.setProperty("log4j.configuration", new File(".", File.separatorChar+"log4j.properties").toURL().toString());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
- Create your own log4j.properties file:
# Logging level
# Root logger option
log4j.rootLogger=DEBUG, stdout, file
# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=c:/project/resources/t-output/log4j-application.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
Hope this helps.