Configure the relative path of the log file in Log4j

Source: Internet
Author: User

Configure the relative path of the log file in Log4j

Method 1: the solution is to replace the absolute path with the relative path. In fact, the FileAppender of log4j has such a mechanism, such as log4j. appender. logfile. file =$ {WORKDIR}/logs/app. log
"$ {WORKDIR}/" is a variable and will be replaced by the value of "WORKDIR" in System Property. In this way, you can use System. setProperty ("WORKDIR", WORKDIR); To set the root path before loading the configuration file in log4j. This operation can be performed through an initial servlet.

Method 2: Use server environment variables
The configuration file of log4j supports the environment variables of the vm on the server. The format is similar to $ {catalina. home}
Log4j. appender. R = org. apache. log4j. RollingFileAppender
Log4j. appender. R. File =$ {catalina. home}/logs/logs_tomcat.log
Log4j. appender. R. MaxFileSize = 10KB
$ {Catalina. home} is not an environment variable in windows. You do not need to set this variable in the environment variable of Windows. In this case, you can see that tomcat \ bin \ catalina. bat (startup and shutdown call this) comes with-Dcatalina. home = "% CATALINA_HOME % ". Inherit this idea, so you can also set a parameter-Dmylog. home = "D:/abc/log" to the vm parameter started by the corresponding Server java.

Method 3: load the file attribute in the init () method initialized by servlet to implement the relative path
Specific implementation: Make a servlet and read the properties file to a properties file during system loading. change the attribute value of the file (I used a relative directory) (add the system root directory), and set the properties object to propertyConfig, in this way, the log settings are initialized. you do not need to configure it later.
Generally, the log4j log output path is fixed to a folder during our development project. If I change the environment, the Log Path needs to be modified again, which is inconvenient, currently, I have dynamically changed the Log Path to save log files in relative paths.
(1). When the project is started, load the initialization class:
Public class Log4jInit extends HttpServlet {
Static Logger logger = Logger. getLogger (Log4jInit. class );
Public Log4jInit (){
}

Public void init (ServletConfig config) throws ServletException {
String prefix = config. getServletContext (). getRealPath ("/");
String file = config. getInitParameter ("log4j ");
String filePath = prefix + file;
Properties props = new Properties ();
Try {
FileInputStream istream = new FileInputStream (filePath );
Props. load (istream );
Istream. close ();
// ToPrint (props. getProperty ("log4j. appender. file. File "));
String logFile = prefix + props. getProperty ("log4j. appender. file. File"); // set the path
Props. setProperty ("log4j. appender. file. File", logFile );
PropertyConfigurator. configure (props); // load log4j configuration information
} Catch (IOException e ){
ToPrint ("cocould not read configuration file [" + filePath + "].");
ToPrint ("Ignoring configuration file [" + filePath + "].");
Return;
}
}

Public static void toPrint (String content ){
System. out. println (content );
}
}
In fact, log4j. properties is the default name of log4j configuration file, which can be placed anywhere in the classpath that can be read by JVM. It is generally placed in the WEB-INF/classes directory. When the log4j configuration file is no longer the default name, you need to load and provide parameters, such as "ropertyConfigurator. configure (props); // load log4j configuration information"

(2). configuration in Web. xml
<Servlet>
<Servlet-name> log4j-init </servlet-name>
<Servlet-class> Log4jInit </servlet-class>
<Init-param>
<Param-name> log4j </param-name>
<Param-value> WEB-INF/classes/log4j. properties </param-value>
</Init-param>
<Load-on-startup> 1 </load-on-startup>
</Servlet>

Note: The above load-on-startup is set to 0 to load the Servlet when the Web Container starts. The log4j. properties file is placed in the root properties subdirectory, or in other directories. The. properties file should be stored in a centralized manner to facilitate management.
(3) In. log4j. properties, you can configure log4j. appender. file. File as the relative path of the current application.

Log4j getting started

Configure Log4j in Hibernate to display SQL Parameters

Log4j learning notes (1) _ Log4j basics & configuration item Parsing

Log4j learning notes (2) _ Log4j configuration example & integrate Log4j with Spring

For more details, please continue to read the highlights on the next page:


 

  • 1
  • 2
  • Next Page

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.