Java-based log4j configuration and java-based log4j Configuration

Source: Internet
Author: User

Java-based log4j configuration and java-based log4j Configuration

Java-based log4j Configuration

Log4j has many advantages and is easy to use, that is, it is difficult to configure. Next I will introduce the configuration method of log4j.

Log4j is used to record logs.

The running process of the software is inseparable from logs. logs are mainly used to record important operation information during system operation, so as to monitor system operation and help users detect and avoid possible problems in advance, or locate the cause based on the log.

Logs are classified into the following types based on the Record Content:

 

To use log4j, first download the JAR file of log4j. log4j is an open source project of Apache, its official website is http://logging.apache.org/log4j

 

I have read a lot of log4j articles and basically have log4j configuration methods, but there are no log4j configuration steps. Let me talk about it:

(1) Add the JAR file used by log4j to the project.

In MyEclipse, select the Project to use log4j, and then select "Project" → "Properties" → "Java Build Path" → "Libraries" → "Add External JARs ......" Option. In the dialog box that appears, select JAR and find the file on your computer, which is your Jar package.

 

(2) Create the log4.properties File

To use log4j, you must create a log4j. propterties file. This file is used to configure log information, such as the output level, output destination, and output format.

Select the project to use log4j, right-click src, and select "New"> "File" in sequence. The "New File" dialog box is displayed, and the File name "log4j" is input. properties.

 

As for the configuration method of log4j, It is the section that let others reprint the bad one:

1 ### set log levels ### 2 log4j. rootLogger = debug, stdout, D, E 3 4 ### output to the console ### 5 log4j. appender. stdout = org. apache. log4j. leleappender 6 log4j. appender. stdout. target = System. out 7 log4j. appender. stdout. layout = org. apache. log4j. patternLayout 8 log4j. appender. stdout. layout. conversionPattern = % d {ABSOLUTE} % 5 p % c {1}: % L-% m % n 9 10 ### output to log file ### 11 log4j. appender. D = org. apache. log4j. D AilyRollingFileAppender12 log4j. appender. d. file = logs/log. log13 log4j. appender. d. append = true14 log4j. appender. d. threshold = DEBUG # output the logs above the DEBUG level 15 log4j. appender. d. layout = org. apache. log4j. patternLayout16 log4j. appender. d. layout. conversionPattern = %-d {yyyy-MM-dd HH: mm: ss} [% t: % r]-[% p] % m % n17 18 ### Save the exception information to a separate file ### 19 log4j. appender. D = org. apache. log4j. dailyRollingFileAppender20 log4j. Appender. d. file = logs/error. log # abnormal log file name 21 log4j. appender. d. append = true22 log4j. appender. d. threshold = ERROR # Only logs with the ERROR level and above are output !!! 23 log4j. appender. d. layout = org. apache. log4j. patternLayout24 log4j. appender. d. layout. conversionPattern = %-d {yyyy-MM-dd HH: mm: ss} [% t: % r]-[% p] % m % n

 

C v can be used directly for Project Creation (if you want to knock on it, I don't mind ...)

 

Use log4j in the main method:

1 public   class  TestLog4j  {2       public   static   void  main(String[] args)  {3          PropertyConfigurator.configure( " D:/Code/conf/log4j.properties " );4          Logger logger  =  Logger.getLogger(TestLog4j. class );5          logger.debug( " debug " );6          logger.error( " error " );7      } 8  }

 

 

 

Log4j output level:

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

 

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.