Java Learning Notes Configuration and usage of log4j in 9-spring MVC

Source: Internet
Author: User
Tags log4j

If you use the spring plug-in to create a spring template project, it defaults to log4j, as long as you change the log4j configuration to use, if you create a project, you need to load the log4f package, use the following steps

1.pom.xml join Log4j's dependency pack

<!--Logging-->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactid >slf4j-api</artifactId>
			<version>1.6.6</version>
		</dependency>
		< dependency>
			<groupId>org.slf4j</groupId>
			<artifactid>jcl-over-slf4j</ artifactid>
			<version>1.6.6</version>
			<scope>runtime</scope>
		</ dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId> slf4j-log4j12</artifactid>
			<version>1.6.6</version>
			<scope>runtime</scope >
		</dependency>
2. Create Log4j.xml under Src/main/resources

<?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE log4j:configuration Public "-//apache//dtd log4j 1.2//en" "Log4j.dtd" > <log4j:configuration "http://jakarta.apache.org/log4j/" > <!--appenders--> <appender name= "Console" class= " Org.apache.log4j.ConsoleAppender "> <param name=" Target "value=" System.out "/> <layout" class= " Org.apache.log4j.PatternLayout "> <param name=" Conversionpattern "value="%d [%t]%-5p:%c-%m%n "/> </lay out> </appender> <appender name= "file" class= "Org.apache.log4j.DailyRollingFileAppender" > <par  Am Name= "File" value= "${log.dir}/com.xxx.account.log"/> <param name= "Append" value= "true"/> Name= "Datepattern" value= "Yyyy-mm-dd"/> <layout class= "Org.apache.log4j.PatternLayout" > <pa Ram Name= "Conversionpattern" value= "%d [%t]%-5p%c-%m%n"/> </layout> </appender> <app Ender Name="Account-error" class= "Org.apache.log4j.FileAppender" > <param name= "File" value= "${log.dir}/ Account-error.log "/> <param name=" Append "value=" true "/> <layout class=" Org.apache.log4j.PatternLa Yout "> <param name=" Conversionpattern "value="%d| [%t]|%-5p|%c| -%m%n "/> </layout> </appender> <!--application loggers--> <logger name=" Com.xxx.ac Count "> <level value=" info "/> </logger> <logger name=" Com.xxx.account-error "> <level valu e= "Error"/> <appender-ref ref= "${log.appender}"/> <appender-ref ref= "Account-error"/> &LT;/LOGGER&G
	

	T <!--Root Logger--> <root> <level value= "info" ></level> <appender-ref ref= "${log.appender
 } "/> </root> </log4j:configuration>
Log4j.xml defines 3 Appender, which is the location of the log output, one is the console, two are files, but there is a file Account-error.log is dedicated to collecting special error files, you can define a number of different scenarios for use, The relevant definitions of log4j are as follows:
(1). Output mode Appender generally have 5 kinds of:               org.apache.log4j.rollingfileappender (
Scrolling files, automatic recording of the latest log)                org.apache.log4j.consoleappender (console)                org.apache.log4j.fileappender (file)           &NBSP ;    org.apache.log4j.dailyrollingfileappender (Generate a log file daily)               & Nbsp;org.apache.log4j.writerappender (send log information to any specified place in streaming format)   (2). Priority for journaling is priority, priority is divided into              , FATAL, ERROR, WARN, INFO, DEBUG, all.               LOG4J recommends using only the five levels of fatal, ERROR, WARN, INFO, and Debug.   (3). The parameters in the format description layout start with a%, followed by different parameters representing different formatting information (parameters are listed in alphabetical order):                 %c & nbsp       Output the full name of the class, can be modified to%d{num}, the Num class name output dimension (such as: "Org.apache.elathen.ClassName",%c{2} will output elathen. ClassnamE                  %d       output log time its format is%D{YYYY-MM-DD HH:MM:SS , SSS}, can specify format such as%d{hh:mm:ss}                 %l         Output Day 
Log event occurrence location, including class name, occurrence thread, number of lines in code                  %n       line break  
               %m       Output code specify information, such as info ("message"), Output message                  %p       Output priority-FATAL, ERROR etc                  %r         output from boot to display this log information the number of milliseconds        
          %t         OUTPUT thread name for this log event  

3. Use of log4j in controller

private static final Logger Logger = Loggerfactory.getlogger ("Com.xxx.account-error");

Logger.error ("Test log");

This records a log to the Account-error.log, but also wrote a log to the common log file Com.xxx.account.log, if only want to write to the public log file, the log initialization when the name of the class can be

private static final Logger Logger = Loggerfactory.getlogger ("Xxx.class");
XXX is the name of the current class

Related Article

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.