SPRINGMVC Configuration log4j Log

Source: Internet
Author: User
Tags print format log4j

Configure log4j log, first import Slf4j-log4j12-1.7.7.jar
Configuring in Maven

   <dependency>
            <groupId>org.slf4j</groupId>
            <artifactid>slf4j-log4j12</ artifactid>
            <version>1.7.7</version>
        </dependency>

When you use the properties file configuration

### set log levels ###
Log4j.rootlogger = debug,d,e LOG4J.APPENDER.D

= Org.apache.log4j.DailyRollingFileAppender
log4j.appender.d.file = ${webapp.root}/logs/common-default.log
Log4j.appender.d.append = True
log4j.appender.d.threshold = INFO
log4j.appender.d.layout = Org.apache.log4j.PatternLayout
Log4j.appender.d.layout.conversionpattern =%-d{yyyy-mm-dd HH:mm:ss}  [%t:% R]-[%p]  %m%n

log4j.appender.e = Org.apache.log4j.DailyRollingFileAppender
log4j.appender.e.file =${ Webapp.root}/logs/error.log
Log4j.appender.e.append = true
log4j.appender.e.threshold = Error
Log4j.appender.e.layout = org.apache.log4j.PatternLayout
Log4j.appender.e.layout.conversionpattern =%-d{ YYYY-MM-DD HH:mm:ss}  [%t:%r]-[%p]  %m%n

The Web.xml file is now configured to

    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value> config/log4j.properties</param-value>
    </context-param>
    <!--3000 Show Open a watchdog thread scans the profile changes every 60 seconds; This makes it easy to change the location of the log store-->
    <context-param>
        <param-name> log4jrefreshinterval</param-name>
        <param-value>3000</param-value>
    </context-param >
    <listener>
        <listener-class>org.springframework.web.util.log4jconfiglistener</ Listener-class>
    </listener>

When you use the XML file configuration

<?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE log4j:configuration Public "-//apache//dtd log4j 1.2//en" "Log4j.dtd" > <log4j:configuration> <!-- [Console stdout]--> <appender name= "console" class= "Org.apache.log4j.ConsoleAppender" > <param name= "en Coding "value=" GBK "/> <param name=" target "value=" System.out "/> <layout" class= "Org.apache.lo" G4j.
    Patternlayout "> <param name=" Conversionpattern "value="%-5p%c{2}-%m%n "/> </layout> </appender> <!--[public Appender]--> <appender name= "Default-appender" class= "Org.apache.log4j.Da" Ilyrollingfileappender "> <param name=" File "value=" ${webapp.root}/logs/common-default.log "/>" ;p Aram Name= "Append" value= "true"/> <param name= "encoding" value= "GBK"/> <param name= "Datepa" Ttern "value=" '. ' Yyyy-mm-dd '. Log ' "/> <layout class=" Org.apache.log4j.PatternLAyout "> <param name=" Conversionpattern "value="%d%-5p%c{2}-%m%n "/> </layout> & lt;/appender> <!--[error log Appender]--> <appender name= "Error-appender" class= "Org.apache.log4j.DailyRo" Llingfileappender "> <param name=" File "value=" ${webapp.root}/logs/common-error.log "/> <param Name= "Append" value= "true"/> <param name= "encoding" value= "GBK"/> <param "name=" va lue= "Error"/> <param name= "Datepattern" value= "'. ' Yyyy-mm-dd '. Log ' "/> <layout class=" org.apache.log4j.PatternLayout "> <param name=" Conversi Onpattern "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= "${webapp.root}/logs/logistics-component.log"/>
        <param name= "Append" value= "true"/> <param name= "encoding" value= "GBK"/> <param Name= "Datepattern" value= "'. ' Yyyy-mm-dd '. Log ' "/> <layout class=" org.apache.log4j.PatternLayout "> <param name=" Conversi
    Onpattern "value="%d%-5p%c{2}-%m%n "/> </layout> </appender> <!--[Component log]--> <logger name= "Logistics-component" > <level value= "${logginglevel}"/> <appender-ref ref= "C Omponent-appender "/> <appender-ref ref=" Error-appender "/> </logger> <!--Root Logger --> <root> <level value= "${rootlevel}" ></level> <appender-ref ref= "Default-app   ENDER "/> <appender-ref ref= error-appender"/> </root> </log4j:configuration>

At this point Web.xml needs to configure

    <!--log4j configuration, file path because it is followed by the project launch-->
    <context-param>
        <param-name>log4jconfiglocation</ param-name>
        <param-value>config/log4j.xml</param-value>
    </context-param>
    < !--Configure Log4j.xml variables, use the method ${name}-->
    <context-param> <param-name>controller If you need to dynamically use the following methods
        </param-name>
        <param-value>controller-log</param-value>
    </context-param>

    <context-param>
        <param-name>loggingLevel</param-name>
        <param-value>info</ param-value>
    </context-param>
    <!--load log4j configuration file-->
    <listener>
        < Listener-class>org.springframework.web.util.log4jconfiglistener</listener-class>
    </listener >

2 methods individuals prefer to use properties files to facilitate the modification of parameters

used in controller

Create a Logger object first

private static final Logger Logger = Loggerfactory.getlogger (Logincontroller.class);

Then you can use it in the method.

        Logger.info ("index----------------");
        Logger.error ("index----------------");
        Logger.warn ("index----------------");

configuration file Explanation

1. Configure the root logger, whose syntax is:

Log4j.rootlogger = [level], Appendername, Appendername, ...

Where level is the priority of logging, divided into off, FATAL, ERROR, WARN, INFO, DEBUG, all, or levels you define. LOG4J recommends using only four levels, from high to low, respectively, for error, WARN, INFO, and DEBUG. By the level defined here, you can control the switch to log information at the appropriate level in your application. For example, if the info level is defined here, all debug-level log information in the application will not be printed. Appendername refers to where B log information is exported. You can specify multiple output destinations at the same time.

2. Configuration log information output destination Appender, its syntax is:

Log4j.appender.appenderName = Fully.qualified.name.of.appender.class  
Log4j.appender.appenderName.option1 = Value1 ...  
Log4j.appender.appenderName.option = Valuen

Among them, LOG4J provides the following appender:

Org.apache.log4j.ConsoleAppender (console),  
org.apache.log4j.FileAppender (file),  
Org.apache.log4j.DailyRollingFileAppender (a log file is generated every day),  
Org.apache.log4j.RollingFileAppender (a new file is generated when the file size reaches the specified size),  
Org.apache.log4j.WriterAppender (send log information to any specified place in streaming format)

3. Configure the format (layout) of the log information, and its syntax is:

Log4j.appender.appenderName.layout = Fully.qualified.name.of.layout.class  
Log4j.appender.appenderName.layout.option1 = value1  
...  
Log4j.appender.appenderName.layout.option = Valuen

Of these, the log4j provides layout with several e:

Org.apache.log4j.HTMLLayout (layout in HTML table),  
org.apache.log4j.PatternLayout (you can specify layout patterns flexibly),  
Org.apache.log4j.SimpleLayout (The level and information string that contains the log information),  
org.apache.log4j.TTCCLayout (contains information about the time, thread, category, and so on that the log was generated)

LOG4J uses a print format similar to the printf function in c to format the log information, printing parameters as follows:%m the message specified in the output code

%p output priority, that is, the debug,info,warn,error,fatal  
%r output from the application boot to the output of the log information the number of milliseconds the output  
belongs to the class, usually the full name of the class  
%t output the thread name that generated the log event  
%n output A carriage return line break, Windows platform is "RN", UNIX platform is "n"  
%d output log point-in-time date or time, the default format is ISO8601, you can also specify the format after, such as:%d{yyy MMM DD HH:mm:ss, SSS}, output similar to: October 18, 2002 22:10:28,921  
%l output Log event location, including the class name, the thread that occurred, and the number of lines in the code. Example: Testlog4.main (testlog4.java:10)

Reference article: http://www.codeceo.com/article/log4j-usage.html
Reference article: http://www.yiibai.com/log4j/log4j_logging_database.html

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.