[Log4j] How can I make my Java Web project use log4j?

Source: Internet
Author: User

Create a project:

Preparations for using log4j in log4j,

The first jar package, need to log4j-1.2.11.jar package, import to your own project;

2. Create the log4j. properties file and fill in the corresponding configuration. I have filled in two configurations: Console output and file output.

The configuration is as follows:

### Output log information to the console

### Log4j. appender. stdout = org. Apache. log4j. leleappender

### Print the information to system. Out

### Log4j. appender. stdout. Target = system. outlog4j. appender. stdout. layout = org. Apache. log4j. patternlayout

### Specify the output format: Display date and log information

### Log4j. appender. stdout. layout. conversionpattern = % d {yyyy-mm-dd hh: mm: SS} % m % N

### Output log information to the file F: \ log \ test. Log

### Log4j. appender. File = org. Apache. log4j. fileappender

### Specify the log output file name

### Log4j. appender. file. File = F: \ log \ test. loglog4j. appender. file. layout = org. Apache. log4j. patternlayout

### Specify the output format: Display date, log location, and log information

### Log4j. appender. file. layout. conversionpattern = % d {yyyy-mm-dd hh: mm: SS} % L % m % N

### Set priorities and output sources ###

### Set priority to debug

### Log4j. rootlogger = debug, stdout, file

Configuration complete! Write a test program !!!

Log4j. Java

Import org. Apache. log4j. Logger;

Public class log4j {

Public static void main (string [] ARGs ){

// Obtain the logger, which controls the log information

Logger logger = logger. getlogger (log4j. Class. getname ());

Try {

// Use the debug and info methods of the logger object to output log information

Logger. debug ("sets the instructor ID. ");

} Catch (illegalargumentexception ex ){

// Use the debug and info methods of the logger object to output log information

Logger.info (ex. getmessage ());

}

}

}

Test result: This is the output result of the console:

21:35:41 set the instructor number.

The file output is not easy to display. I will not display it here. Please try it on your own !!

And this is the related knowledge http://logging.apache.org/log4j/1.2/download.html of log4j.

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.