It has been a while since you used log4j for log output in spring, but sometimes you find that log files are already stored according to their ideals, but there are some baffling project log files that appear in Tomcat (because the project's log files are named after the project). So it's easier to distinguish between these logs. These are my tangled log files, let me improve the log4j configuration. Found that the configuration used before is really weak burst.
1. First of all, say that they are ideal for storing log path.
I prefer to put the log files under the Web-inf of the project, and then of course there is a folder called logs. Logs believe that many people will exist in such a directory, but in the Web-inf directory to believe that some people do not understand. In fact, it is for the protection of resources.
2. The old way
Write the servlet to reset the log file storage path in the log4j configuration file when the project is deployed.
The Web.xml configuration is as follows:
XML code <servlet> <servlet-name>log4j-init</ servlet-name> <servlet-class>com.foo.log.log4jinit</ servlet-class> <init-param> <param-name>log4j_properties_path</param-name> <param-value>WEB-INF/classes/log4j.properties</param-value> </init-param> <load-on-startup >1</load-on-startup> </servlet>
<servlet>
<servlet-name>log4j-init</servlet-name>
<servlet-class> com.foo.log.log4jinit</servlet-class>
<init-param>
<param-name>log4j_properties_path </param-name>
<param-value>WEB-INF/classes/log4j.properties</param-value>
</ init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Log4init code is not posted, the Internet is also more. The main role is to modify the original Log4j.appender file configuration changes to the current project deployment of the absolute path, the method is diverse, the same functionality.
The problem arises:
In this way, some extra log files are generated in the spring environment. Because spring initialization is earlier than the configured servlet started when the project is deployed, the original default log-store path takes effect (though nothing and no impact on the project).
3. There is no better way to configure it.
Using the log configuration method provided by spring
Web.xml Add the following code:
XML code <context-param> <param-name>webapprootkey </param-name> <param-value>project</param-value> </context-param> <context-param> < param-name>log4jconfiglocation</param-name> <param-value> web-inf/classes/log4j.properties</param-value> </context-param> <listener > <listener-class>org.springframework.web.util.log4jconfiglistener </listener-class> </listener>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>project </param-value>
</context-param>
<context-param>
<param-name> Log4jconfiglocation</param-name>
<param-value>web-inf/classes/log4j.properties</param-value >
</context-param>
<listener>
<listener-class> Org.springframework.web.util.log4jconfiglistener</listener-class>
</listener>
Webapprootkey: Configures the alias for the project, which is configured with the alias project. This can be ignored if you are deploying a project that is not in Tomcat, because Tomcat does not have a different Webapproot attribute configured for each application, so if two or more of the same application attribute names are present, the error occurs.
Then modify the configuration of the log4j.properties to change the path configuration of the log file output to:
Log4j.appender.a1.file=${project}web-inf/logs/project.log
A1 is the name of my appender. ${project} is an absolute path to obtain an application using an application alias in the Web.xml above.
Note: If you do not need to configure the application alias, the Webapprootkey is not configured. You can write this directly:
Log4j.appender.a1.file=${webapp.root}web-inf/logs/project.log
Webapp.root is the default property. If there is a configuration webapprootkey will be covered.
Outside the question:
1. Another way is to use environment variables, such as ${catalina.home}. But this will also produce redundant log, the same as using a servlet.
Of course, if you're not as obsessed with the extra log as I am. Life is also more relaxed ...
2. While in different operating systems, the path separator is different, Linux is "/", and Windows is "\", but the current writing in two environments with the same Tomcat test is no problem. While in Windows, the initialized log sees a somewhat awkward path. But it seems to remember that there are incompatibilities in some application servers ....
3. Recommended by default configuration file Log4j.appender.a1.file=poject.log, and before the project release, with Ant and other scripts to modify the path to log4j.appender.a1.file=${project}web-inf/ Logs/project.log The advantage of doing this is that when you test with JUnit, the generated log files are easily handled or ignored in the root path of the project.