java--log4j

Source: Internet
Author: User

log4j Technology

1. What is log technology and why should I use log technology in development?

LOG: It is an Apache company provided by the enterprise mainstream log technology, in the process of system operation, record key information, record error exception information technology. 4% of the total code volume of the Log Code

* Distinguish between System.out and log technology

* System.out input information to the console, certain output

* Log technology It exists level, through level control log whether output, output level of detail, output destination (console, file, send mail)

Usage log: Mainly used in the development process of debugging and after the project on-line maintenance (record bugs)

2. Using the log4j configuration file, configure the log framework to use

Src/log4j.xml

Src/log4j.properties (Simple)

Configuration Log4j.properties has three components

① Component One: Logger (loggers) is used to configure the log output level, which output source is used "One logger specifies multiple output sources (the output target of the log)"

Format: Logger name = level, output source 1, Output source 2 ...

  Log4j.rootlogger=info, stdout    

* LOG4J provides log levels from high to Low: fatal (fatal error), error (normal error), warn (warning), info (info), debug (Debug), Trace (Stack)

* When logging is log4j, only information at the higher level of the configuration is logged

② component two: output source (appenders) can define multiple output sources (console, log file, mail, database) in log4j

Format: Log4j.appender. Output Source name = Implementation class

////

③ Component III: Layout (Layouts) What information is recorded in the log

// Custom layout log4j.appender.stdout.layout=org.apache.log4j.PatternLayout//  Custom layout format (view layout-related documents, read the meaning)Log4j.appender.stdout.layout.conversionpattern=%d{absolute}%5p%c{1}:%l-%m%n  

3. Using log4j to log logs in the program

① Step One: Defining loggers (in Java classes)

// parameter is the current class Private Static Final Logger LOG = Logger.getlogger (log4jtest.  Class);

② Step Two: Use log4j to provide a log of each level of method logging

    1. Log.fatal ("Fatal error");
    2. Log.error ("ordinary Error");
    3. Log.warn ("warning message");
    4. Log.info ("General Information");
    5. Log.debug ("Debug Information");
    6. Log.trace ("stack Information");

* Common: Error, warn, info, debug

Example

Log4j.properties

):

# # Recorder (loggers) output source (appenders) layout (Layouts) ###### org.apache.log4j.ConsoleAppender Console # # #log4j. appender.stdout =Org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target=System.outlog4j.appender.stdout.layout=Org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d{absolute}%5p%c{1}:%l-%m%n### org.apache.log4j.FileAppender File # # #log4j. Appender.file=Org.apache.log4j.FileAppenderlog4j.appender.file.File=d:/mylog.log### Org.apache.log4j.PatternLayout has the flexibility to specify the layout mode # # #log4j. Appender.file.layout=org.apache.log4j.patternlayout###%d The date or time of the output log point in time, the default format is ISO8601 ######%P-Output priority, i.e. debug,info,warn,error,fatal######%The c output belongs to the class, which is usually the full name of the class ######%L OUTPUT The location of the log event, including the class name, the thread that occurred, and the number of rows in the code ######%the message specified in the M output code ######%n outputs a carriage return newline character, the Windows platform is "\ r \ n" and the UNIX platform is "\ n" # # #log4j. Appender.file.layout.ConversionPattern=%d{absolute}%5p%c{1}:%l-%m%The n### Log4j.rootlogger logger is divided into debug error info message warn warning Errors # # #log4j. Rootlogger=debug,stdout

java--log4j

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.