Java learning notes-configuration and use of log4j in Spring mvc

Source: Internet
Author: User

If you use the spring plug-in to create a spring template project, it will contain log4j by default. You only need to change the log4j configuration to use it. If you create your own project, you need to load the log4f package, the procedure is as follows. pom. add xml to the log4j dependency package [html] <! -- Logging --> <dependency> <groupId> org. slf4j </groupId> <artifactId> slf4j-api </artifactId> <version> 1.6.6 </version> </dependency> <groupId> org. slf4j </groupId> <artifactId> jcl-over-slf4j </artifactId> <version> 1.6.6 </version> <scope> runtime </scope> </dependency> <groupId> org. slf4j </groupId> <artifactId> slf4j-log4j12 </artifactId> <version> 1.6.6 </version> <scope> runtime </scop E> </dependency> 2. Create log4j. xml [html] 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 xmlns: log4j = "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] %-5 p: % c-% m % n "/> </layout> </appender> <appender name =" file "class =" org. apache. log4j. dailyRollingFileAppender "> <param name =" File "value =" $ {log. dir}/com. xxx. account. log "/> <par Am name = "Append" value = "true"/> <param name = "DatePattern" value = ". yyyy-MM-dd "/> <layout class =" org. apache. log4j. patternLayout "> <param name =" ConversionPattern "value =" % d [% t] %-5 p % c-% m % n "/> </layout> </appender> <appender 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. a Pache. log4j. patternLayout "> <param name =" ConversionPattern "value =" % d | [% t] | %-5 p | % c |-% m % n "/> </layout> </appender> <! -- Application Loggers --> <logger name = "com. xxx. account "> <level value =" info "/> </logger> <logger name =" com. xxx. account-error "> <level value =" error "/> <appender-ref =" $ {log. appender} "/> <appender-ref =" account-error "/> </logger> <! -- Root Logger --> <root> <level value = "info"> </level> <appender-ref = "$ {log. appender} "/> </root> </log4j: configuration> log4j. xml defines three appender, that is, the location of log output, one is the console, two are files, but one file account-error.log is dedicated to collecting files with special errors, you can define multiple use cases for different scenarios. The related definitions of log4j are as follows: [html] (1 ). the output method appender generally has five types: org. apache. log4j. rollingFileAppender (rolling file, automatically record the latest log) org. apache. log4j. consoleAppender (console) org. apache. log4j. fileAppender (File) org. Apache. log4j. dailyRollingFileAppender (a log file is generated every day) org. apache. log4j. writerAppender (send log information to any specified place in stream Format) (2 ). the priority of the log, which is divided from high to low into OFF, FATAL, ERROR, WARN, INFO, DEBUG, and ALL. We recommend that you use only FATAL, ERROR, WARN, INFO, and DEBUG levels for Log4j. (3 ). format description: parameters in layout start with %. The following parameters represent different formatting information (the parameters are listed in alphabetical order): % c outputs the full name of the class to which the parameter belongs, the dimension output by the Num Class Name (for example, "org. apache. elattings. className ", % C {2} Will output elattings. className) % d the log output time is in the format of % d {yyyy-MM-dd HH: mm: ss, SSS}. You can specify the format, for example, % d {HH: mm: ss} % l location of log event output, including category name and thread, number of lines in the code % n linefeed % m output code specified information, such as info ("message "), output message % p output priority, namely FATAL, ERROR, etc. % r output the number of milliseconds from startup to display the log information % t output the thread name that generates the log event 3. use log4j [java] private static final Logger logger = LoggerFactory In the controller. getLogger ("com. xxx. account-error "); logger. error ("test log"); this record a log to the account-error.log, but also write a log to the public log File com. xxx. account. log, if you only want to write to the public log file, use the class name during log initialization. [java] 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.