Log4j.properties Configuration Detailed

Source: Internet
Author: User

The first step: Join Log4j-1.2.8.jar to Lib.

Step two: Establish log4j.properties under the classpath. The contents are as follows:

Log4j.rootcategory=info, stdout, R

Log4j.appender.stdout=org.apache.log4j.consoleappender
Log4j.appender.stdout.layout=org.apache.log4j.patternlayout
LOG4J.APPENDER.STDOUT.LAYOUT.CONVERSIONPATTERN=[QC]%p [%t]%c.%m (%l) | %m%n

Log4j.appender.r=org.apache.log4j.dailyrollingfileappender
Log4j.appender.r.file=d:\\tomcat 5.5\\logs\\qc.log
Log4j.appender.r.layout=org.apache.log4j.patternlayout
1log4j.appender.r.layout.conversionpattern=%d-[ts]%p%t%c-%m%n

Log4j.logger.com.neusoft=debug
Log4j.logger.com.opensymphony.oscache=error
Log4j.logger.net.sf.navigator=error
Log4j.logger.org.apache.commons=error
Log4j.logger.org.apache.struts=warn
Log4j.logger.org.displaytag=error
Log4j.logger.org.springframework=debug
Log4j.logger.com.ibatis.db=warn
Log4j.logger.org.apache.velocity=fatal

Log4j.logger.com.canoo.webtest=warn

Log4j.logger.org.hibernate.ps.preparedstatementcache=warn
Log4j.logger.org.hibernate=debug
Log4j.logger.org.logicalcobwebs=warn

Log4j.rootcategory=info, stdout, R

Log4j.appender.stdout=org.apache.log4j.consoleappender
Log4j.appender.stdout.layout=org.apache.log4j.patternlayout
LOG4J.APPENDER.STDOUT.LAYOUT.CONVERSIONPATTERN=[QC]%p [%t]%c.%m (%l) | %m%n

Log4j.appender.r=org.apache.log4j.dailyrollingfileappender
Log4j.appender.r.file=d:\\tomcat 5.5\\logs\\qc.log
Log4j.appender.r.layout=org.apache.log4j.patternlayout
1log4j.appender.r.layout.conversionpattern=%d-[ts]%p%t%c-%m%n

Log4j.logger.com.neusoft=debug
Log4j.logger.com.opensymphony.oscache=error
Log4j.logger.net.sf.navigator=error
Log4j.logger.org.apache.commons=error
Log4j.logger.org.apache.struts=warn
Log4j.logger.org.displaytag=error
Log4j.logger.org.springframework=debug
Log4j.logger.com.ibatis.db=warn
Log4j.logger.org.apache.velocity=fatal

Log4j.logger.com.canoo.webtest=warn

Log4j.logger.org.hibernate.ps.preparedstatementcache=warn
Log4j.logger.org.hibernate=debug
Log4j.logger.org.logicalcobwebs=warn

The third step: the corresponding modification of the properties, before you have to know what these are, in the second part of the explanation.

Fourth step: Add the relevant statement in the class to output the log:

Definition attribute: Protected final Log log = Logfactory.getlog (GetClass ());

In the appropriate method:

if (log.isdebugenabled ()) {

Log.debug ("System .....");

}

Second, log4j Description

1 Og4j.rootcategory=info, stdout, R

This sentence is the output of the log information of level info to stdout and R, the definition of stdout and r in the following code, can be arbitrarily named. Level can be divided into off, FATAL, error, WARN, INFO, DEBUG, all, if the configuration off does not play any information, if configured as info so only display info, WARN, error log information, and DEBUG information will not be displayed, Refer to the third section to define the logger in the configuration file.

3 Log4j.appender.stdout=org.apache.log4j.consoleappender

This sentence defines what type of output is named stdout, which can be

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)

Refer to the third section to define the Appender in the configuration file.

4 Log4j.appender.stdout.layout=org.apache.log4j.patternlayout

This sentence is the type of layout that defines the output named StdOut, which can be

Org.apache.log4j.HTMLLayout (Layout in HTML table Form),

Org.apache.log4j.PatternLayout (flexibility to specify layout mode),

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

Org.apache.log4j.TTCCLayout (contains information about the time, thread, category, etc.) of the log

Refer to the third section to define layout in the configuration file.

5 log4j.appender.stdout.layout.conversionpattern= [QC]%p [%t]%c.%m (%l) | %m%n

If you use the pattern layout to specify the exact format of the printed information Conversionpattern, print the parameters as follows:

%M the message specified in the output code

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

%r output the number of milliseconds that the log information is consumed from the application boot to output

%c output belongs to the class, which is 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, Windows platform is "RN", 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{yyyy MMM dd hh:mm:ss,sss}, output similar: October 18, 2002 22:10:28,921

%l where the output log event occurs, including the class name, the thread that occurred, and the number of rows in the code.

[QC] is the beginning of the log information, can be any character, generally referred to as the project abbreviation.

The output information

[TS] DEBUG [main] Abstractbeanfactory.getbean (189) | Returning cached instance of singleton Bean ' myautoproxy '

Refer to the third section to define the formatting log information in the configuration file.

7 Log4j.appender.r=org.apache.log4j.dailyrollingfileappender

This sentence is the same as the 3rd line. The type that defines the output that is named R produces a log file every day.

8 Log4j.appender.r.file=d:\\tomcat 5.5\\logs\\qc.log

This sentence is a file named D:\\tomcat that defines the output named R 5.5\\logs\\qc.log can be modified by itself.

9 Log4j.appender.r.layout=org.apache.log4j.patternlayout

Same as Line 4th.

Ten Log4j.appender.r.layout.conversionpattern=%d-[ts]%p%t%c-%m%n

Same as Line 5th.

Log4j.logger.com. Neusoft =debug

Specifies that all classes under the Com.neusoft package are rated as Debug.

You can change the Com.neusoft to the package name that you use for your project.

Log4j.logger.com.opensymphony.oscache=error

Log4j.logger.net.sf.navigator=error

These two sentences are the error level of the two packages, and if Ehcache is not configured in the project, these two sentences are not required.

Log4j.logger.org.apache.commons=error

Log4j.logger.org.apache.struts=warn

These two sentences are struts's packages.

Log4j.logger.org.displaytag=error

This sentence is Displaytag's bag. (used by the QC Problem List page)

Log4j.logger.org.springframework=debug

This sentence is spring's package.

Log4j.logger.org.hibernate.ps.preparedstatementcache=warn

Log4j.logger.org.hibernate=debug

These two sentences are Hibernate's package.

---------------------------------------------------------------------------------

[Use in code]


public class Testlog4j {
public static void Main (string[] args) {
Propertyconfigurator.configure ("D:/code/conf/log4j.properties");
Logger Logger = Logger.getlogger (testlog4j. Class);
Logger.debug ("Debug");
Logger.error ("error");
}
}


Run and see if the exception is stored in a separate file Error.log


Log4j.properties use
I. Description of the meaning of the parameter
Types of output levels
ERROR, WARN, INFO, DEBUG
Error is a critical error, mainly a program
WARN for general warnings, such as session loss
Info is the general information to display, such as login log out
Debug information for the program
Configure log information Output destination
Log4j.appender.appenderName = Fully.qualified.name.of.appender.class
1.org.apache.log4j.consoleappender (console)
2.org.apache.log4j.fileappender (file)
3.org.apache.log4j.dailyrollingfileappender (Generate a log file every day)
4.org.apache.log4j.rollingfileappender (creates a new file when the file size reaches the specified size)
5.org.apache.log4j.writerappender (send log information in stream format to any specified location)
Configure the format of log information
Log4j.appender.appenderName.layout = Fully.qualified.name.of.layout.class
1.org.apache.log4j.htmllayout (Layout in HTML table Form),
2.org.apache.log4j.patternlayout (flexibility to specify layout mode),
3.org.apache.log4j.simplelayout (contains the level of log information and the information string),
4.org.apache.log4j.ttcclayout (contains information about the time, thread, category, etc.) of the log
Console options
Threshold=debug: Specifies the lowest level of output for log messages.
Immediateflush=true: The default value is true, meaning that all messages will be output immediately.
Target=system.err: By default: System.out, specify output console
Fileappender Options
THRESHOLD=DEBUF: Specifies the lowest level of output for log messages.
Immediateflush=true: The default value is true, meaning that all messages will be output immediately.
File=mylog.txt: Specifies the message output to the Mylog.txt file.
Append=false: The default value is True, the message is added to the specified file, and false refers to overwriting the message with the specified file content.
Rollingfileappender Options
Threshold=debug: Specifies the lowest level of output for log messages.
Immediateflush=true: The default value is true, meaning that all messages will be output immediately.
File=mylog.txt: Specifies the message output to the Mylog.txt file.
Append=false: The default value is True, the message is added to the specified file, and false refers to overwriting the message with the specified file content.
MAXFILESIZE=100KB: The suffix can be kb, MB, or GB. When the log file reaches this size, it will automatically scroll to move the original content to the Mylog.log.1 file.
maxbackupindex=2: Specifies the maximum number of scroll files that can be produced.
log4j.appender.a1.layout.conversionpattern=%-4r%-5p%d{yyyy-mm-dd HH:mm:ssS}%c%m%n
The meaning of several symbols in the log Information format:
-X: Left-aligned when the information is output;
%p: Output log information priority, i.e. Debug,info,warn,error,fatal,
%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: October 18, 2002 22:10:28,921
%r: The number of milliseconds to output the log information from the application boot to output
%c: The class in which the output log information 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
%l: The location of the output log event, which corresponds to the combination of%c.%m (%f:%l), including the class name, the thread that occurred, and the number of rows in the code. Example: Testlog4.main (testlog4.java:10)
%x: The NDC (nested diagnostic environment) associated with the output and current line threads, especially for multi-client multithreaded applications such as Java Servlets.
Percent: Output a "%" character
%F: The name of the file where the output log message was generated
%l: Line numbers in the output code
%M: The specified message in the output code, resulting in the log specific information
%n: Output a carriage return line break, Windows platform is "\ r \ n", Unix platform for "\ n" Output log information line-wrapping
You can add modifiers between% and pattern characters to control their minimum width, maximum width, and text alignment. Such as:
1)%20c: Specify the name of the output category, the minimum width is 20, if the category name is less than 20, the default is the right alignment.
2)%-20c: Specify the name of the output category, the minimum width is 20, if the category name is less than 20, the "-" number specifies left-aligned.
3)%.30c: Specify the name of the output category, the maximum width is 30, if the category name is greater than 30, will be the left more than the character of the cut off, but less than 30, there will be no spaces.
4)%20.30c: If the category name is less than 20, fill in the blanks, and right-aligned, if its name is longer than 30 characters, the character from the left is more than the output of the characters cut off

Three. Advanced usage
Experimental Purpose:
1. Write fatal-level error to 2000NT log
2. Warn,error,fatal level error sending email notification Administrator
3. Other levels of errors directly in the background output
Experimental steps:
Output to 2000NT Log
1. Copy the NTEventLogAppender.dll in the log4j compression package to the Winnt\System32 directory
2. Write the configuration file log4j.properties
# in 2000 system log output
Log4j.logger.ntlog=fatal, A8
# APPENDER A8
Log4j.appender.a8=org.apache.log4j.nt.nteventlogappender
Log4j.appender.a8.source=javatest
Log4j.appender.a8.layout=org.apache.log4j.patternlayout
LOG4J.APPENDER.A8.LAYOUT.CONVERSIONPATTERN=%-4R%-5p [%t]%37c%3x-%m%n
3. Calling code:
Logger logger2 = Logger.getlogger ("Ntlog"); To be the same as the name set in the configuration file
LOGGER2.DEBUG ("Debug!!!");
Logger2.info ("info!!!");
Logger2.warn ("Warn!!!");
LOGGER2.ERROR ("Error!!!");
Only this error will be written to 2000 log
Logger2.fatal ("Fatal!!!");
Send an email notification to the administrator:
1. First download JavaMail and JAF,
Http://java.sun.com/j2ee/ja/javamail/index.html
Http://java.sun.com/beans/glasgow/jaf.html
References Mail.jar and Activation.jar in the project.
2. Write the configuration file
# Send logs to email
Log4j.logger.maillog=warn,a5
# APPENDER A5
Log4j.appender.a5=org.apache.log4j.net.smtpappender
Log4j.appender.a5.buffersize=5
[Email protected]
[Email protected]
Log4j.appender.a5.subject=errorlog
Log4j.appender.a5.smtphost=smtp.263.net
Log4j.appender.a5.layout=org.apache.log4j.patternlayout
LOG4J.APPENDER.A5.LAYOUT.CONVERSIONPATTERN=%-4R%-5p [%t]%37c%3x-%m%n
3. Calling code:
Send logs to Mail
Logger Logger3 = Logger.getlogger ("Maillog");
Logger3.warn ("Warn!!!");
LOGGER3.ERROR ("Error!!!");
Logger3.fatal ("Fatal!!!");
Output all categories of errors in the background:
1. Write the configuration file
# in the background output
Log4j.logger.console=debug, A1
# APPENDER A1
Log4j.appender.a1=org.apache.log4j.consoleappender
Log4j.appender.a1.layout=org.apache.log4j.patternlayout
LOG4J.APPENDER.A1.LAYOUT.CONVERSIONPATTERN=%-4R%-5p [%t]%37c%3x-%m%n
2. Calling code
Logger logger1 = Logger.getlogger ("console");
LOGGER1.DEBUG ("Debug!!!");
Logger1.info ("info!!!");
Logger1.warn ("Warn!!!");
LOGGER1.ERROR ("Error!!!");
Logger1.fatal ("Fatal!!!");
--------------------------------------------------------------------
All profiles: log4j.properties
# in the background output
Log4j.logger.console=debug, A1
# APPENDER A1
Log4j.appender.a1=org.apache.log4j.consoleappender
Log4j.appender.a1.layout=org.apache.log4j.patternlayout
LOG4J.APPENDER.A1.LAYOUT.CONVERSIONPATTERN=%-4R%-5p [%t]%37c%3x-%m%n
# in 2000 system log output
Log4j.logger.ntlog=fatal, A8
# APPENDER A8
Log4j.appender.a8=org.apache.log4j.nt.nteventlogappender
Log4j.appender.a8.source=javatest
Log4j.appender.a8.layout=org.apache.log4j.patternlayout
LOG4J.APPENDER.A8.LAYOUT.CONVERSIONPATTERN=%-4R%-5p [%t]%37c%3x-%m%n
# Send logs to email
Log4j.logger.maillog=warn,a5
# APPENDER A5
Log4j.appender.a5=org.apache.log4j.net.smtpappender
Log4j.appender.a5.buffersize=5
[Email protected]
[Email protected]
Log4j.appender.a5.subject=errorlog
Log4j.appender.a5.smtphost=smtp.123.net
Log4j.appender.a5.layout=org.apache.log4j.patternlayout
LOG4J.APPENDER.A5.LAYOUT.CONVERSIONPATTERN=%-4R%-5p [%t]%37c%3x-%m%n
Total Code: Log4jtest.java
/*
* Date Created 2015-07-24
*/
Package Edu.bcu.Bean;
Import org.apache.log4j.*;
Import org.apache.log4j.nt.*;
Import org.apache.log4j.net.*;
/**
* @author www.wjrong.com
*/
public class Log4jtest
{
public static void Main (String args[])
{
Propertyconfigurator.configure ("Log4j.properties");
Output in the background
Logger logger1 = Logger.getlogger ("console");
LOGGER1.DEBUG ("Debug!!!");
Logger1.info ("info!!!");
Logger1.warn ("Warn!!!");
LOGGER1.ERROR ("Error!!!");
Logger1.fatal ("Fatal!!!");
In NT system log output
Logger logger2 = Logger.getlogger ("Ntlog");
Nteventlogappender NLA = new Nteventlogappender ();
LOGGER2.DEBUG ("Debug!!!");
Logger2.info ("info!!!");
Logger2.warn ("Warn!!!");
LOGGER2.ERROR ("Error!!!");
Only this error will be written to 2000 log
Logger2.fatal ("Fatal!!!");
Send logs to Mail
Logger Logger3 = Logger.getlogger ("Maillog");
Smtpappender sa = new Smtpappender ();
Logger3.warn ("Warn!!!");
LOGGER3.ERROR ("Error!!!");
Logger3.fatal ("Fatal!!!");
}
}

Log4j.properties Configuration Detailed

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.