我是靠谱客的博主 俭朴猫咪,最近开发中收集的这篇文章主要介绍系统日志分类别输出至日志文件[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平台为"rn",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 οnclick="dp.sh.Toolbar.Command('ViewSource',this);return false;" href="http://www.icnote.com/#">view plain</a><a οnclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;" href="http://www.icnote.com/#">copy to clipboard</a><a οnclick="dp.sh.Toolbar.Command('PrintSource',this);return false;" href="http://www.icnote.com/#">print</a><a οnclick="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 οnclick="dp.sh.Toolbar.Command('ViewSource',this);return false;" href="http://www.icnote.com/#">view plain</a><a οnclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;" href="http://www.icnote.com/#">copy to clipboard</a><a οnclick="dp.sh.Toolbar.Command('PrintSource',this);return false;" href="http://www.icnote.com/#">print</a><a οnclick="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>&lt; xml version=</span><span class="string">"1.0"</span><span> encoding=</span><span class="string">"UTF-8"</span><span> &gt;    </span></span> </li> <li class=""> <span>&lt;!DOCTYPE log4j:configuration SYSTEM </span><span class="string">"log4j.dtd"</span><span>&gt;    </span> </li> <li class="alt"> <span>&lt;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>&gt;    </span> </li> <li class=""> <span>  </span> </li> <li class="alt"> <span>  </span> </li> <li class=""> <span>&lt;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>&gt;    </span> </li> <li class="alt"> <span>&lt;param name=</span><span class="string">"Target"</span><span> value=</span><span class="string">"System.out"</span><span>/&gt;    </span> </li> <li class=""> <span>&lt;param name=</span><span class="string">"Threshold"</span><span> value=</span><span class="string">"INFO"</span><span>/&gt;    </span> </li> <li class="alt"> <span>&lt;layout </span><span class="keyword">class</span><span>=</span><span class="string">"org.apache.log4j.PatternLayout"</span><span>&gt;    </span> </li> <li class=""> <span>&lt;param name=</span><span class="string">"ConversionPattern"</span><span> value=</span><span class="string">"%d [%p] - %m%n "</span><span>/&gt;    </span> </li> <li class="alt"> <span>&lt;/layout&gt;    </span> </li> <li class=""> <span>&lt;filter </span><span class="keyword">class</span><span>=</span><span class="string">"org.apache.log4j.varia.DenyAllFilter"</span><span>/&gt;    </span> </li> <li class="alt"> <span>&lt;errorHandler </span><span class="keyword">class</span><span>=</span><span class="string">"org.apache.log4j.varia. FallbackErrorHandler"</span><span>/&gt;    </span> </li> <li class=""> <span>&lt;/appender&gt;    </span> </li> <li class="alt"> <span>  </span> </li> <li class=""> <span>&lt;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>&gt;    </span> </li> <li class="alt"> <span>&lt;param name=</span><span class="string">"File"</span><span> value=</span><span class="string">"file.log"</span><span>/&gt;    </span> </li> <li class=""> <span>&lt;param name=</span><span class="string">"Append"</span><span> value=</span><span class="string">"false"</span><span>/&gt;    </span> </li> <li class="alt"> <span>&lt;param name=</span><span class="string">"Threshold"</span><span> value=</span><span class="string">"INFO"</span><span>/&gt;    </span> </li> <li class=""> <span>&lt;layout </span><span class="keyword">class</span><span>=</span><span class="string">"org.apache.log4j.PatternLayout"</span><span>&gt;    </span> </li> <li class="alt"> <span>&lt;param name=</span><span class="string">"ConversionPattern"</span><span> value=</span><span class="string">"%d [%p] - %m%n "</span><span>/&gt;    </span> </li> <li class=""> <span>&lt;/layout&gt;    </span> </li> <li class="alt"> <span>&lt;/appender&gt;    </span> </li> <li class=""> <span>  </span> </li> <li class="alt"> <span>&lt;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>&gt;    </span> </li> <li class=""> <span>&lt;param name=</span><span class="string">"Threshold"</span><span> value=</span><span class="string">"INFO"</span><span>/&gt;    </span> </li> <li class="alt"> <span>&lt;param name=</span><span class="string">"File"</span><span> value=</span><span class="string">"rolling.log"</span><span>/&gt;    </span> </li> <li class=""> <span>&lt;param name=</span><span class="string">"Append"</span><span> value=</span><span class="string">"false"</span><span>/&gt;    </span> </li> <li class="alt"> <span>&lt;param name=</span><span class="string">"MaxFileSize"</span><span> value=</span><span class="string">"10KB"</span><span>/&gt;    </span> </li> <li class=""> <span>&lt;param name=</span><span class="string">"MaxBackupIndex"</span><span> value=</span><span class="string">"1"</span><span>/&gt;    </span> </li> <li class="alt"> <span>&lt;layout </span><span class="keyword">class</span><span>=</span><span class="string">"org.apache.log4j.PatternLayout"</span><span>&gt;    </span> </li> <li class=""> <span>&lt;param name=</span><span class="string">"ConversionPattern"</span><span> value=</span><span class="string">"%d [%p] - %m%n "</span><span>/&gt;    </span> </li> <li class="alt"> <span>&lt;/layout&gt;    </span> </li> <li class=""> <span>&lt;/appender&gt;    </span> </li> <li class="alt"> <span>  </span> </li> <li class=""> <span>&lt;logger additivity=</span><span class="string">"false"</span><span> name=</span><span class="string">"com.eos"</span><span>&gt;    </span> </li> <li class="alt"> <span>&lt;level value=</span><span class="string">"info"</span><span>/&gt;    </span> </li> <li class=""> <span>&lt;appender-ref ref=</span><span class="string">"CONSOLE"</span><span>/&gt;    </span> </li> <li class="alt"> <span>&lt;/logger&gt;    </span> </li> <li class=""> <span>  </span> </li> <li class="alt"> <span>&lt;category additivity=</span><span class="string">"true"</span><span> name=</span><span class="string">"com.eos.log"</span><span>&gt;    </span> </li> <li class=""> <span>&lt;priority value=</span><span class="string">"warn"</span><span>/&gt;    </span> </li> <li class="alt"> <span>&lt;/category&gt;    </span> </li> <li class=""> <span>  </span> </li> <li class="alt"> <span>&lt;root&gt;    </span> </li> <li class=""> <span>&lt;priority value=</span><span class="string">"info"</span><span>/&gt;    </span> </li> <li class="alt"> <span>&lt;appender-ref ref=</span><span class="string">"CONSOLE"</span><span>/&gt;    </span> </li> <li class=""> <span>&lt;/root&gt;    </span> </li> <li class="alt"> <span>&lt;/log4j:configuration&gt;   </span> </li> </ol> </div> <textarea class="java" style="display: none" name="code" rows="15" cols="100">&lt; xml version="1.0" encoding="UTF-8" &gt; &lt;!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"&gt; &lt;log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false" threshold="null"&gt; &lt;appender class="org.apache.log4j.ConsoleAppender" name="CONSOLE"&gt; &lt;param name="Target" value="System.out"/&gt; &lt;param name="Threshold" value="INFO"/&gt; &lt;layout class="org.apache.log4j.PatternLayout"&gt; &lt;param name="ConversionPattern" value="%d [%p] - %m%n "/&gt; &lt;/layout&gt; &lt;filter class="org.apache.log4j.varia.DenyAllFilter"/&gt; &lt;errorHandler class="org.apache.log4j.varia. FallbackErrorHandler"/&gt; &lt;/appender&gt; &lt;appender class="org.apache.log4j.FileAppender" name="FILE"&gt; &lt;param name="File" value="file.log"/&gt; &lt;param name="Append" value="false"/&gt; &lt;param name="Threshold" value="INFO"/&gt; &lt;layout class="org.apache.log4j.PatternLayout"&gt; &lt;param name="ConversionPattern" value="%d [%p] - %m%n "/&gt; &lt;/layout&gt; &lt;/appender&gt; &lt;appender class="org.apache.log4j.RollingFileAppender" name="ROLLING_FILE"&gt; &lt;param name="Threshold" value="INFO"/&gt; &lt;param name="File" value="rolling.log"/&gt; &lt;param name="Append" value="false"/&gt; &lt;param name="MaxFileSize" value="10KB"/&gt; &lt;param name="MaxBackupIndex" value="1"/&gt; &lt;layout class="org.apache.log4j.PatternLayout"&gt; &lt;param name="ConversionPattern" value="%d [%p] - %m%n "/&gt; &lt;/layout&gt; &lt;/appender&gt; &lt;logger additivity="false" name="com.eos"&gt; &lt;level value="info"/&gt; &lt;appender-ref ref="CONSOLE"/&gt; &lt;/logger&gt; &lt;category additivity="true" name="com.eos.log"&gt; &lt;priority value="warn"/&gt; &lt;/category&gt; &lt;root&gt; &lt;priority value="info"/&gt; &lt;appender-ref ref="CONSOLE"/&gt; &lt;/root&gt; &lt;/log4j:configuration&gt; </textarea><br></layout></appender></logger></logger></category></configuration>

最后

以上就是俭朴猫咪为你收集整理的系统日志分类别输出至日志文件[Log4j应用]的全部内容,希望文章能够帮你解决系统日志分类别输出至日志文件[Log4j应用]所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(31)

评论列表共有 0 条评论

立即
投稿
返回
顶部