SPRINGMVC Project Complete Example 05th log--log4j

Source: Internet
Author: User

Log4j

is log for Java, foreigners like this, such as the i18n---internationalization is not between I and N have 18 letters ...

http://logging.apache.org/log4j/2.x/

Straight into the chase.

He is a powerful Japanese functional component

Want to use him, the elephant in the refrigerator as much as the root

1. Download a jar package and put it in the project

2, make a configuration file (can be used before the direct copy in order to modify it)

Say simple words, very simple, but said configuration, in fact, is quite a lot, after all, powerful

Now my configuration file

### set log levels ###

log4j.rootLogger = DEBUG,stdout

 

 

### 输出到控制台 ###

log4j.appender.stdout = org.apache.log4j.ConsoleAppender

log4j.appender.stdout.Target = System.out

log4j.appender.stdout.layout = org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n

 

 

### sql ###

log4j.logger.java.sql.PreparedStatement=DEBUG

log4j.logger.java.sql.ResultSet=DEBUG

See the use of a test case

Our previous Testuserservice.java.

I did three things.

1,import

2, defining variables

3, using

Run this use case

Let's take a look at the output

Note that:

Logger.debug (Object message);  Logger.info (Object message);  Logger.warn (Object message);  Logger.error (Object message);
He has several methods, huh?

In fact, you can put me this paragraph, directly copied paste in the past, if necessary, such as modifying the file log to save the path, a little modification can be, the following specific said configuration file may not look ....

src同级创建并设置log4j.properties

# # # # # # #下面的才是正文, this sentence and the above please do not copy #####

 

### 设置###

log4j.rootLogger = debug,stdout,D,E

 

### 输出信息到控制抬 ###

log4j.appender.stdout = org.apache.log4j.ConsoleAppender

log4j.appender.stdout.Target = System.out

log4j.appender.stdout.layout = org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n

 

### 输出DEBUG 级别以上的日志到=E://logs/error.log ###

log4j.appender.D = org.apache.log4j.DailyRollingFileAppender

log4j.appender.D.File = E://logs/log.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%n

 

### 输出ERROR 级别以上的日志到=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%n

For example, if we add this paragraph

It's the way it works.

SPRINGMVC Project Complete Example 05th log--log4j

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.