Configuration of Java log4j

Source: Internet
Author: User
Tags log4j

Configuration of Java log4j

Log4j has a lot of advantages, it is very convenient to use, is the configuration of some trouble, the following I introduce LOG4J configuration method.

Log4j is used to record logs.

The operation of the software can not be separated from the log. Logs are mainly used to record some important operational information during the operation of the system, to monitor the operation of the system, to help users detect and avoid possible problems in advance, or to find out the cause after a problem.

The log is divided into the following 3 categories, depending on the contents of the record:

    1. SQL log: Records the SQL statements executed by the system.
    2. Exception Log: Records the exception events that occurred during system operation.
    3. Business log: Record system running process, such as user login, operation record.

To use log4j, you first need to download the log4j jar file. log4j is an open source project for Apache , and its official website is http://logging.apache.org/log4j

Read a lot of log4j articles, basically have log4j configuration method, but there is no log4j configuration steps, I say it:

(1). Add the jar file used by log4j in the project.

In MyEclipse, select the project you want to use log4j and then select "Project" → "Properties" → "Java Build Path" → "Libraries" → "Add External JARs ..." option, pop-up the window to select the jar and find the files stored on your computer, which is your jar package.

(2). Create a Log4.properties file

Using log4j, you need to create a log4j.propterties file that is specifically designed to configure log information, such as output level, output destination, output format, and so on.

Then select the project to use log4j, right click on src, select "new" → "file" option, pop up the "New File" dialog box, enter the filename "log4j.properties", so the creation is successful.

As for the log4j configuration method, that is the part that let others reproduced rotten:

1 # # # set log Levels # # #2Log4j.rootlogger =Debug, stdout, D, E3  4 # # # output to console # # #5Log4j.appender.stdout =Org.apache.log4j.ConsoleAppender6Log4j.appender.stdout.Target =System.out7Log4j.appender.stdout.layout =Org.apache.log4j.PatternLayout8Log4j.appender.stdout.layout.ConversionPattern =%d{absolute}%5p%c{1}:%l-%m%N9  Ten # # # Output to log file # # # OneLOG4J.APPENDER.D =Org.apache.log4j.DailyRollingFileAppender ALog4j.appender.d.file = logs/Log.log -Log4j.appender.d.append =true -Log4j.appender.d.threshold =Debug # # Output debug levels above the log theLog4j.appender.d.layout =Org.apache.log4j.PatternLayout -Log4j.appender.d.layout.conversionpattern =%-d{yyyy-mm-dd HH:mm:ss} [%t:%r]-[%p]%m%N -   - # # # Save exception information to a separate file # # # +LOG4J.APPENDER.D =Org.apache.log4j.DailyRollingFileAppender -Log4j.appender.d.file = logs/Error.log # # Exception Log file name +Log4j.appender.d.append =true ALog4j.appender.d.threshold = error # # Output only logs above the error level!!! atLog4j.appender.d.layout =Org.apache.log4j.PatternLayout -Log4j.appender.d.layout.conversionpattern =%-d{yyyy-mm-dd HH:mm:ss} [%t:%r]-[%p]%m%n

Do the project directly C V on the line (if you want to knock I don't mind ...) )

Log4j is used in the Main method:

1  Public   classtestlog4j {2        Public   Static   voidMain (string[] args) {3Propertyconfigurator.configure ("D:/code/conf/log4j.properties" );4Logger Logger = Logger.getlogger (testlog4j.class );5Logger.debug ("Debug" );6Logger.error ("Error" );7      } 8}

Output level of the log4j:

1  FATAL      0  2 ERROR      3  3 WARN       4  4 INFO       6  5 DEBUG      

Configuration of Java 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.