- 浏览: 126411 次
- 来自: ...
最近访客 更多访客>>
文章分类
最新评论
-
dwangel:
给messageSource设置属性
<property ...
Spring i18n的better practice(相对于appfuse) -
dwangel:
spring 的message tag有一个属性text,可以 ...
Spring i18n的better practice(相对于appfuse) -
sn201:
awk高级篇
有问题啊!有问题!有问题!有问题!有问题!
i ...
awk文本处理总结(入门,中级,高级) -
happy_javaboy:
...
Log4j日志管理系统简单使用说明
在项目的开发过程中,系统对日志的要求很细,而且要求分类清楚。所以还是采用了Log4J。
在强调可重用组件开发的今天,除了自己从头到尾开发一个可重用的日志操作类外,Apache为我们提供了一个强有力的日志操作包-Log4j。
Log4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台、文件、GUI组件、甚至是套接口服务器、NT的事件记录器、UNIX Syslog守护进程等;我们也可以控制每一条日志的输出格式;通过定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程。最令人感兴趣的就是,这些可以通过一个配置文件来灵活地进行配置,而不需要修改应用的代码。
此外,通过Log4j其他语言接口,您可以在C、php、C++、.Net、PL/SQL程序中使用Log4j,其语法和用法与在Java程序中一样,使得多语言分布式系统得到一个统一一致的日志组件模块。而且,通过使用各种第三方扩展,您可以很方便地将Log4j集成到J2EE、JINI甚至是SNMP应用中。
Log4j配置文件详细说明(*.properties和*.xml)
属性文件Properties
properties属性文件
编号 配置项 配置项描述 示例
1 log4j.threshold 阈值项 log4j.threshold = error
2 log4j.rootLogger 根日志属性项 log4j.rootLogger = info,stdout1,stdout2
3 log4j.category. 子日志属性项(旧) log4j.category.com.eos = NULL,stdout1
4 log4j.logger. 子日志属性项(新) log4j.logger.com.eos.log = debug,stdout2
5 log4j.additivity. appender是否继承设置 log4j.additivity.com.eos = false
6 log4j.appender. 输出目的地定义项 log4j.appender.stdout2 = org.apache.log4j.ConsoleAppender
7 log4j.appender.A.layout 输出格式定义项 log4j.appender.stdout2.layout = org.apache.log4j.PatternLayout
xml文件
编号 配置项 配置项描述 示例
1 threshold 阈值项 <configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false" threshold="null"><br> 2 root 根日志属性项 <root><br> <priority value="info"></priority><br> <appender-ref ref="CONSOLE"></appender-ref><br> </root><br> 3 priority 级别项(旧) <priority value="info"></priority><br> 4 level 级别项(新) <level value="info"></level><br> 5 category 子日志属性项(旧) <category name="com.eos.log" additivity="true"><br> 6 logger 子日志属性项(新) <logger name="com.eos" additivity="true"><br> 7 appender-ref 输出端控制项 <appender-ref ref="CONSOLE"></appender-ref><br> 8 additivity appender是否继承设置 <logger name="com.eos" additivity="true"><br> 9 appender 输出目的地定义项 <appender class="org.apache.log4j.ConsoleAppender" name="CONSOLE"><br> 10 layout 输出格式定义项 <layout class="org.apache.log4j.PatternLayout"><br><br><strong>详细说明(只针对Log4j常用的,用户可以自定义)Appender</strong><br> Appender继承关系<br> Appender基本种类<br> org.apache.log4j.ConsoleAppender(控制台)<br> org.apache.log4j.FileAppender(文件)<br> org.apache.log4j.DailyRollingFileAppender(每天产生一个日志文件)<br> org.apache.log4j.RollingFileAppender(文件大小到达指定尺寸的时候产生一个新的文件)<br> org.apache.log4j.WriterAppender(将日志信息以流格式发送到任意指定的地方)<br> · ConsoleAppender选项<br> Threshold=WARN:指定日志消息的输出最低层次。<br> ImmediateFlush=true:默认值是true,意谓着所有的消息都会被立即输出。<br> Target=System.err:默认情况下是:System.out,指定输出控制台<br> · FileAppender 选项<br> Threshold=WARN:指定日志消息的输出最低层次。<br> ImmediateFlush=true:默认值是true,意谓着所有的消息都会被立即输出。<br> File=mylog.txt:指定消息输出到mylog.txt文件。<br> Append=false:默认值是true,即将消息增加到指定文件中,false指将消息覆盖指定的文件内容。<br> · DailyRollingFileAppender 选项<br> Threshold=WARN:指定日志消息的输出最低层次。<br> ImmediateFlush=true:默认值是true,意谓着所有的消息都会被立即输出。<br> File=mylog.txt:指定消息输出到mylog.txt文件。<br> Append=false:默认值是true,即将消息增加到指定文件中,false指将消息覆盖指定的文件内容。<br> DatePattern='.'yyyy-ww:每周滚动一次文件,即每周产生一个新的文件。当然也可以指定按月、周、<br> 天、时和分。即对应的格式如下:<br> 1)'.'yyyy-MM: 每月<br> 2)'.'yyyy-ww: 每周<br> 3)'.'yyyy-MM-dd: 每天<br> 4)'.'yyyy-MM-dd-a: 每天两次<br> 5)'.'yyyy-MM-dd-HH: 每小时<br> 6)'.'yyyy-MM-dd-HH-mm: 每分钟<br> n RollingFileAppender 选项<br> Threshold=WARN:指定日志消息的输出最低层次。<br> ImmediateFlush=true:默认值是true,意谓着所有的消息都会被立即输出。<br> File=mylog.txt:指定消息输出到mylog.txt文件。<br> Append=false:默认值是true,即将消息增加到指定文件中,false指将消息覆盖指定的文件内容。<br> MaxFileSize=100KB: 后缀可以是KB, MB 或者是 GB. 在日志文件到达该大小时,将会自动滚动,即将原来<br> 的内容移到mylog.log.1文件。<br> MaxBackupIndex=2:指定可以产生的滚动文件的最大数。 <br><br><strong>详细说明(只针对Log4j,用户可以自定义)Layout</strong><br> Log4j的Layout基本种类<br> org.apache.log4j.HTMLLayout(以HTML表格形式布局),<br> org.apache.log4j.PatternLayout(可以灵活地指定布局模式),<br> org.apache.log4j.SimpleLayout(包含日志信息的级别和信息字符串),<br> org.apache.log4j.TTCCLayout(包含日志产生的时间、线程、类别等等信息)<br> · HTMLLayout选项<br> LocationInfo=true:默认值是false,输出java文件名称和行号<br> Title=my app file: 默认值是 Log4J Log Messages. <br> n PatternLayout 选项<br> log4j.appender.A1.layout.ConversionPattern=%-4r %-5p %d{yyyy-MM-dd HH:mm:ssS} %c %m%n<br> 这里需要说明的就是日志信息格式中几个符号所代表的含义:<br><br> %X: 信息输出时左对齐;<br> %p: 输出日志信息优先级,即DEBUG,INFO,WARN,ERROR,FATAL,<br> %d: 输出日志时间点的日期或时间,默认格式为ISO8601,也可以在其后指定格式,比如:%d{yyy MMM dd HH:mm:ss,SSS},输出类似:2002年10月18日 22:10:28,921<br> %r: 输出自应用启动到输出该log信息耗费的毫秒数<br> %c: 输出日志信息所属的类目,通常就是所在类的全名<br> %t: 输出产生该日志事件的线程名<br> %l: 输出日志事件的发生位置,相当于%C.%M(%F:%L)的组合,包括类目名、发生的线程,以及在代码中的行数。举例:Testlog4.main(TestLog4.java:10)<br> %x: 输出和当前线程相关联的NDC(嵌套诊断环境),尤其用到像java servlets这样的多客户多线程的应用中。<br> %%: 输出一个"%"字符<br> %F: 输出日志消息产生时所在的文件名称<br> %L: 输出代码中的行号<br> %m: 输出代码中指定的消息,产生的日志具体信息<br> %n: 输出一个回车换行符,Windows平台为"\r\n",Unix平台为"\n"输出日志信息换行,可以在%与模式字符之间加上修饰符来控制其最小宽度、最大宽度、和文本的对齐方式。如:<br> 1)%20c:指定输出category的名称,最小的宽度是20,如果category的名称小于20的话,默认的情况下右对齐。<br> 2)%-20c:指定输出category的名称,最小的宽度是20,如果category的名称小于20的话,"-"号指定左对齐。<br> 3)%.30c:指定输出category的名称,最大的宽度是30,如果category的名称大于30的话,就会将左边多出的字符截掉,但小于30的话也不会有空格。<br> 4)%20.30c:如果category的名称小于20就补空格,并且右对齐,如果其名称长于30字符, 就从左边交远销出的字符截掉。<br> · XMLLayout 选项<br> LocationInfo=true:默认值是false,输出java文件和行号<br><br><strong>日志配置文件内容范例</strong> <br> log4j.properties<br><br><div class="dp-highlighter"> <div class="bar"> <div class="tools"> <a onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;" href="http://www.icnote.com/#">view plain</a><a onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;" href="http://www.icnote.com/#">copy to clipboard</a><a onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;" href="http://www.icnote.com/#">print</a><a onclick="dp.sh.Toolbar.Command('About',this);return false;" href="http://www.icnote.com/#">?</a> </div> </div> <ol class="dp-j"> <li class="alt"> <span><span>log4j.rootLogger=DEBUG, CONSOLE </span></span> </li> <li class=""> <span>#DEBUG, CONSOLE,FILE,ROLLING_FILE,MAIL,DATABASE </span> </li> <li class="alt"> <span style="background-color: yellow">#log4j.threshold=WARN</span> </li> <li class="alt"> </li> <li class=""> <span style="background-color: yellow">log4j.logger.org.apache=INFO, FILE </span> </li> <li class="alt"> <span><span style="background-color: yellow">log4j.additivity.org.apache=<span class="keyword">false</span></span></span><span> </span> </li> <li class=""> <span> </span> </li> <li class="alt"> <span>################### </span> </li> <li class=""> <span># Console Appender </span> </li> <li class="alt"> <span>################### </span> </li> <li class=""> <span>log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender </span> </li> <li class="alt"> <span><span style="background-color: yellow">log4j.appender.Threshold=DEBUG </span> </span> </li> <li class=""> <span>log4j.appender.CONSOLE.Target=System.out </span> </li> <li class="alt"> <span>log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout </span> </li> <li class=""> <span>log4j.appender.CONSOLE.layout.ConversionPattern=%d [%p] - %m%n </span> </li> <li class="alt"> <span> </span> </li> <li class=""> <span> </span> </li> <li class="alt"> <span>##################### </span> </li> <li class=""> <span># File Appender </span> </li> <li class="alt"> <span>##################### </span> </li> <li class=""> <span>log4j.appender.FILE=org.apache.log4j.FileAppender </span> </li> <li class="alt"> <span>log4j.appender.FILE.File=file.log </span> </li> <li class=""> <span>log4j.appender.FILE.Append=</span><span class="keyword">false</span><span> </span> </li> <li class="alt"> <span>log4j.appender.FILE.layout=org.apache.log4j.PatternLayout </span> </li> <li class=""> <span>log4j.appender.FILE.layout.ConversionPattern=%d [%p] - %m%n </span> </li> <li class="alt"> <span> </span> </li> <li class=""> <span> </span> </li> <li class="alt"> <span>######################## </span> </li> <li class=""> <span># Rolling File </span> </li> <li class="alt"> <span>######################## </span> </li> <li class=""> <span>log4j.appender.ROLLING_FILE=org.apache.log4j.RollingFileAppender </span> </li> <li class="alt"> <span>log4j.appender.ROLLING_FILE.Threshold=ERROR </span> </li> <li class=""> <span>log4j.appender.ROLLING_FILE.File=rolling.log </span> </li> <li class="alt"> <span>log4j.appender.ROLLING_FILE.Append=</span><span class="keyword">true</span><span> </span> </li> <li class=""> <span>log4j.appender.ROLLING_FILE.MaxFileSize=10KB </span> </li> <li class="alt"> <span>log4j.appender.ROLLING_FILE.MaxBackupIndex=</span><span class="number">1</span><span> </span> </li> <li class=""> <span>log4j.appender.ROLLING_FILE.layout=org.apache.log4j.PatternLayout </span> </li> <li class="alt"> <span>log4j.appender.ROLLING_FILE.layout.ConversionPattern=%d [%p] - %m%n </span> </li> <li class=""> <span> </span> </li> <li class="alt"> <span> </span> </li> <li class=""> <span>#################### </span> </li> <li class="alt"> <span># Socket Appender </span> </li> <li class=""> <span>#################### </span> </li> <li class="alt"> <span>log4j.appender.SOCKET=org.apache.log4j.RollingFileAppender </span> </li> <li class=""> <span>log4j.appender.SOCKET.RemoteHost=localhost </span> </li> <li class="alt"> <span>log4j.appender.SOCKET.Port=</span><span class="number">5001</span><span> </span> </li> <li class=""> <span>log4j.appender.SOCKET.LocationInfo=</span><span class="keyword">true</span><span> </span> </li> <li class="alt"> <span># Set up </span><span class="keyword">for</span><span> Log Facter </span><span class="number">5</span><span> </span> </li> <li class=""> <span>log4j.appender.SOCKET.layout=org.apache.log4j.PatternLayout </span> </li> <li class="alt"> <span>log4j.appender.SOCET.layout.ConversionPattern=%d [%p] - %m%n </span> </li> <li class=""> <span> </span> </li> <li class="alt"> <span> </span> </li> <li class=""> <span>######################## </span> </li> <li class="alt"> <span># Log Factor </span><span class="number">5</span><span> Appender </span> </li> <li class=""> <span>######################## </span> </li> <li class="alt"> <span>log4j.appender.LF5_APPENDER=org.apache.log4j.lf5.LF5Appender </span> </li> <li class=""> <span>log4j.appender.LF5_APPENDER.MaxNumberOfRecords=</span><span class="number">2000</span><span> </span> </li> <li class="alt"> <span> </span> </li> <li class=""> <span> </span> </li> <li class="alt"> <span>######################## </span> </li> <li class=""> <span># SMTP Appender </span> </li> <li class="alt"> <span>####################### </span> </li> <li class=""> <span>log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender </span> </li> <li class="alt"> <span>log4j.appender.MAIL.Threshold=FATAL </span> </li> <li class=""> <span>log4j.appender.MAIL.BufferSize=</span><span class="number">10</span><span> </span> </li> <li class="alt"> <span>log4j.appender.MAIL.From=wuyh</span><span class="annotation">@primeton</span><span>.com </span> </li> <li class=""> <span>log4j.appender.MAIL.SMTPHost=mail.primeton.com </span> </li> <li class="alt"> <span>log4j.appender.MAIL.Subject=Log4J Message </span> </li> <li class=""> <span>log4j.appender.MAIL.To=test</span><span class="annotation">@primeton</span><span>.com </span> </li> <li class="alt"> <span>log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout </span> </li> <li class=""> <span>log4j.appender.MAIL.layout.ConversionPattern=%d [%p] - %m%n </span> </li> <li class="alt"> <span> </span> </li> <li class=""> <span> </span> </li> <li class="alt"> <span>######################## </span> </li> <li class=""> <span># JDBC Appender </span> </li> <li class="alt"> <span>####################### </span> </li> <li class=""> <span>log4j.appender.DATABASE=org.apache.log4j.jdbc.JDBCAppender </span> </li> <li class="alt"> <span>log4j.appender.DATABASE.URL=jdbc:mysql:</span><span class="comment">//localhost:3306/test </span><span> </span> </li> <li class=""> <span>log4j.appender.DATABASE.driver=com.mysql.jdbc.Driver </span> </li> <li class="alt"> <span>log4j.appender.DATABASE.user=root </span> </li> <li class=""> <span>log4j.appender.DATABASE.password= </span> </li> <li class="alt"> <span>log4j.appender.DATABASE.sql=INSERT INTO LOG4J (Message) VALUES (%d [%p] - %m%n) </span> </li> <li class=""> <span>log4j.appender.DATABASE.layout=org.apache.log4j.PatternLayout </span> </li> <li class="alt"> <span>log4j.appender.DATABASE.layout.ConversionPattern=%d [%p] - %m%n </span> </li> <li class=""> <span> </span> </li> <li class="alt"> <span> </span> </li> <li class=""> <span>log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender </span> </li> <li class="alt"> <span>log4j.appender.A1.File=SampleMessages.log4j </span> </li> <li class=""> <span>log4j.appender.A1.DatePattern=yyyyMMdd-HH</span><span class="string">'.log4j'</span><span> </span> </li> <li class="alt"> <span>log4j.appender.A1.layout=org.apache.log4j.xml.XMLLayout </span> </li> <li class=""> <span> </span> </li> <li class="alt"> <span>################### </span> </li> <li class=""> <span>#自定义Appender </span> </li> <li class="alt"> <span>################### </span> </li> <li class=""> <span>log4j.appender.im = net.cybercorlin.util.logger.appender.IMAppender </span> </li> <li class="alt"> <span> </span> </li> <li class=""> <span>log4j.appender.im.host = mail.cybercorlin.net </span> </li> <li class="alt"> <span>log4j.appender.im.username = username </span> </li> <li class=""> <span>log4j.appender.im.password = password </span> </li> <li class="alt"> <span>log4j.appender.im.recipient = wuyh</span><span class="annotation">@primeton</span><span>.com </span> </li> <li class=""> <span> </span> </li> <li class="alt"> <span>log4j.appender.im.layout=org.apache.log4j.PatternLayout </span> </li> <li class=""> <span>log4j.appender.im.layout.ConversionPattern =%d [%p] - %m%n </span> </li> </ol> </div> <textarea class="java" style="display: none" name="code" rows="15" cols="100">log4j.rootLogger=DEBUG, CONSOLE #DEBUG, CONSOLE,FILE,ROLLING_FILE,MAIL,DATABASE log4j.logger.org.apache=INFO, FILE log4j.additivity.org.apache=false ################### # 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=%d [%p] - %m%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=%d [%p] - %m%n ######################## # 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=%d [%p] - %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=%d [%p] - %m%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 log4j.appender.MAIL.From=wuyh@primeton.com log4j.appender.MAIL.SMTPHost=mail.primeton.com log4j.appender.MAIL.Subject=Log4J Message log4j.appender.MAIL.To=test@primeton.com log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout log4j.appender.MAIL.layout.ConversionPattern=%d [%p] - %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 (%d [%p] - %m%n) log4j.appender.DATABASE.layout=org.apache.log4j.PatternLayout log4j.appender.DATABASE.layout.ConversionPattern=%d [%p] - %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 = wuyh@primeton.com log4j.appender.im.layout=org.apache.log4j.PatternLayout log4j.appender.im.layout.ConversionPattern =%d [%p] - %m%n </textarea><br><br> #注意:在属性配置文件中,属性值的第一个一定是级别,输出端可有可无,以逗号分割。(而xml文件格式没有这种限制)<br><br> log4j.xml<br><div class="dp-highlighter"> <div class="bar"> <div class="tools"> <a onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;" href="http://www.icnote.com/#">view plain</a><a onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;" href="http://www.icnote.com/#">copy to clipboard</a><a onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;" href="http://www.icnote.com/#">print</a><a onclick="dp.sh.Toolbar.Command('About',this);return false;" href="http://www.icnote.com/#">?</a> </div> </div> <ol class="dp-j"> <li class="alt"> <span><span>< xml version=</span><span class="string">"1.0"</span><span> encoding=</span><span class="string">"UTF-8"</span><span> > </span></span> </li> <li class=""> <span><!DOCTYPE log4j:configuration SYSTEM </span><span class="string">"log4j.dtd"</span><span>> </span> </li> <li class="alt"> <span><log4j:configuration xmlns:log4j=</span><span class="string">"http://jakarta.apache.org/log4j/"</span><span> debug=</span><span class="string">"false"</span><span> threshold=</span><span class="string">"null"</span><span>> </span> </li> <li class=""> <span> </span> </li> <li class="alt"> <span> </span> </li> <li class=""> <span><appender </span><span class="keyword">class</span><span>=</span><span class="string">"org.apache.log4j.ConsoleAppender"</span><span> name=</span><span class="string">"CONSOLE"</span><span>> </span> </li> <li class="alt"> <span><param name=</span><span class="string">"Target"</span><span> value=</span><span class="string">"System.out"</span><span>/> </span> </li> <li class=""> <span><param name=</span><span class="string">"Threshold"</span><span> value=</span><span class="string">"INFO"</span><span>/> </span> </li> <li class="alt"> <span><layout </span><span class="keyword">class</span><span>=</span><span class="string">"org.apache.log4j.PatternLayout"</span><span>> </span> </li> <li class=""> <span><param name=</span><span class="string">"ConversionPattern"</span><span> value=</span><span class="string">"%d [%p] - %m%n "</span><span>/> </span> </li> <li class="alt"> <span></layout> </span> </li> <li class=""> <span><filter </span><span class="keyword">class</span><span>=</span><span class="string">"org.apache.log4j.varia.DenyAllFilter"</span><span>/> </span> </li> <li class="alt"> <span><errorHandler </span><span class="keyword">class</span><span>=</span><span class="string">"org.apache.log4j.varia. FallbackErrorHandler"</span><span>/> </span> </li> <li class=""> <span></appender> </span> </li> <li class="alt"> <span> </span> </li> <li class=""> <span><appender </span><span class="keyword">class</span><span>=</span><span class="string">"org.apache.log4j.FileAppender"</span><span> name=</span><span class="string">"FILE"</span><span>> </span> </li> <li class="alt"> <span><param name=</span><span class="string">"File"</span><span> value=</span><span class="string">"file.log"</span><span>/> </span> </li> <li class=""> <span><param name=</span><span class="string">"Append"</span><span> value=</span><span class="string">"false"</span><span>/> </span> </li> <li class="alt"> <span><param name=</span><span class="string">"Threshold"</span><span> value=</span><span class="string">"INFO"</span><span>/> </span> </li> <li class=""> <span><layout </span><span class="keyword">class</span><span>=</span><span class="string">"org.apache.log4j.PatternLayout"</span><span>> </span> </li> <li class="alt"> <span><param name=</span><span class="string">"ConversionPattern"</span><span> value=</span><span class="string">"%d [%p] - %m%n "</span><span>/> </span> </li> <li class=""> <span></layout> </span> </li> <li class="alt"> <span></appender> </span> </li> <li class=""> <span> </span> </li> <li class="alt"> <span><appender </span><span class="keyword">class</span><span>=</span><span class="string">"org.apache.log4j.RollingFileAppender"</span><span> name=</span><span class="string">"ROLLING_FILE"</span><span>> </span> </li> <li class=""> <span><param name=</span><span class="string">"Threshold"</span><span> value=</span><span class="string">"INFO"</span><span>/> </span> </li> <li class="alt"> <span><param name=</span><span class="string">"File"</span><span> value=</span><span class="string">"rolling.log"</span><span>/> </span> </li> <li class=""> <span><param name=</span><span class="string">"Append"</span><span> value=</span><span class="string">"false"</span><span>/> </span> </li> <li class="alt"> <span><param name=</span><span class="string">"MaxFileSize"</span><span> value=</span><span class="string">"10KB"</span><span>/> </span> </li> <li class=""> <span><param name=</span><span class="string">"MaxBackupIndex"</span><span> value=</span><span class="string">"1"</span><span>/> </span> </li> <li class="alt"> <span><layout </span><span class="keyword">class</span><span>=</span><span class="string">"org.apache.log4j.PatternLayout"</span><span>> </span> </li> <li class=""> <span><param name=</span><span class="string">"ConversionPattern"</span><span> value=</span><span class="string">"%d [%p] - %m%n "</span><span>/> </span> </li> <li class="alt"> <span></layout> </span> </li> <li class=""> <span></appender> </span> </li> <li class="alt"> <span> </span> </li> <li class=""> <span><logger additivity=</span><span class="string">"false"</span><span> name=</span><span class="string">"com.eos"</span><span>> </span> </li> <li class="alt"> <span><level value=</span><span class="string">"info"</span><span>/> </span> </li> <li class=""> <span><appender-ref ref=</span><span class="string">"CONSOLE"</span><span>/> </span> </li> <li class="alt"> <span></logger> </span> </li> <li class=""> <span> </span> </li> <li class="alt"> <span><category additivity=</span><span class="string">"true"</span><span> name=</span><span class="string">"com.eos.log"</span><span>> </span> </li> <li class=""> <span><priority value=</span><span class="string">"warn"</span><span>/> </span> </li> <li class="alt"> <span></category> </span> </li> <li class=""> <span> </span> </li> <li class="alt"> <span><root> </span> </li> <li class=""> <span><priority value=</span><span class="string">"info"</span><span>/> </span> </li> <li class="alt"> <span><appender-ref ref=</span><span class="string">"CONSOLE"</span><span>/> </span> </li> <li class=""> <span></root> </span> </li> <li class="alt"> <span></log4j:configuration> </span> </li> </ol> </div> <textarea class="java" style="display: none" name="code" rows="15" cols="100">< xml version="1.0" encoding="UTF-8" > <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false" threshold="null"> <appender class="org.apache.log4j.ConsoleAppender" name="CONSOLE"> <param name="Target" value="System.out"/> <param name="Threshold" value="INFO"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d [%p] - %m%n "/> </layout> <filter class="org.apache.log4j.varia.DenyAllFilter"/> <errorHandler class="org.apache.log4j.varia. FallbackErrorHandler"/> </appender> <appender class="org.apache.log4j.FileAppender" name="FILE"> <param name="File" value="file.log"/> <param name="Append" value="false"/> <param name="Threshold" value="INFO"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d [%p] - %m%n "/> </layout> </appender> <appender class="org.apache.log4j.RollingFileAppender" name="ROLLING_FILE"> <param name="Threshold" value="INFO"/> <param name="File" value="rolling.log"/> <param name="Append" value="false"/> <param name="MaxFileSize" value="10KB"/> <param name="MaxBackupIndex" value="1"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d [%p] - %m%n "/> </layout> </appender> <logger additivity="false" name="com.eos"> <level value="info"/> <appender-ref ref="CONSOLE"/> </logger> <category additivity="true" name="com.eos.log"> <priority value="warn"/> </category> <root> <priority value="info"/> <appender-ref ref="CONSOLE"/> </root> </log4j:configuration> </textarea><br></layout></appender></logger></logger></category></configuration>
在强调可重用组件开发的今天,除了自己从头到尾开发一个可重用的日志操作类外,Apache为我们提供了一个强有力的日志操作包-Log4j。
Log4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台、文件、GUI组件、甚至是套接口服务器、NT的事件记录器、UNIX Syslog守护进程等;我们也可以控制每一条日志的输出格式;通过定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程。最令人感兴趣的就是,这些可以通过一个配置文件来灵活地进行配置,而不需要修改应用的代码。
此外,通过Log4j其他语言接口,您可以在C、php、C++、.Net、PL/SQL程序中使用Log4j,其语法和用法与在Java程序中一样,使得多语言分布式系统得到一个统一一致的日志组件模块。而且,通过使用各种第三方扩展,您可以很方便地将Log4j集成到J2EE、JINI甚至是SNMP应用中。
Log4j配置文件详细说明(*.properties和*.xml)
属性文件Properties
properties属性文件
编号 配置项 配置项描述 示例
1 log4j.threshold 阈值项 log4j.threshold = error
2 log4j.rootLogger 根日志属性项 log4j.rootLogger = info,stdout1,stdout2
3 log4j.category. 子日志属性项(旧) log4j.category.com.eos = NULL,stdout1
4 log4j.logger. 子日志属性项(新) log4j.logger.com.eos.log = debug,stdout2
5 log4j.additivity. appender是否继承设置 log4j.additivity.com.eos = false
6 log4j.appender. 输出目的地定义项 log4j.appender.stdout2 = org.apache.log4j.ConsoleAppender
7 log4j.appender.A.layout 输出格式定义项 log4j.appender.stdout2.layout = org.apache.log4j.PatternLayout
xml文件
编号 配置项 配置项描述 示例
1 threshold 阈值项 <configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false" threshold="null"><br> 2 root 根日志属性项 <root><br> <priority value="info"></priority><br> <appender-ref ref="CONSOLE"></appender-ref><br> </root><br> 3 priority 级别项(旧) <priority value="info"></priority><br> 4 level 级别项(新) <level value="info"></level><br> 5 category 子日志属性项(旧) <category name="com.eos.log" additivity="true"><br> 6 logger 子日志属性项(新) <logger name="com.eos" additivity="true"><br> 7 appender-ref 输出端控制项 <appender-ref ref="CONSOLE"></appender-ref><br> 8 additivity appender是否继承设置 <logger name="com.eos" additivity="true"><br> 9 appender 输出目的地定义项 <appender class="org.apache.log4j.ConsoleAppender" name="CONSOLE"><br> 10 layout 输出格式定义项 <layout class="org.apache.log4j.PatternLayout"><br><br><strong>详细说明(只针对Log4j常用的,用户可以自定义)Appender</strong><br> Appender继承关系<br> Appender基本种类<br> org.apache.log4j.ConsoleAppender(控制台)<br> org.apache.log4j.FileAppender(文件)<br> org.apache.log4j.DailyRollingFileAppender(每天产生一个日志文件)<br> org.apache.log4j.RollingFileAppender(文件大小到达指定尺寸的时候产生一个新的文件)<br> org.apache.log4j.WriterAppender(将日志信息以流格式发送到任意指定的地方)<br> · ConsoleAppender选项<br> Threshold=WARN:指定日志消息的输出最低层次。<br> ImmediateFlush=true:默认值是true,意谓着所有的消息都会被立即输出。<br> Target=System.err:默认情况下是:System.out,指定输出控制台<br> · FileAppender 选项<br> Threshold=WARN:指定日志消息的输出最低层次。<br> ImmediateFlush=true:默认值是true,意谓着所有的消息都会被立即输出。<br> File=mylog.txt:指定消息输出到mylog.txt文件。<br> Append=false:默认值是true,即将消息增加到指定文件中,false指将消息覆盖指定的文件内容。<br> · DailyRollingFileAppender 选项<br> Threshold=WARN:指定日志消息的输出最低层次。<br> ImmediateFlush=true:默认值是true,意谓着所有的消息都会被立即输出。<br> File=mylog.txt:指定消息输出到mylog.txt文件。<br> Append=false:默认值是true,即将消息增加到指定文件中,false指将消息覆盖指定的文件内容。<br> DatePattern='.'yyyy-ww:每周滚动一次文件,即每周产生一个新的文件。当然也可以指定按月、周、<br> 天、时和分。即对应的格式如下:<br> 1)'.'yyyy-MM: 每月<br> 2)'.'yyyy-ww: 每周<br> 3)'.'yyyy-MM-dd: 每天<br> 4)'.'yyyy-MM-dd-a: 每天两次<br> 5)'.'yyyy-MM-dd-HH: 每小时<br> 6)'.'yyyy-MM-dd-HH-mm: 每分钟<br> n RollingFileAppender 选项<br> Threshold=WARN:指定日志消息的输出最低层次。<br> ImmediateFlush=true:默认值是true,意谓着所有的消息都会被立即输出。<br> File=mylog.txt:指定消息输出到mylog.txt文件。<br> Append=false:默认值是true,即将消息增加到指定文件中,false指将消息覆盖指定的文件内容。<br> MaxFileSize=100KB: 后缀可以是KB, MB 或者是 GB. 在日志文件到达该大小时,将会自动滚动,即将原来<br> 的内容移到mylog.log.1文件。<br> MaxBackupIndex=2:指定可以产生的滚动文件的最大数。 <br><br><strong>详细说明(只针对Log4j,用户可以自定义)Layout</strong><br> Log4j的Layout基本种类<br> org.apache.log4j.HTMLLayout(以HTML表格形式布局),<br> org.apache.log4j.PatternLayout(可以灵活地指定布局模式),<br> org.apache.log4j.SimpleLayout(包含日志信息的级别和信息字符串),<br> org.apache.log4j.TTCCLayout(包含日志产生的时间、线程、类别等等信息)<br> · HTMLLayout选项<br> LocationInfo=true:默认值是false,输出java文件名称和行号<br> Title=my app file: 默认值是 Log4J Log Messages. <br> n PatternLayout 选项<br> log4j.appender.A1.layout.ConversionPattern=%-4r %-5p %d{yyyy-MM-dd HH:mm:ssS} %c %m%n<br> 这里需要说明的就是日志信息格式中几个符号所代表的含义:<br><br> %X: 信息输出时左对齐;<br> %p: 输出日志信息优先级,即DEBUG,INFO,WARN,ERROR,FATAL,<br> %d: 输出日志时间点的日期或时间,默认格式为ISO8601,也可以在其后指定格式,比如:%d{yyy MMM dd HH:mm:ss,SSS},输出类似:2002年10月18日 22:10:28,921<br> %r: 输出自应用启动到输出该log信息耗费的毫秒数<br> %c: 输出日志信息所属的类目,通常就是所在类的全名<br> %t: 输出产生该日志事件的线程名<br> %l: 输出日志事件的发生位置,相当于%C.%M(%F:%L)的组合,包括类目名、发生的线程,以及在代码中的行数。举例:Testlog4.main(TestLog4.java:10)<br> %x: 输出和当前线程相关联的NDC(嵌套诊断环境),尤其用到像java servlets这样的多客户多线程的应用中。<br> %%: 输出一个"%"字符<br> %F: 输出日志消息产生时所在的文件名称<br> %L: 输出代码中的行号<br> %m: 输出代码中指定的消息,产生的日志具体信息<br> %n: 输出一个回车换行符,Windows平台为"\r\n",Unix平台为"\n"输出日志信息换行,可以在%与模式字符之间加上修饰符来控制其最小宽度、最大宽度、和文本的对齐方式。如:<br> 1)%20c:指定输出category的名称,最小的宽度是20,如果category的名称小于20的话,默认的情况下右对齐。<br> 2)%-20c:指定输出category的名称,最小的宽度是20,如果category的名称小于20的话,"-"号指定左对齐。<br> 3)%.30c:指定输出category的名称,最大的宽度是30,如果category的名称大于30的话,就会将左边多出的字符截掉,但小于30的话也不会有空格。<br> 4)%20.30c:如果category的名称小于20就补空格,并且右对齐,如果其名称长于30字符, 就从左边交远销出的字符截掉。<br> · XMLLayout 选项<br> LocationInfo=true:默认值是false,输出java文件和行号<br><br><strong>日志配置文件内容范例</strong> <br> log4j.properties<br><br><div class="dp-highlighter"> <div class="bar"> <div class="tools"> <a onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;" href="http://www.icnote.com/#">view plain</a><a onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;" href="http://www.icnote.com/#">copy to clipboard</a><a onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;" href="http://www.icnote.com/#">print</a><a onclick="dp.sh.Toolbar.Command('About',this);return false;" href="http://www.icnote.com/#">?</a> </div> </div> <ol class="dp-j"> <li class="alt"> <span><span>log4j.rootLogger=DEBUG, CONSOLE </span></span> </li> <li class=""> <span>#DEBUG, CONSOLE,FILE,ROLLING_FILE,MAIL,DATABASE </span> </li> <li class="alt"> <span style="background-color: yellow">#log4j.threshold=WARN</span> </li> <li class="alt"> </li> <li class=""> <span style="background-color: yellow">log4j.logger.org.apache=INFO, FILE </span> </li> <li class="alt"> <span><span style="background-color: yellow">log4j.additivity.org.apache=<span class="keyword">false</span></span></span><span> </span> </li> <li class=""> <span> </span> </li> <li class="alt"> <span>################### </span> </li> <li class=""> <span># Console Appender </span> </li> <li class="alt"> <span>################### </span> </li> <li class=""> <span>log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender </span> </li> <li class="alt"> <span><span style="background-color: yellow">log4j.appender.Threshold=DEBUG </span> </span> </li> <li class=""> <span>log4j.appender.CONSOLE.Target=System.out </span> </li> <li class="alt"> <span>log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout </span> </li> <li class=""> <span>log4j.appender.CONSOLE.layout.ConversionPattern=%d [%p] - %m%n </span> </li> <li class="alt"> <span> </span> </li> <li class=""> <span> </span> </li> <li class="alt"> <span>##################### </span> </li> <li class=""> <span># File Appender </span> </li> <li class="alt"> <span>##################### </span> </li> <li class=""> <span>log4j.appender.FILE=org.apache.log4j.FileAppender </span> </li> <li class="alt"> <span>log4j.appender.FILE.File=file.log </span> </li> <li class=""> <span>log4j.appender.FILE.Append=</span><span class="keyword">false</span><span> </span> </li> <li class="alt"> <span>log4j.appender.FILE.layout=org.apache.log4j.PatternLayout </span> </li> <li class=""> <span>log4j.appender.FILE.layout.ConversionPattern=%d [%p] - %m%n </span> </li> <li class="alt"> <span> </span> </li> <li class=""> <span> </span> </li> <li class="alt"> <span>######################## </span> </li> <li class=""> <span># Rolling File </span> </li> <li class="alt"> <span>######################## </span> </li> <li class=""> <span>log4j.appender.ROLLING_FILE=org.apache.log4j.RollingFileAppender </span> </li> <li class="alt"> <span>log4j.appender.ROLLING_FILE.Threshold=ERROR </span> </li> <li class=""> <span>log4j.appender.ROLLING_FILE.File=rolling.log </span> </li> <li class="alt"> <span>log4j.appender.ROLLING_FILE.Append=</span><span class="keyword">true</span><span> </span> </li> <li class=""> <span>log4j.appender.ROLLING_FILE.MaxFileSize=10KB </span> </li> <li class="alt"> <span>log4j.appender.ROLLING_FILE.MaxBackupIndex=</span><span class="number">1</span><span> </span> </li> <li class=""> <span>log4j.appender.ROLLING_FILE.layout=org.apache.log4j.PatternLayout </span> </li> <li class="alt"> <span>log4j.appender.ROLLING_FILE.layout.ConversionPattern=%d [%p] - %m%n </span> </li> <li class=""> <span> </span> </li> <li class="alt"> <span> </span> </li> <li class=""> <span>#################### </span> </li> <li class="alt"> <span># Socket Appender </span> </li> <li class=""> <span>#################### </span> </li> <li class="alt"> <span>log4j.appender.SOCKET=org.apache.log4j.RollingFileAppender </span> </li> <li class=""> <span>log4j.appender.SOCKET.RemoteHost=localhost </span> </li> <li class="alt"> <span>log4j.appender.SOCKET.Port=</span><span class="number">5001</span><span> </span> </li> <li class=""> <span>log4j.appender.SOCKET.LocationInfo=</span><span class="keyword">true</span><span> </span> </li> <li class="alt"> <span># Set up </span><span class="keyword">for</span><span> Log Facter </span><span class="number">5</span><span> </span> </li> <li class=""> <span>log4j.appender.SOCKET.layout=org.apache.log4j.PatternLayout </span> </li> <li class="alt"> <span>log4j.appender.SOCET.layout.ConversionPattern=%d [%p] - %m%n </span> </li> <li class=""> <span> </span> </li> <li class="alt"> <span> </span> </li> <li class=""> <span>######################## </span> </li> <li class="alt"> <span># Log Factor </span><span class="number">5</span><span> Appender </span> </li> <li class=""> <span>######################## </span> </li> <li class="alt"> <span>log4j.appender.LF5_APPENDER=org.apache.log4j.lf5.LF5Appender </span> </li> <li class=""> <span>log4j.appender.LF5_APPENDER.MaxNumberOfRecords=</span><span class="number">2000</span><span> </span> </li> <li class="alt"> <span> </span> </li> <li class=""> <span> </span> </li> <li class="alt"> <span>######################## </span> </li> <li class=""> <span># SMTP Appender </span> </li> <li class="alt"> <span>####################### </span> </li> <li class=""> <span>log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender </span> </li> <li class="alt"> <span>log4j.appender.MAIL.Threshold=FATAL </span> </li> <li class=""> <span>log4j.appender.MAIL.BufferSize=</span><span class="number">10</span><span> </span> </li> <li class="alt"> <span>log4j.appender.MAIL.From=wuyh</span><span class="annotation">@primeton</span><span>.com </span> </li> <li class=""> <span>log4j.appender.MAIL.SMTPHost=mail.primeton.com </span> </li> <li class="alt"> <span>log4j.appender.MAIL.Subject=Log4J Message </span> </li> <li class=""> <span>log4j.appender.MAIL.To=test</span><span class="annotation">@primeton</span><span>.com </span> </li> <li class="alt"> <span>log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout </span> </li> <li class=""> <span>log4j.appender.MAIL.layout.ConversionPattern=%d [%p] - %m%n </span> </li> <li class="alt"> <span> </span> </li> <li class=""> <span> </span> </li> <li class="alt"> <span>######################## </span> </li> <li class=""> <span># JDBC Appender </span> </li> <li class="alt"> <span>####################### </span> </li> <li class=""> <span>log4j.appender.DATABASE=org.apache.log4j.jdbc.JDBCAppender </span> </li> <li class="alt"> <span>log4j.appender.DATABASE.URL=jdbc:mysql:</span><span class="comment">//localhost:3306/test </span><span> </span> </li> <li class=""> <span>log4j.appender.DATABASE.driver=com.mysql.jdbc.Driver </span> </li> <li class="alt"> <span>log4j.appender.DATABASE.user=root </span> </li> <li class=""> <span>log4j.appender.DATABASE.password= </span> </li> <li class="alt"> <span>log4j.appender.DATABASE.sql=INSERT INTO LOG4J (Message) VALUES (%d [%p] - %m%n) </span> </li> <li class=""> <span>log4j.appender.DATABASE.layout=org.apache.log4j.PatternLayout </span> </li> <li class="alt"> <span>log4j.appender.DATABASE.layout.ConversionPattern=%d [%p] - %m%n </span> </li> <li class=""> <span> </span> </li> <li class="alt"> <span> </span> </li> <li class=""> <span>log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender </span> </li> <li class="alt"> <span>log4j.appender.A1.File=SampleMessages.log4j </span> </li> <li class=""> <span>log4j.appender.A1.DatePattern=yyyyMMdd-HH</span><span class="string">'.log4j'</span><span> </span> </li> <li class="alt"> <span>log4j.appender.A1.layout=org.apache.log4j.xml.XMLLayout </span> </li> <li class=""> <span> </span> </li> <li class="alt"> <span>################### </span> </li> <li class=""> <span>#自定义Appender </span> </li> <li class="alt"> <span>################### </span> </li> <li class=""> <span>log4j.appender.im = net.cybercorlin.util.logger.appender.IMAppender </span> </li> <li class="alt"> <span> </span> </li> <li class=""> <span>log4j.appender.im.host = mail.cybercorlin.net </span> </li> <li class="alt"> <span>log4j.appender.im.username = username </span> </li> <li class=""> <span>log4j.appender.im.password = password </span> </li> <li class="alt"> <span>log4j.appender.im.recipient = wuyh</span><span class="annotation">@primeton</span><span>.com </span> </li> <li class=""> <span> </span> </li> <li class="alt"> <span>log4j.appender.im.layout=org.apache.log4j.PatternLayout </span> </li> <li class=""> <span>log4j.appender.im.layout.ConversionPattern =%d [%p] - %m%n </span> </li> </ol> </div> <textarea class="java" style="display: none" name="code" rows="15" cols="100">log4j.rootLogger=DEBUG, CONSOLE #DEBUG, CONSOLE,FILE,ROLLING_FILE,MAIL,DATABASE log4j.logger.org.apache=INFO, FILE log4j.additivity.org.apache=false ################### # 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=%d [%p] - %m%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=%d [%p] - %m%n ######################## # 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=%d [%p] - %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=%d [%p] - %m%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 log4j.appender.MAIL.From=wuyh@primeton.com log4j.appender.MAIL.SMTPHost=mail.primeton.com log4j.appender.MAIL.Subject=Log4J Message log4j.appender.MAIL.To=test@primeton.com log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout log4j.appender.MAIL.layout.ConversionPattern=%d [%p] - %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 (%d [%p] - %m%n) log4j.appender.DATABASE.layout=org.apache.log4j.PatternLayout log4j.appender.DATABASE.layout.ConversionPattern=%d [%p] - %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 = wuyh@primeton.com log4j.appender.im.layout=org.apache.log4j.PatternLayout log4j.appender.im.layout.ConversionPattern =%d [%p] - %m%n </textarea><br><br> #注意:在属性配置文件中,属性值的第一个一定是级别,输出端可有可无,以逗号分割。(而xml文件格式没有这种限制)<br><br> log4j.xml<br><div class="dp-highlighter"> <div class="bar"> <div class="tools"> <a onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;" href="http://www.icnote.com/#">view plain</a><a onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;" href="http://www.icnote.com/#">copy to clipboard</a><a onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;" href="http://www.icnote.com/#">print</a><a onclick="dp.sh.Toolbar.Command('About',this);return false;" href="http://www.icnote.com/#">?</a> </div> </div> <ol class="dp-j"> <li class="alt"> <span><span>< xml version=</span><span class="string">"1.0"</span><span> encoding=</span><span class="string">"UTF-8"</span><span> > </span></span> </li> <li class=""> <span><!DOCTYPE log4j:configuration SYSTEM </span><span class="string">"log4j.dtd"</span><span>> </span> </li> <li class="alt"> <span><log4j:configuration xmlns:log4j=</span><span class="string">"http://jakarta.apache.org/log4j/"</span><span> debug=</span><span class="string">"false"</span><span> threshold=</span><span class="string">"null"</span><span>> </span> </li> <li class=""> <span> </span> </li> <li class="alt"> <span> </span> </li> <li class=""> <span><appender </span><span class="keyword">class</span><span>=</span><span class="string">"org.apache.log4j.ConsoleAppender"</span><span> name=</span><span class="string">"CONSOLE"</span><span>> </span> </li> <li class="alt"> <span><param name=</span><span class="string">"Target"</span><span> value=</span><span class="string">"System.out"</span><span>/> </span> </li> <li class=""> <span><param name=</span><span class="string">"Threshold"</span><span> value=</span><span class="string">"INFO"</span><span>/> </span> </li> <li class="alt"> <span><layout </span><span class="keyword">class</span><span>=</span><span class="string">"org.apache.log4j.PatternLayout"</span><span>> </span> </li> <li class=""> <span><param name=</span><span class="string">"ConversionPattern"</span><span> value=</span><span class="string">"%d [%p] - %m%n "</span><span>/> </span> </li> <li class="alt"> <span></layout> </span> </li> <li class=""> <span><filter </span><span class="keyword">class</span><span>=</span><span class="string">"org.apache.log4j.varia.DenyAllFilter"</span><span>/> </span> </li> <li class="alt"> <span><errorHandler </span><span class="keyword">class</span><span>=</span><span class="string">"org.apache.log4j.varia. FallbackErrorHandler"</span><span>/> </span> </li> <li class=""> <span></appender> </span> </li> <li class="alt"> <span> </span> </li> <li class=""> <span><appender </span><span class="keyword">class</span><span>=</span><span class="string">"org.apache.log4j.FileAppender"</span><span> name=</span><span class="string">"FILE"</span><span>> </span> </li> <li class="alt"> <span><param name=</span><span class="string">"File"</span><span> value=</span><span class="string">"file.log"</span><span>/> </span> </li> <li class=""> <span><param name=</span><span class="string">"Append"</span><span> value=</span><span class="string">"false"</span><span>/> </span> </li> <li class="alt"> <span><param name=</span><span class="string">"Threshold"</span><span> value=</span><span class="string">"INFO"</span><span>/> </span> </li> <li class=""> <span><layout </span><span class="keyword">class</span><span>=</span><span class="string">"org.apache.log4j.PatternLayout"</span><span>> </span> </li> <li class="alt"> <span><param name=</span><span class="string">"ConversionPattern"</span><span> value=</span><span class="string">"%d [%p] - %m%n "</span><span>/> </span> </li> <li class=""> <span></layout> </span> </li> <li class="alt"> <span></appender> </span> </li> <li class=""> <span> </span> </li> <li class="alt"> <span><appender </span><span class="keyword">class</span><span>=</span><span class="string">"org.apache.log4j.RollingFileAppender"</span><span> name=</span><span class="string">"ROLLING_FILE"</span><span>> </span> </li> <li class=""> <span><param name=</span><span class="string">"Threshold"</span><span> value=</span><span class="string">"INFO"</span><span>/> </span> </li> <li class="alt"> <span><param name=</span><span class="string">"File"</span><span> value=</span><span class="string">"rolling.log"</span><span>/> </span> </li> <li class=""> <span><param name=</span><span class="string">"Append"</span><span> value=</span><span class="string">"false"</span><span>/> </span> </li> <li class="alt"> <span><param name=</span><span class="string">"MaxFileSize"</span><span> value=</span><span class="string">"10KB"</span><span>/> </span> </li> <li class=""> <span><param name=</span><span class="string">"MaxBackupIndex"</span><span> value=</span><span class="string">"1"</span><span>/> </span> </li> <li class="alt"> <span><layout </span><span class="keyword">class</span><span>=</span><span class="string">"org.apache.log4j.PatternLayout"</span><span>> </span> </li> <li class=""> <span><param name=</span><span class="string">"ConversionPattern"</span><span> value=</span><span class="string">"%d [%p] - %m%n "</span><span>/> </span> </li> <li class="alt"> <span></layout> </span> </li> <li class=""> <span></appender> </span> </li> <li class="alt"> <span> </span> </li> <li class=""> <span><logger additivity=</span><span class="string">"false"</span><span> name=</span><span class="string">"com.eos"</span><span>> </span> </li> <li class="alt"> <span><level value=</span><span class="string">"info"</span><span>/> </span> </li> <li class=""> <span><appender-ref ref=</span><span class="string">"CONSOLE"</span><span>/> </span> </li> <li class="alt"> <span></logger> </span> </li> <li class=""> <span> </span> </li> <li class="alt"> <span><category additivity=</span><span class="string">"true"</span><span> name=</span><span class="string">"com.eos.log"</span><span>> </span> </li> <li class=""> <span><priority value=</span><span class="string">"warn"</span><span>/> </span> </li> <li class="alt"> <span></category> </span> </li> <li class=""> <span> </span> </li> <li class="alt"> <span><root> </span> </li> <li class=""> <span><priority value=</span><span class="string">"info"</span><span>/> </span> </li> <li class="alt"> <span><appender-ref ref=</span><span class="string">"CONSOLE"</span><span>/> </span> </li> <li class=""> <span></root> </span> </li> <li class="alt"> <span></log4j:configuration> </span> </li> </ol> </div> <textarea class="java" style="display: none" name="code" rows="15" cols="100">< xml version="1.0" encoding="UTF-8" > <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false" threshold="null"> <appender class="org.apache.log4j.ConsoleAppender" name="CONSOLE"> <param name="Target" value="System.out"/> <param name="Threshold" value="INFO"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d [%p] - %m%n "/> </layout> <filter class="org.apache.log4j.varia.DenyAllFilter"/> <errorHandler class="org.apache.log4j.varia. FallbackErrorHandler"/> </appender> <appender class="org.apache.log4j.FileAppender" name="FILE"> <param name="File" value="file.log"/> <param name="Append" value="false"/> <param name="Threshold" value="INFO"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d [%p] - %m%n "/> </layout> </appender> <appender class="org.apache.log4j.RollingFileAppender" name="ROLLING_FILE"> <param name="Threshold" value="INFO"/> <param name="File" value="rolling.log"/> <param name="Append" value="false"/> <param name="MaxFileSize" value="10KB"/> <param name="MaxBackupIndex" value="1"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d [%p] - %m%n "/> </layout> </appender> <logger additivity="false" name="com.eos"> <level value="info"/> <appender-ref ref="CONSOLE"/> </logger> <category additivity="true" name="com.eos.log"> <priority value="warn"/> </category> <root> <priority value="info"/> <appender-ref ref="CONSOLE"/> </root> </log4j:configuration> </textarea><br></layout></appender></logger></logger></category></configuration>
相关推荐
通过上述配置,`log4j`就能够按照日期每天创建新的日志文件,并且可以根据需求定制不同类别的日志输出。这对于大型系统的监控和维护来说,是非常重要的功能。在实际开发中,根据项目规模和需求,可能还需要考虑日志...
Log4j的核心组成部分包括:Loggers(日志类别)、Appenders(日志输出目的地)和Layouts(日志格式)。通过这三个核心组件的组合使用,我们可以非常容易地控制日志信息的级别、输出的位置以及输出的格式。 #### 二...
Log4j是Apache的一个开源项目,它为Java应用程序提供了一个灵活的日志系统。这个系统能够帮助开发者记录程序运行过程中的各种信息,包括错误、警告、调试信息等,从而便于问题定位和性能分析。Log4j提供了多种配置...
* org.apache.log4j.DailyRollingFileAppender(每天产生一个日志文件) * org.apache.log4j.RollingFileAppender(文件大小到达指定尺寸的时候产生一个新的文件) * org.apache.log4j.WriterAppender(将日志信息以...
Log4j是一款广泛使用的日志记录框架,尤其在Java应用程序中。它提供了强大的日志管理功能,能够帮助开发者实现灵活的日志输出控制,方便调试、性能分析和问题追踪。本篇将详细介绍Log4j的配置文件及其常用属性,旨在...
在JBOSS环境中运行应用程序时,Hibernate会默认输出大量的日志信息到指定的日志文件中(例如`D:\jboss-4.0.2\server\default\log\server.log`)。这些日志信息虽然有助于排查问题,但也会显著降低应用程序的启动速度...
Log4j的配置通常通过`log4j.properties`或`log4j.xml`文件完成,其中包含了日志器、输出器和布局的详细设置。例如: ```properties # 配置控制台输出 log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4...
Java日志工具Log4j是Java开发者非常熟悉的一个开源日志框架,它为应用程序提供了灵活且高效的日志记录功能。Log4j不仅简单易用,而且功能强大,能够帮助开发者在开发、测试和生产环境中方便地收集和分析日志信息。...
【log4j配置文档(记录日志)】 Log4j是Apache开源组织...总结来说,Log4j是Java开发中的重要工具,通过灵活配置,它可以适应各种日志需求,帮助开发者更好地管理和分析应用的行为,从而提升开发效率和系统的可维护性。
根据提供的标题、描述、...通过以上分析,我们可以看到log4j配置文件的强大之处在于能够灵活地控制日志的记录方式、格式以及输出目标,这对于开发高质量的应用程序来说是非常重要的。希望这些知识点能对你有所帮助。
例如,你可以使用`org.apache.log4j.ConsoleAppender`将日志输出到控制台,`org.apache.log4j.FileAppender`写入文件,或者使用`org.apache.log4j.DailyRollingFileAppender`每天创建一个新的日志文件。 - 配置...
### log4j使用配置方法及项目中的应用 #### 概述与背景 在软件开发过程中,日志记录是一项至关...通过以上配置,Log4j能够按照预设规则高效、准确地记录和输出日志信息,极大提升了应用程序的可观测性和可维护性。
- `org.apache.log4j.DailyRollingFileAppender`:每天创建新的日志文件。 - `org.apache.log4j.RollingFileAppender`:文件大小达到指定大小时创建新文件。 - `org.apache.log4j.WriterAppender`:将日志信息写入...
在Java日志处理领域,Log4j是一款广泛应用的开源日志框架,它为开发者提供了灵活且高效的日志记录功能。本文将深入解析Log4j的配置文件属性、日志级别、布局方式以及参数设置等核心知识点,帮助你更好地理解和利用这...
- **log4j.rootCategory**:设置根日志类别。这里的“INFO”表示日志级别,而“stdout, R”则是日志输出的目标。例如,“stdout”表示控制台输出,而“R”则需要进一步定义。 - **log4j.appender.stdout**:定义了名...
- 滚动文件输出(RollingFileAppender): 使用`org.apache.log4j.RollingFileAppender`并设置了文件路径、文件最大尺寸以及备份文件的最大数量。 - **Layout(布局)**: 定义了日志输出的具体格式。例如`...
- `log4j.appender.FILE`:定义名为FILE的Appender,通常会指定类型(如`FileAppender`)和相关属性,如`log4j.appender.FILE.File=log/app.log`指定了日志文件路径。 - `log4j.appender.FILE.layout`:设置FILE ...
- `org.apache.log4j.DailyRollingFileAppender`:每天产生一个新日志文件。 - `org.apache.log4j.RollingFileAppender`:文件达到指定大小时产生新文件。 - `org.apache.log4j.WriterAppender`:将日志信息输出...
日志记录在软件开发中扮演着至关重要的角色,Log4j通过集中管理和定制化的日志输出,简化了这个过程。传统的日志语句散落在代码各处,不利于维护和查找。Log4j通过配置文件控制日志的显示,允许开发者根据需要调整...