Log4j Path Problems

Source: Internet
Author: User
[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.

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.