[Original] log4j path Solution
Posted on magic. Yang read (348) Comments (0) EDIT add to favorites category: Java note There are two major log4j path problems: 1. log4j. properties configuration file path; 2. Output log file path;
1. log4j. properties configuration file path Solution
It is the same as reading and writing other java files. In a web application, you can use the following two methods:
Method 1: Put the log4j. properties file in the project's WEB-INF directory, and then initialize the configuration of the log4j. properties file through the following code:
Servletcontext context = This. getservlet (). getservletcontext ();
Propertyconfigurator. Configure (context. getrealpath ("/") + "/WEB-INF/log4j. properties ");
Here this. getservlet () is the method in the struts action class. If it inherits httpservlet directly, the application environment is obtained through the following code.
Servletcontext context = getservletcontext ();
Method 2: Search for log4j. properties directly. Take Tomcat as an example. If your log4j. properties file is placed under project/conf (the project is the root directory of the Application), you can find it as follows: Propertyconfigurator. Configure (../webapps/project/CONF/log4j. properties );
Why is this true? In tomcat, all configuration files are based on the bin directory of Tomcat. The actual reasons are unclear.
2. Log File Path Solution
This is the same as method 2 in the previous problem. For example, if I want to output logs to the logs directory of the project, write the logs in the log4j. properties file as follows: Log4j. appender. R. File = ../webapps/project/logs/log4j. Log
Here, we can see that the default root directory of the configuration in the Tomcat deployed application is % catalina_home %/bin, in the future, it will be no longer difficult to solve similar Path Problems from here.