Preface
I've been saying, how to read the Log4j profile log4j.properties.
The next discussion is: How to set the path of the log file created in log4j.
Tomcat's okay. For complex WebLogic, this is how to set the path, because it doesn't understand how to press the war file.
In the last article
Also mentioned in the log (log4j) configuration series (WebLogic application log4j) in Tomcat and WebLogic , if there is no configuration path, Logfile.log is also placed in the Base_domain directory by default.
Of course you configure an absolute path, but this is definitely not recommended.
You can set the relative path through ${param}/logfile.log.
In the Java Fetch System variables (environment variables and settings variables) , it is mentioned that log4j configuration can configure system variables.
Based on this, there are 2 ways to configure the relative path of the log4j.
method One: set the LOG_DIR system variable through the servlet initialization init () method
The variable name of this system can be arbitrarily taken, just make sure that the name of the variable used in the Init method and log4j is consistent.
System.setproperty ("Log_dir", "c:\\oracle\\middleware\\user_projects\\domains\\base_domain\\logs");
Of course, the Log_dir configured here cannot use an absolute path.
Can be passed Getservletcontext (). Getrealpath ("/"); And so on some of these methods to navigate to some relative paths.
Of course, this configuration can also be directly set in the configuration file Log4j.appender.logfile.File
String logFile = spath+ "/" + Ps.getproperty ("Log4j.appender.logfile.File");
Ps.setproperty ("Log4j.appender.logfile.File", logfile);
Propertyconfigurator.configure (PS);
method Two: Through server environment variables
To deal with the inconvenience of a method, you also need to get some paths through the API.
Is it possible to directly use the already initialized system variables inside the Web server? Of course it can.
In the Windows environment, in the WebLogic. cmd file, there are
Set Java_properties=-dplatform.home=%wl_home%-dwls.home=%wls_home%-dweblogic.home=%wls_home%
-D variables for such a setting are written to the system variable.
So the above example uses ${platform.home} directly; ${wls.home} is OK.
In Tomcat, there are similar ${catalina.home} ...