1. Do not place log4j. properties under the root directory of classpath
When you use JUnit for local testing, the following message is displayed:
Log4j: warn no appenders cocould be found for logger (Org. springframework. Beans. Factory. Access. singletonbeanfactorylocator ).
Log4j: Warn please initialize the log4j system properly.
Then we cannot see the configuration information of spring bean loading.
Add log4j. properties to the classpath path to display bean loading information.
2. Configure log4j for spring in Web Applications
Spring uses a servlet listener to write the absolute root path to the system variable when the web iner is started, so that $ {myappfuse can be used in the log4j configuration file. root} to indicate the system variable you just set: log4j. appender. logfile. file =$ {myappfuse. root}/logs/mylog. log
The configuration statement in Web. XML is as follows:
<! -- If not set, the default value is Web. root, but it is recommended to avoid project conflicts -->
<Context-param>
<Param-Name> webapprootkey </param-Name>
<Param-value> myappfuse. Root </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>
A listener is configured here, and servlet can also be configured:
<Servlet>
<Servlet-Name> log4j </servlet-Name>
<Servlet-class>
Org. springframework. Web. util. log4jconfigservlet
</Servlet-class>
<Load-on-startup> 1 </load-on-startup>
</Servlet>
After the configuration, we can find the log file mylog. log in the logs directory of the current application.
The value of the attribute log4jconfiglocation is recommended to be set to:/WEB-INF/classes/log4j. properties, so that we do some tests without starting the web application, which can properly record the log information.