Log4j.rootlogger=warn, console, File # #, MAIL
# # # Console standard out # # Log4j.appender.console=org.apache.log4j.consoleappender log4j.appender.console.target= System.out Log4j.appender.console.layout=org.apache.log4j.patternlayout Log4j.appender.console.layout.conversionpattern=%d{absolute}%5p%c{1}:%l-%m%n
# # # File # # Log4j.appender.file=org.apache.log4j.dailyrollingfileappender log4j.appender.file.file=${ Catalina.home}/logs/ackonoa.log Log4j.appender.file.layout=org.apache.log4j.patternlayout Log4j.appender.file.layout.conversionpattern=%d{absolute}%5p%c{1}:%l-%m%n
# # # SMTP Appender EMAIL # # # #log4j. Appender.mail=org.apache.log4j.net.smtpappender #log4j. appender.mail.threshold= FATAL #log4j. appender.mail.buffersize=10 #[email protected] #log4j. appender.mail.smtpusername= User Name # log4j.appender.mail.smtppassword= password #log4j. appender.mail.smtphost=smtp.163.com #log4j. appender.mail.subject= log4j Message #[email protected] #log4j. Appender.mail.layout=org.apache.log4j.patternlayout # Log4j.appender.mail.layout.conversionpattern=[ackonoa]%d-[%t]%-5p%c:%l-%-4r%x-%m%n
# # # Set Log levels-for more verbose logging "info" to ' Debug ' # # #
Log4j.logger.com.ackon.oa.test=debug Log4j.logger.org.apache=warn log4j.logger.org.springframework=error, file Log4j.logger.org.hibernate=error, file Log4j.logger.org.hibernate.cache=info log4j.logger.org.hibernate.type=error, file
Log4j.properties use
The configuration file for log4j is the level, storage, or output source (appenders) and layout (Layouts) used to set the logger (loggers).
It can be used in key=value format settings or in XML format setting information. By configuration, you can create a running environment for log4j.
I. Description of the meaning of the parameter
Logger levels [level]
DEBUG, INFO, WARN, ERROR, FATAL
Debug 7 is the debugging information for the program
Info 6 is the general information to display
WARN 4 for general warning
Error 3 is critical
FATAL 0 Fatal Error
When the level setting is low, high-level logs are also output, such as: set to Eroor, fatal also print;
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 format), copy the printed content into an. html file
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=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.
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,----need to study
%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-mmm-dd hh:mm:ss,sss},
Output similar to: October 18, 2002 22:10:28, 921; %d{absolute} is not included date
%p: Output log information priority, that is, debug,info,warn,error,fatal, default or set length of less than 5 is left-aligned,%5p or larger right-aligned, left white;
%r: The number of milliseconds to output the log information from the application boot to output
%c: The output log information belongs to the class, the default is the full name of the class;
For example, 1 in Com.ackon.oa.test.logtest,%c{1} represents the class layer and 1 is the class name logtest,2 is test. Logtest
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, it is truncated from the far left character.
%t: Output The name of the thread that generated the log event----pending research
%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: Com.ackon.oa.test.LogTest.test (logtest.java:17)
%l: Line numbers in the output code
%x: The NDC (nested diagnostic environment) associated with the output and current line threads, especially for multi-client multithreaded applications such as Java Servlets. ----Pending Research
Percent: Output a "%" character
%F: The name of the file where the output log message was generated, this example is Logtest.java
%M: Output The message specified in the code, resulting in the log specific information, such as method body Log.debug ("This is debug information for the program");
%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:
Two. File Configuration Sample1
Log4j.rootlogger=error,stdout,r
# console output
Log4j.appender.stdout=org.apache.log4j.consoleappender
Log4j.appender.stdout.layout=org.apache.log4j.patternlayout
Log4j.appender.stdout.layout.conversionpattern=%-d{absolute} [%{3}c][%l]-[%5p]%m%n
# file output One document a day, the output path can be customized, usually under the root path
Log4j.appender.r=org.apache.log4j.dailyrollingfileappender
Log4j.appender.r.file=blog_log.txt
log4j.appender.r.maxfilesize=500kb
log4j.appender.r.maxbackupindex=10
Log4j.appender.r.layout=org.apache.log4j.patternlayout
Log4j.appender.r.layout.conversionpattern=%d{yyyy-mm-dd hh:mm:ss,sss} [%t] [%c] [%p]-%m%n
File Configuration Sample2
The log4j configuration file given below implements a full set of functions such as output to console, file, rollback file, send log mail, output to database log table, custom label, etc.
Log4j.rootlogger=debug,console,a1,im
#DEBUG, Console,file,rolling_file,mail,database
Log4j.addivity.org.apache=true
###################
# Console Appender
###################
Log4j.appender.console=org.apache.log4j.consoleappender
Log4j.appender.threshold=debug
Log4j.appender.console.target=system.out
Log4j.appender.console.layout=org.apache.log4j.patternlayout
Log4j.appender.console.layout.conversionpattern=[framework]%d-%c-%-4r [%t]%-5p%c%x-%m%n
#log4j. Appender.console.layout.conversionpattern=[start]%d{date}[date]%n%p[priority]%n%x[ndc]%n%t[thread] n%c[ category]%n%m[message]%n%n
#####################
# File Appender
#####################
Log4j.appender.file=org.apache.log4j.fileappender
Log4j.appender.file.file=file.log
Log4j.appender.file.append=false
Log4j.appender.file.layout=org.apache.log4j.patternlayout
Log4j.appender.file.layout.conversionpattern=[framework]%d-%c-%-4r [%t]%-5p%c%x-%m%n
# Use this layout for Logfactor 5 analysis
########################
# rolling File
########################
Log4j.appender.rolling_file=org.apache.log4j.rollingfileappender
Log4j.appender.ROLLING_FILE. Threshold=error
Log4j.appender.ROLLING_FILE. File=rolling.log
Log4j.appender.ROLLING_FILE. Append=true
Log4j.appender.ROLLING_FILE. maxfilesize=10kb
Log4j.appender.ROLLING_FILE. Maxbackupindex=1
Log4j.appender.rolling_file.layout=org.apache.log4j.patternlayout
Log4j.appender.rolling_file.layout.conversionpattern=[framework]%d-%c-%-4r [%t]%-5p%c%x-%m%n
####################
# Socket Appender
####################
Log4j.appender.socket=org.apache.log4j.rollingfileappender
Log4j.appender.socket.remotehost=localhost
log4j.appender.socket.port=5001
Log4j.appender.socket.locationinfo=true
# Set up for Log Facter 5
Log4j.appender.socket.layout=org.apache.log4j.patternlayout
log4j.appender.socet.layout.conversionpattern=[start]%d{date}[date]%n%p[priority]%n%x[ndc]%n%t[thread]%n%c[ category]%n%m[message]%n%n
########################
# Log Factor 5 Appender
########################
Log4j.appender.lf5_appender=org.apache.log4j.lf5.lf5appender
Log4j.appender.LF5_APPENDER. maxnumberofrecords=2000
########################
# SMTP Appender
#######################
Log4j.appender.mail=org.apache.log4j.net.smtpappender
Log4j.appender.mail.threshold=fatal
log4j.appender.mail.buffersize=10
[Email protected]
Log4j.appender.mail.smtpusername= User Name
log4j.appender.mail.smtppassword= Password
Log4j.appender.mail.smtphost=smtp.163.com
Log4j.appender.mail.subject=log4j Message
[Email protected]
Log4j.appender.mail.layout=org.apache.log4j.patternlayout
Log4j.appender.mail.layout.conversionpattern=[ackonoa]%d-[%t]%-5p%c:%l-%-4r%x-%m%n
########################
# JDBC Appender
#######################
Log4j.appender.database=org.apache.log4j.jdbc.jdbcappender
Log4j.appender.database.url=jdbc:mysql://localhost:3306/test
Log4j.appender.database.driver=com.mysql.jdbc.driver
Log4j.appender.database.user=root
log4j.appender.database.password=
Log4j.appender.database.sql=insert into log4j (Message) VALUES (' [Framework]%d-%c-%-4r [%t]%-5p%c%x-%m%n ')
Log4j.appender.database.layout=org.apache.log4j.patternlayout
Log4j.appender.database.layout.conversionpattern=[framework]%d-%c-%-4r [%t]%-5p%c%x-%m%n
Log4j.appender.a1=org.apache.log4j.dailyrollingfileappender
log4j.appender.a1.file=samplemessages.log4j
Log4j.appender.a1.datepattern=yyyymmdd-hh '. log4j '
Log4j.appender.a1.layout=org.apache.log4j.xml.xmllayout
###################
#自定义Appender
###################
log4j.appender.im = Net.cybercorlin.util.logger.appender.IMAppender
Log4j.appender.im.host = Mail.cybercorlin.net
Log4j.appender.im.username = Username
Log4j.appender.im.password = password
log4j.appender.im.recipient = [email protected]
Log4j.appender.im.layout=org.apache.log4j.patternlayout
Log4j.appender.im.layout.ConversionPattern =[framework]%d-%c-%-4r [%t]%-5p%c%x-%m%n
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.263.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 2003-11-13
*/
Package Edu.bcu.Bean;
Import org.apache.log4j.*;
Import org.apache.log4j.nt.*;
Import org.apache.log4j.net.*;
/**
* @author Yanxu
*/
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!!!");
}
}
Test mail, file, console output section has been successful, forget the original content from where, tell me that will be Added link
log4j Log Configuration