log4j Log Output Learning (Eclipse)

Source: Internet
Author: User
Tags log4j

Learning URL 1:http://www.cnblogs.com/licheng/archive/2008/08/23/1274566.html

First, fast start

1. Download log4j compression package, local pressurization

Create a new Java project in the 2.Eclipse development tool, right-click the project select Properties to open the Properties window for the current project, select the Java Build Path >> library (libaries) > > Add an external jar (add External JARs) to add the Log4j.jar package you just downloaded to the project.

3. Create a new Log4j.properties profile in the project: This file is dedicated to configuring log information and its contents determine the output location and format of the log information. Right-click the project select New >> file named Xxx.properties

4. Write the configuration information to the file log4j.properties that you just created:

# # #将日志信息输出到控制台 # # #log4j. appender.stdout=org.apache.log4j.consoleappenderlog4j.appender.stdout.layout= ORG.APACHE.LOG4J.PATTERNLAYOUTLOG4J.APPENDER.STDOUT.LAYOUT.CONVERSIONPATTERN=%D{YYYY-MM-DD HH:mm:ss}%m%n  %l %n## #将日志信息输出到文件中 # # #log4j. appender.file=org.apache.log4j.fileappenderlog4j.appender.file.file= sysinfo.loglog4j.appender.file.layout= ORG.APACHE.LOG4J.PATTERNLAYOUTLOG4J.APPENDER.FILE.LAYOUT.CONVERSIONPATTERN=%D{YYYY-MM-DD HH:mm:ss}%m%n  %l%n # # #设置日志的优先级别 # # #log4j. rootlogger=debug,stdout,file

5. Create a new Java file to test whether the configuration was successful:

1 ImportOrg.apache.log4j.Logger;2 ImportOrg.apache.log4j.PropertyConfigurator;3 4  Public classLog4jdemo {5      Public Static voidMain (string[] args) {6Logger log =NULL;7         Try {8             //Initialize the log generator, load the log configuration file9Propertyconfigurator.configure ("Log4j.properties");TenLog = Logger.getlogger (Log4jdemo.class. GetName ()); One  ALog.debug ("main"); -             inti = 1/0; -}Catch(Exception e) { the             //Record Log - Log.info (E.getmessage ()); -             //e.printstacktrace (); -         } +     } -}

6. Output results

Ii. Expansion of Learning

Log4j Log Output Learning (Eclipse)

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.