Control the log output directory of log4j under Tomcat

Source: Internet
Author: User
Tags tomcat server log4j
Log output directory under Tomcat control log4j 2008-06-16 20:14
When you configure the output directory of the log4j log file, you always write out the absolute path of the log. I want to write down the log to the root directory of my project. If you write an absolute path in a configuration item, it is cumbersome to change the value of the absolute path when my project is ported. How can you not change the value of the path that.
This problem has been bothering me for a long time and it's finally solved today.
Represents an absolute path even if the value of a system variable is used. The "Catalina.home" value of a system variable under the Tomcat server represents the root directory of Tomcat. Because the path below the ComCat root is relatively fixed, you can add the path to the project after that value. For example, if you want to write the log to the MyApp directory, just do it in the configuration entry:
<param name= "File" value= "${catalina.home}/webapps/myapp/error.log"/>

This can solve the problem of the log output path under the Tomcat server, but if you change the server, there is a problem with this configuration.
The solution to this problem is to customize a system variable yourself and then use it in the configuration item.
First in customizing a system variable "myapphome" (do not conflict with other system variables) This variable represents the root directory of this project
Log4j in the configuration entry:
<param name= "File" value= "${myapphome}/error.log"/>
Customizing system variables in a program before reading a configuration item
Appfilepath = This.getservletcontext (). Getrealpath ("/"); Get the root path of the project
Appfilepath=appfilepath.replace ('//', '/');
System.setproperty ("Myapphome", Appfilepath);
Domconfigurator.configure (absolute path of configuration file);

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.