Using log4j in Java projects

Source: Internet
Author: User

    • log4j download
    1. Official website: http://logging.apache.org/log4j/
    2. log4j 1.2:http://logging.apache.org/log4j/1.2/download.html
    • Use steps
    1. Copy the log4j jar package to the project and add the build Path
    2. Creating a configuration file Log4j.properties
    3. Load the configuration file in the main function of the program: Propertyconfigurator.configure ("log4j.properties");
    4. Create log instance: Logger log = Logger.getlogger (Xxx.class);
    5. Log records: Log.debug ();
    • Configuration file Example

# # # Set Log level # # #
Log4j.rootlogger = Debug, stdout, D, E

# # # Output to console # # #
Log4j.appender.stdout = Org.apache.log4j.ConsoleAppender
Log4j.appender.stdout.Target = System.out
Log4j.appender.stdout.layout = Org.apache.log4j.PatternLayout
Log4j.appender.stdout.layout.ConversionPattern =%d{absolute}%5p%c{1}:%l-%m%n

# # # Output to log file # # #
LOG4J.APPENDER.D = Org.apache.log4j.DailyRollingFileAppender
Log4j.appender.d.file = Logs/log.log
Log4j.appender.d.append = True
Log4j.appender.d.threshold = DEBUG
Log4j.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.E = Org.apache.log4j.DailyRollingFileAppender
Log4j.appender.e.file = Logs/error.log
Log4j.appender.e.append = True
Log4j.appender.e.threshold = ERROR
Log4j.appender.e.layout = Org.apache.log4j.PatternLayout
Log4j.appender.e.layout.conversionpattern =%-d{yyyy-mm-dd HH:mm:ss} [%t:%r]-[%p]%m%n

    • Conversionpattern detailed

Format name meaning
The full name of the class to which the%c output log information belongs
%d the date or time of the output log time, the default format is ISO8601, or the format can be specified later, for example:%d{yyy-mm-dd HH:mm:ss}, output similar to: 2002-10-18-22:10:28
%f the class name of the class to which the output log information belongs
%l the occurrence of the output log event where the statement that outputs the log information is in the first line of the class it is in
%m the information specified in the output code, such as a message in log (message)
%n output a carriage return newline character, Windows platform is "RN", UNIX platform is "n"
%p output priority, or debug,info,warn,error,fatal. Debug if the debug () output is called, and so on
%r the number of milliseconds that the output from the application starts to output this log information
%t output The name of the thread that generated the log event

Using log4j in Java projects

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.