Log4j Advanced Tips-Location of dynamic configuration log output files

Source: Internet
Author: User
Tags log4j
Og4j Advanced Tips-Location of dynamic configuration log output files

The usual use of log4j is to write the output file directly to death in its configuration file (XML or properties), which in many cases is very simple and effective. The disadvantage of this is that if you want this Web application to run, you have to modify the log4j configuration file. For example, the configuration of log4j in struts2.x, everyone has to follow their own path to configure. This undoubtedly increases the complexity of the configuration. Therefore, in some cases, we would prefer that the log file be exported by default to a directory in the Web application, such as how it would be implemented if you wanted to output logs to Webroot/logs/log.log.

In the configuration file (which I used in the properties file, XML I did not test, in theory) uses ${user.dir} in a similar way, where the variable must be a variable in the system.getpropertities. And that's what we're going to take advantage of. It is easy to get an application's publishing directory first in a listener or high-priority, boot servlet, such as String home = Servletcontext.getrealpath ("/"); Then call system.setpropertity ("Web_home", Home), "Web_home" name can be arbitrary. The configuration in Log4j.properties is: Log4j.appender.trace.file=${web_home}/logs/log.log. The log is exported to the logs directory in the Web application publishing directory.



==============================

From:http://www.blogjava.net/hello-yun/archive/2012/07/29/384259.html

Usually in the project we will put the log4j configuration in the Classpath,
The log4j output path is also written directly in Log4j.xml or log4j.properties,
This is the original so do not bother anything, but in our company to configure everything to separate.
So log4j's output directory is no longer specified by the developer,
So how to separate it.
Some are directly to the Log4j.xml or properties of the file separation, the project started loading in,
So the entire log4j configuration file is not controlled by the developer,
However, usually the configuration of the log4j by the operation of the personnel configuration of the Dongdong is also an output directory,
The other configuration of the log4j is controlled by the developer,
At this point, you can use ${} to specify
<param name= "File" value= "${log4j.home}/test.log"/>
Log4j.home is specified by the container when it is started, plus-dlog4j.home=d:/log in the JVM
So the ${log4j.home in log4j.xml the actual output directory,
You can also put this log4j.home in a separate properties that can be
After the container is started, the listener resolves properties and gets the Log4j.home variable
Set the value to System.env
System.setproperties ("Log4j.home");
This way, log4j can find the output directory as well



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.