How to use relative paths in the log4j. properties file)

Source: Internet
Author: User

Preface:
Log4j is a widely used Java log component. We can easily use it to output log information to the console, files, databases, and other storage media, or even output it to any storage media in the form of stream extension.

Problems to be Solved in this article:
How to use relative paths in the log4j. properties file?By default, we can only configure the absolute path in log4j. properties. In this way, the system deployment and the migration of the open environment will cause great inconvenience. We need to change the path. Therefore, we can use the following method to implement the relative path in log4j. properties:

1. Implement a Servlet

/**

* Log4j Initialization

 

* @ Author xiongchun

* @ Since 2011-04-26

*/

Public class log4jinitservlet extends httpservlet {

/**

* Servlet Initialization

*/

Public void Init (servletconfig config) throws servletexception {

 

String root = config. getservletcontext (). getrealpath ("/");

String log4jlocation = config. getinitparameter ("log4jlocation ");

System. setproperty ("webroot", root );

If (g4utils. isnotempty (log4jlocation )){

Propertyconfigurator. Configure (root + log4jlocation );

}

 

}

}

2. Load the servlet in Web. xml

<! -- Configure log4j -->

<Servlet>

<Servlet-Name> log4jinit </servlet-Name>

<Servlet-class> org. eredlab. g4.rif. util. log4jinitservlet </servlet-class>

<Init-param>

<Param-Name> log4jlocation </param-Name>

<Param-value> WEB-INF/classes/log4j. properties </param-value>

</Init-param>

<Load-on-startup> 0 </load-on-startup>

</Servlet>

3. The relative path can be used in log4j. properties as follows:

 

# Rlogfile

Log4j. appender. rlogfile = org. Apache. log4j. rollingfileappender

Log4j. appender. rlogfile. layout = org. Apache. log4j. patternlayout

Log4j. appender. rlogfile. layout. conversionpattern = % d % P [% C]-<% m> % N

Log4j. appender. rlogfile. File =$ {Webroot}/Logs/eredg4.log

Log4j. appender. rlogfile. maxfilesize = 2048kb

Log4j. appender. rlogfile. maxbackupindex = 10

Annotation:After writing it, I found that this method uses absolute paths, but it uses dynamic methods to obtain absolute paths to achieve the effect similar to relative paths.

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.