log4j configuration and examples of a

Source: Internet
Author: User

Website to publish, in order to track some logs, need to use log4j, so I studied the log4j configuration

Post a configuration source file that you used first log4j.properties

Log4j.rootlogger=debug, console, FILE
# # for CONSOLE
log4j.appender.console= Org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.patternlayout
LOG4J.APPENDER.CONSOLE.LAYOUT.CONVERSIONPATTERN=%D{MM-DDHH:MM:SS}[%C-%L][%T][%-4R]-%m%n
# # for file
Log4j.appender.file=org.apache.log4j.rollingfileappender
Log4j.appender.file.file=d:/logs/log4j.log
LOG4J.APPENDER.FILE.MAXFILESIZE=1MB
Log4j.appender.FILE.Append = True
log4j.appender.file.layout= Org.apache.log4j.PatternLayout
Log4j.appender.file.layout.conversionpattern=%d{yyyy-mm-ddhh\:mm\:ss} [%t]% -5p%c (line-%l)%-4r%x-%m%n

Here is a key explanation:

Log4j.rootlogger=debug, CONSOLE, FILE-----Configuration root Logger, the first word after the equal sign, debug represents the output level (there are 5 kinds of fatal Errorwarn INFO Debug, the more the output more detailed, Each level in order contains all the preceding outputs and masks all subsequent outputs. There are two recommended options: Error only; DEBUG: Log The details of all errors. Under production configuration, turn off the console console option, only file is enabled, and the next two console and file represent the location of the log output, and the following two can be arbitrarily named, in order to be clearly understood, using words that can see the meaning at a glance. For example, using the console to represent the console output, file represents the output

Log4j.appender.console=org.apache.log4j.consoleappender-------Notice the CONSOLE in front of the equals sign, which corresponds to your definition above, The meaning of this sentence is to define console as Org.apache.log4j.ConsoleAppender (log4j console output), LOG4J provides the following appender:

Org.apache.log4j.ConsoleAppender (console)

Org.apache.log4j.FileAppender (file)

Org.apache.log4j.DailyRollingFileAppender (Generate a log file every day)

Org.apache.log4j.RollingFileAppender (a new file is generated when the file size reaches a specified size)

Org.apache.log4j.WriterAppender (send log information to any specified place in streaming format)

Log4j.appender.console.layout=org.apache.log4j.patternlayout-------Define the output layout (output format) used by log4j.

LOG4J offers the following 4 layout styles:

Org.apache.log4j.HTMLLayout (layout in HTML tabular format)

Org.apache.log4j.PatternLayout (You can specify the layout pattern flexibly, that is, you can customize the output style),

Org.apache.log4j.SimpleLayout (The level and information string that contains the log information),

Org.apache.log4j.TTCCLayout (contains information about the time, thread, category, and so on that the log was generated)

LOG4J.APPENDER.CONSOLE.LAYOUT.CONVERSIONPATTERN=%D{MM-DDHH:MM:SS}[%C-%L][%T][%-4R]-%m%n------- This is for patternlayou your custom output format, focusing on the printing parameters, log4j is similar to the C language in the print format of the function of the format of the log information

%M the message specified in the output code

%p output priority, i.e. Debug,info,warn,error,fatal

%r output the number of milliseconds it takes to boot to output the log information

The class to which the%c output belongs, usually the full name of the class in which it is located

%t output The name of the thread that generated the log event

%n output a carriage return line break, Windows platform is "\ r \ n", Unix platform is "\ n", that is, a hop message occupy one line

%d output log point-in-time date or time, followed by a pair of curly braces for custom formatting

The class to which the%c output belongs, usually the full name of the class in which it is located

%l Accurate to Line

%x Output Alignment

The following is a sentence I intercepted from the log information output from my console, which corresponds to the definition format of the above.

09-3010:34:04[COM.OPENSYMPHONY.XWORK2.CONFIG.PROVIDERS.INTERCEPTORBUILDER-45][MAIN][106]-No interceptor found for Name = Includemethods

Log4j.appender.file.file=d:/logs/log4j.log--------Define the location and file name of the output file

LOG4J.APPENDER.FILE.MAXFILESIZE=1MB----------Define the size of each file to exceed this size, create a new file, note MB or KB

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.