Web project log4j log configuration and use

Source: Internet
Author: User

Configuring log4j monitoring and file mapping and related settings in Web. xml

Web. XML code

<!--configuring log4j profile Paths--
    <context-param>
        <param-name>log4jconfiglocation</param-name >
        <param-value>classpath:log4j.properties</param-value>
    </context-param>
    < !--60s Detection Log profile changes-
    <context-param>
        <param-name>log4jRefreshInterval</param-name>
        <param-value>60000</param-value>
    </context-param>

    <!--configuring log4j listener
    -- <listener>
        <listener-class>org.springframework.web.util.log4jconfiglistener</ Listener-class>
    </listener>

Content configuration and parsing of log4j.properties

# Debug,info,warn,error,fatal Log level
log_level=info

log4j.rootlogger=${log_level},console,file
# LOG4J encoding format, time format settings
Log4j.appender.console=org.apache.log4j.consoleappender
Log4j.appender.console.encoding=utf-8
log4j.appender.console.layout=org.apache.log4j.patternlayout
# LOG4J.APPENDER.CONSOLE.LAYOUT.CONVERSIONPATTERN=[%-5P]%d{yyyy-mm-dd HH:mm:ss}%c{8}@ (%f:%l):%m%n 
LOG4J.APPENDER.CONSOLE.LAYOUT.CONVERSIONPATTERN=[%-5P]%d{yyyy-mm-dd HH:mm:ss}%c{1}@ (%f:%l):%m%n

Log4j.appender.file=org.apache.log4j.dailyrollingfileappender
log4j.appender.file.file=${catalina.base}/ Logs/survey.log
log4j.appender.file.encoding=utf-8
log4j.appender.file.datepattern= '. ' Yyyy-mm-dd
log4j.appender.file.layout=org.apache.log4j.patternlayout
#log4j. appender.file.layout= Org.apache.log4j.HTMLLayout
log4j.appender.file.layout.conversionpattern=[%-5p]%d{yyyy-mm-dd Hh\:mm\:ss}%c {8}@ (%f\:%l) \:%m%n 

Description

Log4j.appender.CONSOLE.layout.ConversionPattern

%m The message specified in the output code
  %p output priority, i.e. Debug,info,warn,error,fatal
  %r Output The number of milliseconds that the log information is consumed from the application boot to output
  %c output belongs to the class, which is usually the full name of the class
  %t Output The name of the thread that generated the log event
  %n Output a carriage return line break, Windows platform is "/r/n", UNIX platform is "/n"
  %d The date or time of the output log time, the default format is ISO8601, can also be specified after the format, such as:%d{yyy MMM dd HH:mm:ss, SSS}, output similar: October 18, 2002 22:10:28, 921
  %l where the output log event occurs, including the class name, the thread that occurred, and the number of rows in the code. Example: Testlog4.main (testlog4.java:10)

Log4j.appender.console.encoding=utf-8 is the encoded format of the log code

Log4j.appender.FILE.File = ${catalina.base}/.... is the location of the log log, ${catalina.base} is the parent directory of the Lib/bin directory that corresponds to the Tomcat instance typically ${catalina.home} tomcat, and catalina.base refers to The location of private information to each Tomcat directory is the parent directory of conf, logs, temp, webapps, and work.

How to use the log log:

In the related methods of using SQL in Java, in the related class of the Try method, use the following methods:

Logger log = Logger.getlogger (This.getclass ());

write in Catch

Log.error (e);

you can output the log to the corresponding log file



Note that the code specification: in the class should be detailed, the log error reason is to facilitate the query at a later time convenient query, to check the error code in the try, you can effectively capture.

Log.error (e.getmessage () + "Cause of error"), may not be written in this format, but be sure to be clear


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.