The use of log4j logging is very convenient, it provides two types of log configuration, log4j.propertes and Log4j.xml, this document first posted Log4j.properties configuration method
1. Brief analysis of log4j.properties file configuration
The following gives the log4j.properties file commonly used in some information, in the use of only a copy of the section and the note can be released, if there is a question thank you for a message.
# #logger是进行记录的主要类, Appender is the way to record, layout is the format of the record #Logger-log writer, for the programmer to output log information #Appender-log destinations, the formatted log information output to the designated place to go Consoleappender-The destination is the console's Appender #FileAppender-the destination is the appender of the file #RollingFileAppender-Appender of the file with the destination size limited #Layout -The log formatter, which is used to format the programmer's logging request as a string #PatternLayout-format the layou of logging request with the specified pattern #Log4j提供的appender有以下几种: # O Rg.apache.log4j.ConsoleAppender (console), # Org.apache.log4j.FileAppender (file), # Org.apache.log4j.DailyRollingFileAppender (Generate a log file every day), # Org.apache.log4j.RollingFileAppender (a new file is generated when the file size reaches the specified size), # Org.apache.log4j.WriterAppender (sending log information to any specified place in streaming format) #Log4j提供的layout有以下几种: # Org.apache.log4j.HTMLLayout (layout in HTML table), # org.apache.log4j.PatternLayout (you can specify layout patterns flexibly), # Org.apache.log4j.SimpleLayout (The level and information string that contains the log information), # Org.apache.log4j.TTCCLayout (contains information about the time, thread, category, and so on that the log was generated) LOG4J uses a print format similar to the printf function in c to format the log information and print the parameters as #%m the message specified in the output code #%M Output Log Method name #%p output priority, that is, Debug,info,warn,error,fatal # %r output the number of milliseconds it takes to export the log information from the application to output #%c output is a class, usuallyis the full name of the class #%t output produces the log event's thread name #%n output a carriage return line feed. Windows platform is "RN", UNIX platform is "n" #%d output log point of date or time, the default format is ISO8601, you can also specify the format later, such as:%d{y YYY MMM DD hh:mm:ss,sss}, output similar to: October 18, 2002 22:10:28,921 #%l The location of the output log event, including the class name, the thread that occurred, and the number of lines in the code. Example: Testlog4.main (testlog4.java:10) #%l where the output log takes place #%F output class name ##################################################### ################ #设置级别和目的地-Prints log information for debug logs to the STDOUT and sys,query three destinations Log4j.rootlogger=debug,stdout # STDOUT: Destinations- Print to Screen # Org.apache.log4j.ConsoleAppender: Console Log4j.appender.stdout=org.apache.log4j.consoleappender # # Org.apache.log4j.PatternLayout: Flexibility to specify layout patterns Log4j.appender.stdout.layout=org.apache.log4j.patternlayout # #
The previous sentence sets the Patternlayout flexible specify the format, you specify the print parameters [%-5p][%d{hh:mm:ss}][%c-%m]%m%n [%-5p][%d{hh:mm:ss}][%l]%m%n Log4j.appender.stdout.layout.conversionpattern=[%-5p][%d{yyyy-mm-dd HH:mm:ss sss}][%t][%c-%m][%l] (%F:%L)-%m%n # QUERY: Destination--Output to file (qualify per file size) # # All info, warn, error, fatal level of data will be output to the Query.log log file # #log4j. logger.query=info,querY Log4j.logger.query=info # #输出到文件 (this defaults to append), using Org.apache.log4j.FileAppender: The log will be appended in a file log4j.appender.query= Org.apache.log4j.RollingFileAppender # #设置文件输出路径 html:log/query.html Log4j.appender.query.file=log/query.log # To set the file output style; HTML format: Org.apache.log4j.HTMLLayout log4j.appender.query.layout=org.apache.log4j.patternlayout # # The previous sentence sets the Patternlayout flexible specify the format, you specify the print parameters [%-5p][%d{hh:mm:ss}][%l]%m%n log4j.appender.query.layout.conversionpattern=[%
-5p][%d{yyyy-mm-dd Hh:mm:ss}][%c-%m]%m%n # To specify the maximum size of a file log4j.appender.query.maxfilesize=2048kb # # The number of logs that can be backed up LOG4J.APPENDER.QUERY.MAXBACKUPINDEX=100 # SYS: Destination-Output to file (generate a file every day) # # All error, fatal level of data will be here to perform output to the Sys.log log file #l Og4j.logger.sys=error,sys Log4j.logger.sys=error # # Org.apache.log4j.RollingFileAppender: Generate a log file every day # Using Org.apache.log4j.FileAppender: The log will append Log4j.appender.sys=org.apache.log4j.dailyrollingfileappender # # in a file
Set file output path ${user.home}/log/sys.log log4j.appender.sys.file=log/sys.log # # Org.apache.log4j.PatternLayout: Flexibility to specify layout patterns Log4j.apPender.
Sys.layout=org.apache.log4j.patternlayout # # The last sentence set the Patternlayout flexible specify the format, you specify the print parameters [%-5p][%d{hh:mm:ss}][%l]%m%n Log4j.appender.sys.layout.conversionpattern=[%-5p][%d{hh:mm:ss}][%c-%m]%m%n #设置特定包的级别 # # The log content under the Com.swh.weixin package is displayed with debug, and destination # # to export the log level of the Com.swh.weixin.util packet to debug information to the pack destination #
Log4j.logger.com.swh.weixin.util=debug,pack # #输出到文件 (this defaults to append), use Org.apache.log4j.FileAppender: Log will append in a file
Log4j.appender.pack=org.apache.log4j.rollingfileappender # #设置文件输出路径 or ${user.home}/log/pack.log Log4j.appender.pack.file=log/pack.log # #设置文件输出样式 Log4j.appender.pack.layout=org.apache.log4j.patternlayout # # The previous sentence sets the Patternlayout flexible specify the format, you specify the print parameters [%-5p][%d{hh:mm:ss}][%l]%m%n log4j.appender.pack.layout.conversionpattern=[%-
5P][%D{YYYY MM DD Hh:mm:ss}][%c-%m]%m%n # Specifies the maximum size of the file log4j.appender.pack.maxfilesize=1024kb #日志最大备份数目
log4j.appender.pack.maxbackupindex=100 ######################################################################## # #设置级别和目的地 #log4j, Rootlogger=debug,appender1,appender2 ##只设置特定包的级别和目的地 #log4j. Logger.com.coderdream=debug,appender1 #log4j. Logger.com.coderdream.dao=info,appender1, Appender2 # #输出到控制台 #log4j. Appender.appender1=org.apache.log4j.consoleappender # #设置输出样式 # Log4j.appender.appender1.layout=org.apache.log4j.patternlayout # #自定义样式 # #%r Time 0 # #%t method name Main # #%p priority Debug/info/erro R # #%c belongs to the full name of the class (including the package name) # #%l The location of the occurrence of a line in a class, # #%M output code, such as message # #%n output a newline symbol #log4j. Appender.appender1. Layout. Conversionpattern=[%d{yy/mm/dd hh:mm:ss:sss}][%c-%m]%m%n # #输出到文件 (this defaults to append) #log4j. appender.appender2= Org.apache.log4j.FileAppender # #设置文件输出路径 # # 1 text file #log4j. Appender.appender2.file=c:/log4jcrm_dao.log # #设置文件输出样式 # Log4j.appender.appender2.layout=org.apache.log4j.patternlayout # Log4j.appender.appender2.layout.conversionpattern=[%d{hh:mm:ss:sss}][%c-%m]-%m%n # #把日志文件写入数据库 ##################
####### #日志输出到远程数据库 ######################################## # #把日志文件写入数据库 # #记录的日志级别 Log4j.logger.db=info # #日志输出到数据库 Log4j.appender.db = Org.apache.log4j.jdbc. Jdbcappender # #缓存 log4j.appender.db.BufferSize = 0 # #数据库驱动 log4j.appender.db.Driver = com.mysql.jdbc.Driver # #数据url地址, Local can be abbreviated: jdbc:mysql:///test Log4j.appender.db.URL = jdbc:mysql://localhost:3306/swh_hibernate4?useunicode=true&
Characterencoding=utf8 # #数据库用户名 Log4j.appender.db.User = root # #数据库密码 Log4j.appender.db.Password = root # #日志布局模式
Log4j.appender.db.layout = org.apache.log4j.PatternLayout # #日志插入数据库中, t_logs table fields can be customized
Log4j.appender.db.layout.ConversionPattern = INSERT into T_logs (createdate, Thread, priority, category,<br/> methodname, message) values ('%d ', '%t ', '%-5p ', '%c ', '%m ', ' [%l]-%m ')
The next article will post a detailed configuration of the Log4j.xml http://blog.csdn.net/sanfye/article/details/50392073