Spring project configuration log4j logging feature

Source: Internet
Author: User
Tags log4j

One, add log4j dependency Package

The dependency package Log4j-x.x.xx.jar can be downloaded from the official website, and after the download build path, add dependency packages such as using MAVEN, you can add the following dependencies

    <!--https://mvnrepository.com/artifact/log4j/log4j -    <Dependency>      <groupId>Log4j</groupId>      <Artifactid>Log4j</Artifactid>      <version>1.2.17</version>    </Dependency>

Two, create the log4j.properties configuration file

The log4j.properties configuration file is used to configure the output format of the log, the address of the output ....

# # # set log Levels # # #log4j. Rootlogger= INFO, D,ELOG4J.APPENDER.D=Org.apache.log4j.rollingfileappenderlog4j.appender.d.file=${scheduleproject}web-inf/logs/schedule.loglog4j.appender.d.append=Truelog4j.appender.d.threshold=debuglog4j.appender.d.maxfilesize=50000kblog4j.appender.d.layout=Org.apache.log4j.patternlayoutlog4j.appender.d.layout.conversionpattern=%-d{yyyy-mm-dd HH:mm:ss}[%t:%r]-[%p]%M%NLOG4J.APPENDER.E=Org.apache.log4j.rollingfileappenderlog4j.appender.e.file=${scheduleproject}web-inf/logs/schedule.loglog4j.appender.e.append=Truelog4j.appender.e.threshold=errorlog4j.appender.e.maxfilesize=50000kblog4j.appender.e.layout=Org.apache.log4j.patternlayoutlog4j.appender.e.layout.conversionpattern=%-d{yyyy-mm-dd Hh\:mm\:ss}[%l\:%c\:%t\:%r]-[%p]%m%n

Third, configure the Load Log4j.properties property in the Web. xml file

<!--load the log4j configuration file log4j.properties -    <Context-param>        <Param-name>Log4jconfiglocation</Param-name>        <Param-value>/web-inf/log4j.properties</Param-value>    </Context-param>    <!--Set the time interval to refresh the log profile, which is set to 10s -    <Context-param>        <Param-name>Log4jrefreshinterval</Param-name>        <Param-value>10000</Param-value>    </Context-param>    <!--load the log4j listener in the spring framework Log4jconfiglistener -    <Listener>        <Listener-class>Org.springframework.web.util.Log4jConfigListener</Listener-class>    </Listener>    <!--to avoid inter-project conflicts, define a unique Webapprootkey -    <Context-param>        <Param-name>Webapprootkey</Param-name>        <Param-value>Scheduleproject</Param-value>    </Context-param>

Four, use log4j in code to log records

 Public class Test {    privatestatic Logger Logger = Logger.getlogger (test.class);      Public Static void Main (String[]args)    {        Logger.info ("23423");        Logger.warn ("SDFSDF");}    }

Five, references

LOG4J Log Configuration Example

log4j log file configuration relative path in Spring MVC

Spring project configuration log4j logging feature

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.