Configure log4j in spring Projects

Source: Internet
Author: User

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.

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.