General steps for using log4j

Source: Internet
Author: User

It is hard to remember, so some of the Notes should be recorded.

1. Create a log4j. properties configuration file and place it in the same path of the main portal class.

Og4j. rootlogger = debug, stdout, R

Log4j. appender. stdout = org. Apache. log4j. leleappender
Log4j. appender. stdout. layout = org. Apache. log4j. patternlayout

# Pattern to output the caller's file name and line number.
Log4j. appender. stdout. layout. conversionpattern = % d % 5 p [% T] (% F: % L)-% m % N

Log4j. appender. r = org. Apache. log4j. rollingfileappender
Log4j. appender. R. File = yesftp. Log

Log4j. appender. R. maxfilesize = 1000kb
# Keep one backup file
Log4j. appender. R. maxbackupindex = 1

Log4j. appender. R. layout = org. Apache. log4j. patternlayout
Log4j. appender. R. layout. conversionpattern = % d % 5 p [% T] (% F: % L)-% m % N

 

2. initialize log4j in the static area of the entry class

Static {
Propertyconfigurator. Configure (
Yesftpclientframe. Class. getresource ("log4j. properties "));
}

 

3. initialize a private static variable in every location where logs are needed.

Private Static logger = logger. getlogger (XX. Class );

 

4. Use the methods provided by log4j

Logger.info ("hello ");

5. Use log4j to output abnormal stack information

Logger. Error ("Oops, got an exception:", e );

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.