log4j.properties configuration details and examples - all tests passed

Recently, when I used log4j to write logs, I found that everything written on the Internet was the same. The writing was good, but it was incomplete, and the writing was full, but there was no format. It looked tired. Here, I have sorted out what I have collected on the Internet, and all of them have been successfully tested on the machine. Is there such a good document? 
########################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################## ####################################### 
#①Configure the root Logger, its syntax is: 

#log4j.rootLogger = [level],appenderName,appenderName2,... 
#level is the priority of logging, divided into OFF, TRACE, DEBUG, INFO, WARN, ERROR, FATAL, ALL 
##Log4j recommends using only four Level, priority from low to high are DEBUG, INFO, WARN, ERROR #Through 
the level defined here, you can control the switch to the corresponding level of log information in 
the application #For example, if the INFO level is defined here, the application All DEBUG level log information in the program will not be printed out. 
#appenderName is to specify where the log information is output. Multiple output destinations can be specified at the same time 
################################################################################################################################################################################################################################## ################################################# 
_ ################################################## ################# 
#②Configure the log information output destination Appender, its syntax is: 

#log4j.appender.appenderName = fully.qualified.name.of.appender.class 
#log4j.appender.appenderName.optionN = valueN 
#  #The
appenders provided by Log4j are as follows: 
#1)org.apache.log4j.ConsoleAppender (output to console) 
#2) org.apache.log4j.FileAppender (output to file) 
#3) org.apache.log4j.DailyRollingFileAppender (generates a log file every day) 
#4) org.apache.log4j.RollingFileAppender (file When the size reaches the specified size, a new file is generated) 
#5) org.apache.log4j.WriterAppender (send log information to any specified place in stream format) 

#1) ConsoleAppender option property 
# -Threshold = DEBUG: specify The lowest level of log message output 
# -ImmediateFlush = TRUE: the default value is true, all messages will be output immediately 
# -Target = System.err: the default value System.out, output to the console (err is red, out is black ) 

#2) FileAppender option property 
# -Threshold = INFO: Specify the lowest level of output of log messages 
# -ImmediateFlush = TRUE: The default value is true, all messages will be output immediately 
# -File = C:\log4j.log: Specify the message output to C:\log4j .log file 
# -Append = FALSE: The default value is true, append the message to the specified file, false means to overwrite the content of the specified file 
# -Encoding = UTF-8: You can specify the file encoding format 

#3) DailyRollingFileAppender option property 
# -Threshold = WARN: Specify the lowest level of log message output 
# -ImmediateFlush = TRUE: The default value is true, all messages will be output immediately 
# -File = C:\log4j.log: Specify the message output to C:\log4j .log file 
# -Append = FALSE: The default value is true, append the message to the specified file, and false means to overwrite the content of the specified file with the message 
# -DatePattern='.'yyyy-ww: Scroll the file once a week, that is, every week Generate a new file. You can also use the following parameters: 
# '.'yyyy-MM:monthly 
# '.'yyyy-ww:weekly 
# '.'yyyy-MM-dd:daily 
# '.'yyyy-MM-dd-a: twice a day 
# '.'yyyy-MM-dd-HH: every hour 
# '.'yyyy-MM-dd-HH-mm: every minute 
# -Encoding = UTF-8: You can specify the file encoding format 

#4) RollingFileAppender option property 
# -Threshold = ERROR: Specify the lowest level of log message output 
# -ImmediateFlush = TRUE: The default value is true, all messages will be output immediately 
# -File = C:/log4j.log: Specify the message output to C:/log4j .log file 
# -Append = FALSE: The default value is true, append the message to the specified file, false means to overwrite the content of the specified file with the message 
# -MaxFileSize = 100KB: The suffix can be KB, MB, GB. When the log file reaches this When the size is set, it will scroll automatically. For example: log4j.log.1 
# -MaxBackupIndex = 2: Specify the maximum number of rolling files that can be generated 
# -Encoding = UTF-8: You can specify the file encoding format 
####### ################################################## ################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################## 
_ ################################################## ### 
#③Configure the format (layout) of the log information, its syntax is: 

#log4j.appender.appenderName.layout = fully.qualified.name.of.layout.class 
#log4j.appender.appenderName.layout.optionN = valueN 

#Log4j provides the following layouts: 
#5)org.apache .log4j.HTMLLayout (layout in HTML form) 
#6) org.apache.log4j.PatternLayout (flexible to specify the layout pattern) 
#7) org.apache.log4j.SimpleLayout (contains the level of log information and information strings) 
#8) org.apache.log4j.TTCCLayout (including the time, thread, category, etc. of log generation) 
#9) org.apache.log4j.xml.XMLLayout (layout in XML format) 

#5) HTMLLayout option attribute 
# -LocationInfo = TRUE: default value false, output java file name and line number 
# -Title=Struts Log Message: default value Log4J Log Messages 

#6)PatternLayout option attribute 
# -ConversionPattern = %m%n: format the specified message (parameter meaning below) 

#9)XMLLayout option attribute 
# -LocationInfo = TRUE: default value is false, output java file name and line number 

#Log4J uses a print format similar to the printf function in C language to format log information, and the printing parameters are as follows: 
# %m output The message specified in the code 
# %p Output priority, that is, DEBUG, INFO, WARN, ERROR, FATAL 
# %r The number of milliseconds it takes to output the log information from the application startup to the output of the log information 
# %c The category to which the output belongs, usually where it is The full name of the class 
# %t Output the name of the thread that generated the log event 
# %n Output a carriage return and line feed, "\r\n" for Windows platforms, "\n" for Unix platforms 
# %d Output the log time point Date or time, the default format is ISO8601, you can also specify the format after 
# such as: %d{yyyy year MM month dd day HH:mm:ss,SSS}, the output is similar to: 2012-01-05 22:10: 28,921 
# %l Output the location where the log event occurs, including the category name, the thread where it occurred, and the number of lines in the code 
# Such as: Testlog.main(TestLog.java:10) 
# %F Output where the log message is generated File name 
# %L Line number in the output code 
# %x Output NDC (Nested Diagnostic Environment) associated with the current thread, such as java servlets multi-client multi-threaded applications 
# %% Output a "%" character 

# Modifiers can be added between the % and the pattern character to control the minimum width, maximum width, and alignment of the text. For example: 
# %5c: output category name, the minimum width is 5, category<5, right-aligned by default 
# %-5c: output category name, minimum width is 5, category<5, "-" sign specifies left-aligned , there will be spaces 
# %.5c: output category name, the maximum width is 5, category>5, the extra characters on the left will be truncated, <5 will not have spaces 
# %20.30c:category name<20 fill spaces , and right-aligned, >30 characters, truncate the characters from the left to sell out 
############################## ################################################## 
################################################## ############################## 
#④Specify a specific level of output for a specific package 
#log4j.logger.org.springframework=DEBUG 
################################################## ############################## 

#OFF,systemOut,logFile,logDailyFile,logRollingFile,logMail,logDB,ALL 
log4j.rootLogger =ALL,systemOut,logFile,logDailyFile,logRollingFile,logMail,logDB 


# output to console 

log4j.appender.systemOut = org.apache.log4j.ConsoleAppender 
log4j.appender.systemOut.layout = org.apache.log4j.PatternLayout 
log4j.appender.systemOut.layout.ConversionPattern = [%-5p][%-22d{yyyy/MM/dd HH:mm:ssS}][%l]%n%m%n 
log4j.appender.systemOut.Threshold = DEBUG 
log4j.appender.systemOut.ImmediateFlush = TRUE 
log4j.appender.systemOut.Target = System.out 


# output to file 

log4j.appender.logFile = org.apache.log4j.FileAppender 
log4j.appender.logFile.layout = org.apache.log4j.PatternLayout 
log4j.appender.logFile.layout.ConversionPattern = [%-5p][%-22d{yyyy/MM/dd HH:mm:ssS}][%l]%n%m%n 
log4j.appender.logFile.Threshold = DEBUG 
log4j.appender.logFile.ImmediateFlush = TRUE 
log4j.appender.logFile.Append = TRUE 
log4j.appender.logFile.File = ../Struts2/WebRoot/log/File/log4j_Struts.log 
log4j.appender.logFile.Encoding = UTF-8

 
#Press DatePattern to output to file 

log4j.appender.logDailyFile = org.apache.log4j.DailyRollingFileAppender 
log4j.appender.logDailyFile.layout = org.apache.log4j.PatternLayout 
log4j.appender.logDailyFile.layout.ConversionPattern = [%-5p][%-22d{yyyy/MM/dd HH:mm:ssS}][%l]%n%m%n 
log4j.appender.logDailyFile.Threshold = DEBUG 
log4j.appender.logDailyFile.ImmediateFlush = TRUE 
log4j.appender.logDailyFile.Append = TRUE 
log4j.appender.logDailyFile.File = ../Struts2/WebRoot/log/DailyFile/log4j_Struts 
log4j.appender.logDailyFile.DatePattern = '.'yyyy-MM-dd-HH-mm'.log' 
log4j.appender.logDailyFile.Encoding = UTF-8 


#Set the file size to output to a file 

log4j.appender.logRollingFile = org.apache.log4j.RollingFileAppender 
log4j.appender.logRollingFile.layout = org.apache.log4j.PatternLayout 
log4j.appender.logRollingFile.layout.ConversionPattern = [%-5p][%-22d{yyyy/MM/dd HH:mm:ssS}][%l]%n%m%n 
log4j.appender.logRollingFile.Threshold = DEBUG 
log4j.appender.logRollingFile.ImmediateFlush = TRUE 
log4j.appender.logRollingFile.Append = TRUE 
log4j.appender.logRollingFile.File = ../Struts2/WebRoot/log/RollingFile/log4j_Struts.log 
log4j.appender.logRollingFile.MaxFileSize = 1MB 
log4j.appender.logRollingFile.MaxBackupIndex = 10 
log4j.appender.logRollingFile.Encoding = UTF-8 


#Send logs by Email 

log4j.appender.logMail = org.apache.log4j.net.SMTPAppender 
log4j.appender.logMail.layout = org.apache.log4j.HTMLLayout 
log4j.appender.logMail.layout.LocationInfo = TRUE 
log4j.appender.logMail.layout.Title = Struts2 Mail LogFile 
log4j.appender.logMail.Threshold = DEBUG 
log4j.appender.logMail.SMTPDebug = FALSE 
log4j.appender.logMail.SMTPHost = SMTP.163.com 
log4j.appender.logMail.From = [email protected] 
log4j.appender.logMail.To = [email protected] 
#log4j.appender.logMail.Cc = [email protected] 
#log4j.appender.logMail.Bcc = [email protected] 
log4j.appender.logMail.SMTPUsername = xly3000 
log4j.appender.logMail.SMTPPassword = 1234567 
log4j.appender.logMail.Subject = Log4j Log Messages 
#log4j.appender.logMail.BufferSize = 1024 
#log4j.appender.logMail.SMTPAuth = TRUE 


#Log logs to MySQL database 

log4j.appender.logDB = org.apache.log4j.jdbc.JDBCAppender 
log4j.appender.logDB.layout = org.apache.log4j.PatternLayout 
log4j.appender.logDB.Driver = com.mysql.jdbc.Driver 
log4j.appender.logDB.URL = jdbc:mysql://127.0.0.1:3306/xly 
log4j.appender.logDB.User = root 
log4j.appender.logDB.Password = 123456 
log4j.appender.logDB.Sql = INSERT INTOT_log4j(project_name,create_date,level,category,file_name,thread_name,line,all_category,message)values('Struts2','%d{yyyy-MM-ddHH:mm:ss}','%p','%c','%F','%t','%L','%l','%m')

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325329774&siteId=291194637