Use and configuration for adding log4j__log4j to Java Web projects

Source: Internet
Author: User
Tags java web log4j

One: The introduction of the log4j-dependent jar package, if not MAVEN project, directly to download the jar package, put it in the project's Lib directory.

<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>

Second: In the engineering of the SRC directory to add File Log4j.properties, and add content


# # #设置 ###
Log4j.rootlogger = Debug, console, I, D, E, F

### output information to control lift ###
Log4j.appender.console = Org.apache.log4j.DailyRollingFileAppender
Log4j.appender.console.Target = System.out
Log4j.appender.console.layout = Org.apache.log4j.PatternLayout
Log4j.appender.console.layout.ConversionPattern = [%-5p]%d{yyyy-mm-dd hh\:mm\:ss,sss} console\:%m-%l%n

### output debug level above log to =e://logs/error.log ###
LOG4J.APPENDER.D = Org.apache.log4j.DailyRollingFileAppender
Log4j.appender.d.file = E\://logs/debug.log
Log4j.appender.d.append = True
Log4j.appender.d.threshold = DEBUG
Log4j.appender.d.layout = Org.apache.log4j.PatternLayout
Log4j.appender.d.layout.conversionpattern =%-d{yyyy-mm-dd Hh\:mm\:ss} [%t\:%r]-[%p]%m-%l%n

### output the log above the error level to =e://logs/error.log ###
LOG4J.APPENDER.E = Org.apache.log4j.DailyRollingFileAppender
Log4j.appender.e.file =e\://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-%l%n


### output info level above log to =e://logs/info.log ###
LOG4J.APPENDER.I = Org.apache.log4j.DailyRollingFileAppender
Log4j.appender.i.file =e\://logs/info.log
Log4j.appender.i.append = True
Log4j.appender.i.threshold = INFO
Log4j.appender.i.layout = Org.apache.log4j.PatternLayout
Log4j.appender.i.layout.conversionpattern =%-d{yyyy-mm-dd Hh\:mm\:ss} [%t\:%r]-[%p]%m-%l%n


### output fatal level above log to =e://logs/fatal.log ###
LOG4J.APPENDER.F = Org.apache.log4j.DailyRollingFileAppender
Log4j.appender.f.file =e\://logs/fatal.log
Log4j.appender.f.append = True
Log4j.appender.f.threshold = FATAL
Log4j.appender.f.layout = Org.apache.log4j.PatternLayout
Log4j.appender.f.layout.conversionpattern =%-d{yyyy-mm-dd Hh\:mm\:ss} [%t\:%r]-[%p]%m-%l%n


This defines five levels of logging, including the format for output to the console, and each type of log has a build policy, which is the configuration above


Three: Web.xml to join LOG4J configuration

<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>


Four: Calling methods, generating objects inside a class

Private Logger Logger = Logger.getlogger (This.getclass (). GetName ());

Logger.error ("message");

Logger.info ("message");


This allows you to view the generated log on the E disk. This default configuration method general items are sufficient. Basically do not need any change, more strategy information, can Baidu understand. In fact, this configuration defines these types of logs, how they are generated, and what formats are generated.


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.