Open-source log technology log4j

Source: Internet
Author: User

Instructor's summary:

Logs: In addition to recording exception information, you can also record key information during normal operation of the program.

 

Use log4j to record log files:

001. Create a lib folder in the project and copy the downloaded jar package to the folder.

002. Right-click the copied jar package and perform the operation,

 

 

An additional project will be introduced into the external library.

003. Create a file named log4j. properties under the src directory

004. Write and paste the content of the prepared configuration file. You can correct the encoding method. Right-click the configuration file → properties → other → UTF-8, and then click OK. If an error is reported again, ignore it. Click OK and press Ctrl + s again.

005. In the class where the main method is located, add the following code:

// Create a static logger object

Public static logger = logger. getlogger ("log4j ");

Next, you can use logger. debug ("log Content ");

Note: By decompiling the source code, we know that the getlogger method has two reloads.

We currently use the overload of input strings. After multi-party data verification, any content passed in brackets has no impact on the abstract factory to generate a logger instance. So you can use it with confidence.

 

You only need to add logger. debug ("input string information") after the exception statement to output information in the console or file!

 

Configuration File Information:

 

### Set the logger output level and output destination ### log4j. rootlogger = debug, logfile

 

### Output log information to the file: jbit. log ### log4j. appender. logfile = org. apache. log4j. fileappender log4j. appender. logfile. file = temp/Mingming. log log4j. appender. logfile. layout = org. apache. log4j. patternlayout log4j. appender. logfile. layout. conversionpattern = % d {yyyy-mm-dd hh: mm: SS} % L % F % P % m % N

 

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.