Steps for using Log4j in Java Web

Source: Internet
Author: User

Use the Log4J guide in Java Web. Every time you start writing a project, you forget how to configure Log4J. Therefore, write a step as a record.

Step 1 download the Log4J jar package

Download the latest Log4J jar package from Apache Logging Services site. If maven2 is used, you can add the following dependency directly in pom. xml, and maven will automatically download it.

       log4j      log4j      1.2.15  

 

Step 2 import the jar package to the Web Project

If you use Maven2, you can ignore this step. Because this dependency is declared in pom. xml, Maven will automatically import the jar during build.

A normal project needs to copy the downloaded jar package to the project's WEB-INF/lib.

Step 3 import the Log4J xml configuration file or the properties configuration file

For Maven projects, directly place the configuration file under the <project>/Java Resources/src. main/resources directory.

For non-Maven projects, You must place the configuration file under classpath.

Properties instance: log4j. properties

log4j.rootLogger=INFO,stdout,===%d %p ====5==%d %p  - %m%n

 

Xml instance: log4j. xml

                                                                                                                                                                                   
Step 4 Write Log Code

If everything above is configured, you can write the Log code.

1. Import Package first

  org.apache.log4j.Logger;

2. Get Logger class members

  Logger log = Logger.getLogger(MyClassName.);

3. Log

 log.trace("Hello World!"  log.debug("How are you today?"  log.info("I am fine."  log.error("I am programming."  log.warn("I love programming."  log.fatal("I am now dead. I should have been a movie star.");

Step 5 Run

Start the Web app to view and run the relevant code to output logs.

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.