log4j configuration file

Source: Internet
Author: User
Tags print format

1. Configure the root logger, whose syntax is:

Log4j.rootlogger = [level], Appendername, Appendername, ...

Level is the priority of logging, which is divided into off, FATAL, ERROR, WARN, INFO, DEBUG, all, or levels you define. LOG4J recommends using only four levels, with the priority from high to low being error, WARN, INFO, DEBUG. By defining the level here, you can control the switch to the appropriate level of log information in your application. For example, if the info level is defined here, the log information for all debug levels in the application will not be printed. Appendername refers to where the B log information is exported. You can specify multiple output destinations at the same time.
Example: Log4j.rootlogger=info,console,file

This example means that the log information in the print application, in addition to the debug level, is output to the console and file


  2. Configure the log information output destination Appender, whose syntax is:

Log4j.appender.appenderName = Fully.qualified.name.of.appender.class
Log4j.appender.appenderName.option1 = value1
...
Log4j.appender.appenderName.option = Valuen

Among them, LOG4J provides the following types of Appender:
Org.apache.log4j.ConsoleAppender (console),
Org.apache.log4j.FileAppender (file),
Org.apache.log4j.DailyRollingFileAppender (generates a log file every day),
Org.apache.log4j.RollingFileAppender (creates a new file when the file size reaches the specified size),
Org.apache.log4j.WriterAppender (send log information in stream format to any specified location)

Example: Log4j.appender.file=org.apache.log4j.dailyrollingfileappender
Log4j.appender.file.file=c:/sys.log

This example means: Define a daily Appender that produces a log file. , and the location of the log file is c:/sys.log



3. Configure the format (layout) of the log information with the following syntax:

  Log4j.appender.appenderName.layout = Fully.qualified.name.of.layout.class
Log4j.appender.appenderName.layout.option1 = value1
...  
Log4j.appender.appenderName.layout.option = Valuen

where the layout provided by Log4j has several e:
  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 the level and information string for log information),
Org.apache.log4j.TTCCLayout (contains information about the time, thread, category, and so on)

log4j formats the log information in print format similar to the printf function in C, and prints the following parameters:%m The message specified in the output code

%p The output priority, which is Debug,info,warn,error,fatal
%r  Output from application boot to output the log information takes the number of milliseconds
%c output belongs to the class, usually the full name of the class
%t output the thread name that generated the log event
%n output a carriage return newline character, the Windows platform is "RN", and the UNIX platform is "n"
%d the date or time of the output log time, the default format is ISO8601, can also be specified after the format, such as:%d{yyy MMM dd hh:mm:ss,sss}, output similar to: October 18, 2002 22:10:28,921
%l The location where the output log event occurs, including the class name, the thread that occurred, and the number of rows in the code. Example: Testlog4.main (testlog4.java:10)

Example: Log4j.appender.file.layout=org.apache.log4j.patternlayout
LOG4J.APPENDER.FILE.LAYOUT.CONVERSIONPATTERN=[%D{YY/MM/DD Hh:mm:ss:sss}][%5p][%l]%m%n

The example means that the output format of the definition file log information is output in the specified mode, which is [yy/mm/dd HH:mm:ss:SSS time] [priority] [log event occurrence location] Log information line-wrapping

log4j configuration file

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.