Based on the front, this article summarizes how to configure log4j in spring, and gives a link to the complete project at the end of this article.
The first is the code to be added in Web. xml:
<!--6. Configure LOG4J-<!--6.1 Configuration load Log4j.xml file path--<context-param> <param-name>log4jconfiglocation </param-name> <param-value>/WEB-INF/classes/spring/log4jConfig.xml</param-value> </ Context-param> <!--6.2 Configuring Log4j.xml variables--<context-param> <param-name>rootlevel</ param-name> <param-value>DEBUG</param-value> </context-param> <context-param> <param-name>loggingLevel</param-name>
Then add Log4jconfig.xml under the Src/config/spring folder
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE log4j:configuration Public "-//apache//dtd log4j 1.2//en" "Web-inf/classes/spring/log4j.dtd" ><log4j: Configuration xmlns:log4j= "http://jakarta.apache.org/log4j/" > <!--this profile defines a total of 4 appender, which is the four output location of the log log, About Appender See note 4--> <!--[console]-<appender name= "console" class= "Org.apache.log4j.ConsoleAppender" > & lt;! --Consoleappender means the log output is console-to-<param name= "encoding" value= "UTF-8"/> <param name= "target" V Alue= "System.out"/> <!--the output location of the log logs is the system output, typically the console-to-<!--about the parameters in layout see note 5--and <layout class = "Org.apache.log4j.PatternLayout" > <param name= "Conversionpattern" value= "%-5p%c{2}-%m%n"/> </l Ayout> </appender> <!--[public Appender]--<appender name= "Default-appender" class= " Org.apache.log4j.DailyRollingFileAppender "> <!--dailyrollingfileappender is output to generate one log file per day--<param NA Me= "File" ValUe= ". /logs/common-default.log "/> <!--log file output path--<param name=" Append "value=" true "/> <!--settings are restarted Service, add a new log on the base of the original log--<param name= "encoding" value= "UTF-8"/> <param name= "Datepattern" value= "'. ' Yyyy-mm-dd '. Log ' "/> <!--the names of daily logs, such as: Common-default.2015-02-06.log---<layout class=" ORG.APACHE.LOG4J.P Atternlayout "> <param name=" Conversionpattern "value="%d%-5p%c{2}-%m%n "/> </layout> </appen der> <!--[error log dedicated to files that collect special errors]--<appender name= "Error-appender" class= "Org.apache.log4j.DailyRollingFil Eappender "> <param name=" File "value=". /logs/common-error.log "/> <param name=" Append "value=" true "/> <param name=" encoding "value=" UTF-8 "/ > <param name= "Threshold" value= "error"/> <!--log output level is error, see note 6--and <param name= "Datepattern "Value=" '. ' Yyyy-mm-dd '. Log ' "/> <layout class=" Org.apache.log4j.PatternLayout "> <param name= "Conversionpattern" value= "%d%-5p%c{2}-%m%n"/> </layout> </appender> < !--[Component Log Appender]-<appender name= "Component-appender" class= "Org.apache.log4j.DailyRollingFileAppender" > <param name= "File" value= ". /logs/logistics-component.log "/> <param name=" Append "value=" true "/> <param name=" encoding "value=" u Tf-8 "/> <param name=" Datepattern "value=" '. ' Yyyy-mm-dd '. Log ' "/> <layout class=" org.apache.log4j.PatternLayout "> <param name=" Conversionpattern "Val Ue= "%d%-5p%c{2}-%m%n"/> </layout> </appender> <!--used to define logger lever and the appender used, where append Er-ref must be the name of the appenders that was previously defined. Log will then output the log in the output format defined by Appender. --<logger name= "logistics-component" > <level value= "${logginglevel}"/> <appender-ref ref= "Component-appender"/> <appender-ref ref= "Error-appender"/> </logger> <!--RootLogger--<!--1.root is the function of collecting the information flow from all the feedback below and appender the output based on the configuration in root, as long as you configure additivity= "FA in Looger LSE ", will not be fed back into root. The 2.root tag is the default output form of log, and if the log of a class does not explicitly specify its output lever and format in loggers, it will take the format defined in root-<root> <level value= "${ro Otlevel} "></level> <appender-ref ref=" Default-appender "/> <appender-ref ref=" Error-appender "/> </root></log4j:configuration>
Next, copy the LOG4J.DTD under the Log4jjar package Log4j-1.2.17\org\apache\log4j\xml to src/config/spring.
Of course, people need to import the appropriate package.
Complete project See: http://download.csdn.net/detail/u012116457/8431651
Play Spring MVC (v)----Integrate log4j in spring