Before using log4j is to take someone else to write good use, today carefully looked under how to configure. Http://blog.sina.com.cn/s/blog_5ed94d710101go3u.html
################################################################################
#① configures the root logger with the following syntax:
#
#log4j. Rootlogger = [Level],appendername,appendername2,...
#level是日志记录的优先级, divided into Off,trace,debug,info,warn,error,fatal,all
# #Log4j建议只使用四个级别, priority from low to High is debug,info,warn,error
#通过在这里定义的级别, you can control the switch to the appropriate level of log information in your application
#比如在这里定义了INFO级别, the log information for all debug levels in the application will not be printed
#appenderName就是指定日志信息输出到哪个地方. Multiple output purposes can be specified simultaneously
################################################################################
################################################################################
#② Configuration log information output destination Appender, whose syntax is:
#
#log4j. Appender.appendername = Fully.qualified.name.of.appender.class
#log4j. Appender.appenderName.optionN = Valuen
#
#Log4j提供的appender有以下几种:
#1) Org.apache.log4j.ConsoleAppender (output to console)
#2) Org.apache.log4j.FileAppender (output to file)
#3) Org.apache.log4j.DailyRollingFileAppender (generate one log file per 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)
#
#1) consoleappender Option Properties
#-threshold = DEBUG: Specifies the lowest level of output for log messages
#-immediateflush = true: The default value is true and all messages are immediately output
#-target = System.err: Default value System.out, output to console (Err is red, out is black)
#
#2) fileappender Option Properties
#-threshold = INFO: Specifies the lowest level of output for log messages
#-immediateflush = true: The default value is true and all messages are immediately output
#-file = C:\log4j.log: Specify message output to C:\log4j.log file
#-append = false: Default value True, appends a message to the specified file, FALSE refers to overwriting the specified file content
#-encoding = UTF-8: can specify file encoding format
#
#3) dailyrollingfileappender Option Properties
#-threshold = WARN: Specifies the lowest level of output for log messages
#-immediateflush = true: The default value is true and all messages are immediately output
#-file = C:\log4j.log: Specify message output to C:\log4j.log file
#-append = false: Default value True, appends a message to the specified file, FALSE refers to overwriting the specified file content
#-datepattern= '. YYYY-WW: Scrolls a file once a week, which results in a new file every week. You can also press with the following parameters:
# ‘.‘ YYYY-MM: Monthly
# ‘.‘ YYYY-WW: Weekly
# ‘.‘ YYYY-MM-DD: Every day
# ‘.‘ Yyyy-mm-dd-a: two times a day
# ‘.‘ YYYY-MM-DD-HH: Per hour
# ‘.‘ YYYY-MM-DD-HH-MM: Per minute
#-encoding = UTF-8: can specify file encoding format
#
#4) rollingfileappender Option Properties
#-threshold = ERROR: Specifies the lowest level of output for log messages
#-immediateflush = true: The default value is true and all messages are immediately output
#-file = c:/log4j.log: Specify message output to C:/log4j.log file
#-append = false: Default value True, appends a message to the specified file, FALSE refers to overwriting the specified file content
#-maxfilesize = 100KB: suffix can be kb,mb,gb. When the log file reaches this size, it scrolls automatically. such as: Log4j.log.1
#-maxbackupindex = 2: Specifies the maximum number of scroll files that can be produced
#-encoding = UTF-8: can specify file encoding format
################################################################################
################################################################################
#③ the format (layout) of the configuration log information, whose syntax is:
#
#log4j. appender.appenderName.layout = Fully.qualified.name.of.layout.class
#log4j. Appender.appenderName.layout.optionN = Valuen
#
#Log4j提供的layout有以下几种:
#5) org.apache.log4j.HTMLLayout (layout in HTML form)
#6) Org.apache.log4j.PatternLayout (flexibility to specify layout mode)
#7) Org.apache.log4j.SimpleLayout (contains the level and information string for log information)
#8) Org.apache.log4j.TTCCLayout (contains information such as the time, thread, category, etc.) of the log generation
#9) org.apache.log4j.xml.XMLLayout (layout in XML format)
#
#5) htmllayout Option Properties
#-locationinfo = TRUE: Default value False, output Java file name and line number
#-title=struts Log Message: Default value log4j log Messages
#
#6) patternlayout Option Properties
#-conversionpattern =%m%n: Format the specified message (parameter meaning below)
#
#9) xmllayout Option Properties
#-locationinfo = TRUE: Default value False, output Java file name and line number
#
#Log4J采用类似C语言中的printf函数的打印格式格式化日志信息, the printing parameters are 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 line break, Windows platform is "\ r \ n", Unix platform is "\ n"
#%d The date or time of the output log time, the default format is ISO8601, or the format can be specified later
# such as:%d{yyyy mm month DD Day HH:MM:SS,SSS}, output similar to: January 05, 2012 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
# such as: Testlog.main (TESTLOG.JAVA:10)
#%F The name of the file where the output log message was generated
#%l line numbers in the output code
#%x output is associated with the current line threads NDC (nested diagnostic environment), like Java servlets Multi-client multithreaded application
# percent output a "%" character
#
# you can add modifiers between% and pattern characters to control their minimum width, maximum width, and text alignment. Such as:
#%5c: Output category name, minimum width is 5,category<5, right justified by default
#%-5c: Output category name, minimum width is 5,category<5, "-" number specifies left alignment, there will be spaces
#%.5c: Output category name, the maximum width is 5,category>5, will be the left more than the character cut off, <5 no space
#%20.30c:category name <20 fill in the blanks, and right-aligned, >30 characters, from the left hand to sell the characters exported from the cut off
################################################################################
################################################################################
#④ Specifies the output-specific level of 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 ')
Configuration of the log4j