log4j log file storage location settings

Source: Internet
Author: User
Tags log4j

Originated from: http://hbiao68.iteye.com/blog/1947618

Take Dailyrollingfileappender as an example: Suppose a daily log file

The following settings are available:



Log4j.appender.a1.datepattern= '. ' Yyyy-mm-dd


log4j.appender.a1.layout.conversionpattern=%d%5p-%c-%-4r [%t]-%m%n

Unable to find App.log file after testing yourself

To protect a log file: Under the root directory/web-info/logs/, the following 4 solutions are available for individuals:

1 Absolute path


But the wording is very flexible.

The same setting principle is used in the following 3: The JVM's environment variables

2:spring's Log4jconfiglistener

Use the following configuration XML code <context-param> <param-name>webAppRootKey</param-name> <param-value>web app.root</param-value> </context-param> <context-param> <param-name>log4jconfiglocation </param-name> <param-value>classpath:log4j.properties</param-value> </context-param> &L      T;listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener>



1, spring configuration has changed Webapprootkey can not be changed, the value is free to write.

2, log files stored in the Tomcat project directory/webapp (project name)/web-inf/logs/app.log file.

2: Use existing JVM variables:

For example:


The log will be located at: For example Windows:c:\documents and Settings\joe\logs\app.log

3 Set up your own directory, that is, when the project started through the System.setproperty settings, through the implementation of Servletcontextlistener to solve: such as Java code    public class  log4jlistener implements ServletContextListener {        public static final string log4jdirkey =  "Log4jdir";        public void contextdestroyed (servletcontextevent servletcontextevent)  {            system.getproperties (). Remove (Log4jdirkey);       }       public void contextinitialized ( servletcontextevent servletcontextevent)  {       string log4jdir  = servletcontextevent.getservletcontext (). Getrealpath ("/");       // System.out.println ("Log4jdir:" +log4jdir);       system.setproperty (Log4jdirkey, &NBSP;LOG4JDIR);  &nbSp     }  }  

Web.xml configuration: Java code <listener> <listener-class>com.log4j.log4jlistener</listener-class> </lis Tener>

Log4j.prtperties configuration:

Log4j.appender.a1.file=${log4jdir}/web-inf/logs/app1.log to solve.

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.