Configuration of the log4j

Source: Internet
Author: User
Tags print format

The log4j configuration file is used to set the level, the repository, and the layout of the logger, which can be set in key=value format or in XML format. By configuration, you can create a running environment for log4j.

Steps to create log4j:

    1. The associated jar package (Log4j-1.2.8.jar) is introduced in the project in the Lib directory.

    2. Create the Log4j.properties configuration file in the SRC directory.

log4j.rootlogger=debug, stdout, d###  output to 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###  output to log file  ## #log4j. appender.d= org.apache.log4j.dailyrollingfileappenderlog4j.appender.d.file=logs/log.loglog4j.appender.d.append=true      # #默认值是true to add a message to the specified file, false means to wrap the message                                       cover the specified file contents. log4j.appender.d.threshold=debug ##  output DEBUG levels above the log log4j.appender.d.layout= Org.apache.log4j.patternlayoutlog4j.appender.d.layout.conversionpattern=%-d{yyyy-mm-dd hh:mm:ss} [%t :%r]-[%p] %m%n

3. Modify the properties of the project as required.

4. Add the relevant statement to the class in which you want to output the log.

Define the property static Logger Logger=logger.getlogger (Test.class);//test the class for the output log calls Logger.debug ("Debug") in the related method;

Description

Log4j.rootlogger=debug, stdout, d//This sentence is the level of debug log information output to stdout, d two destinations, stdout, D can be arbitrarily named.
  • 1, the level of output of the log is: debug (7) , info (6) WARN (4) ERROR (3) , FATAL (0).

  • 2. the destination of the log output (Appender) is:

Org.apache.log4j.ConsoleAppender (console), Org.apache.log4j.FileAppender (file), Org.apache.log4j.DailyRollingFileAppender (daily generation of one day Log file), Org.apache.log4j.RollingFileAppender (creates a new file when the file size reaches the specified size), Org.apache.log4j.WriterAppender (sends the log information in stream format to any specified location).
    • 3, layout: log Output format, LOG4J provides the following types of layout:

Org.apache.log4j.HTMLLayout (layout in an HTML table), Org.apache.log4j.PatternLayout (with the flexibility to specify layout mode), Org.apache.log4j.SimpleLayout (contains log information and information Strings), Org.apache.log4j.TTCCLayout (contains information such as the time, thread, category, etc.) of the log generation.
    • 4. Printing parameters: log4j Format the log information in a print format similar to the printf function in C, as follows:

%M the message specified in the output code%p the output priority, that is, the Debug,info,warn,error,fatal%r output from the application boot to output the log information in milliseconds the output belongs to the class, usually the full name of the class%t output The name of the thread that generated the log event%n output a carriage return newline character, the Windows platform is "\ r \ n", the UNIX platform is "\ nthe"%d output log point in time, the default format is ISO8601, you can specify the format after that, such as:%d{yyy MMM DD HH : mm:ss,sss}, output similar to July 25, 2016 13:10:28,921%l the occurrence of the output log event, including the class name, the thread that occurred, and the number of rows in the code.


This article is from the "Java to school on Java" blog, please be sure to keep this source http://zhangyh8856643.blog.51cto.com/8846643/1829602

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