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); |