log4j.properties configuration details and examples

      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 
################################################################################################################################################################################################################################## ################################################# 
_ ################################################## ################# 
#2 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 have The following: 
#1) org.apache.log4j.ConsoleAppender (output to console) 
#2) org.apache.log4j.FileAppender (output to file) 
#3) org.apache.log4j.DailyRollingFileAppender (generates a log every day file) 
#4) org.apache.log4j.RollingFileAppender (generates a new file when the file size reaches the specified size) 
#5) org.apache.log4j.WriterAppender (sends log information to any specified place in streaming format) 

#1) ConsoleAppender option properties 
# -Threshold = DEBUG: Specify the lowest level of output of log messages 
# -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 properties 
# -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 specified file content 
# -Encoding = UTF-8: You can specify the file encoding format 

#3) DailyRollingFileAppender option property 
# -Threshold = WARN: 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, and false means to overwrite the content of the specified file with the message 
# -DatePattern='.'yyyy-ww: Scroll every week Once a file, that is, a new file is generated every week. 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:hourly 
# '.'yyyy-MM-dd-HH-mm:every minute 
# -Encoding=UTF -8: File encoding format can be specified 

#4) RollingFileAppender option property 
# -Threshold = ERROR: 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: The specified message is output to the C:/log4j.log file 
# -Append = FALSE: The default value is true, the message will be appended to the specified file, and false means the message will overwrite the specified file content 
# -MaxFileSize = 100KB : The suffix can be KB, MB, GB. When the log file reaches this size, it will be automatically rolled. 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 table format) 
#6) org.apache.log4j.PatternLayout (can flexibly specify layout pattern) 
#7) org.apache.log4j.SimpleLayout (contains log information level and information string) 
#8) org. apache.log4j.TTCCLayout (contains log generation time, thread, category, etc.) 
#9)org.apache.log4j.xml.XMLLayout (layout in XML format) 

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

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

#9) XMLLayout option property 
# -LocationInfo = TRUE: default value 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. The printing parameters are as follows: 
# %m Output the message specified in the code 
# %p Output priority, namely DEBUG, INFO, WARN, ERROR, FATAL 
# %r The number of milliseconds it takes to output the log information from the application startup to outputting the log information 
# %c The category to which the output belongs, usually the full name of the class it belongs to 
# %t The thread name that generated the log event 
# %n Output a carriage return and line feed character, "\r\n" for Windows platform, "\n" for Unix platform 
# %d Output the date or time of the log time point, the default format is ISO8601, you can also specify the format after it 
# For example: %d{yyyy Year MM month dd day HH:mm:ss,SSS}, the output is similar: 2012-01-05 22:10:28,921 
# %l Output the location of the log event, including the category name, the thread that occurred, and the code The number of lines in 
# For example: Testlog.main(TestLog.java:10) 
# %F The name of the file where the log message is generated 
# %L The line number in the output code 
# %x Output the NDC (Nested Diagnostic Environment) associated with the current thread, such as java servlets multi-client multi-threaded applications 
# %% Output a "%" character 

# You can add modifiers between % and pattern characters character to control its minimum width, maximum width, and text alignment. 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 

#输出到控制台 
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 

#输出到文件 
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 

#按DatePattern输出到文件 
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 

#设定文件大小输出到文件 
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 

#用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 

#将日志登录到MySQL数据库 
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=325339358&siteId=291194637