生活随笔
收集整理的這篇文章主要介紹了
log4j xml配置详解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
http://zhangxiang390.iteye.com/blog/258455
熟讀一個典型的log4j配置文件:
?
Xml代碼 ?
<? xml ?version ="1.0" ?encoding ="UTF-8" ?> ??<!DOCTYPE?log4j:configuration?SYSTEM?"log4j.dtd"> ?? ?? ?? < log4j:configuration ?xmlns:log4j ='http://jakarta.apache.org/log4j/' ?> ???? ?? ????< appender ?name ="myConsole" ?class ="org.apache.log4j.ConsoleAppender" > ?? ????????< layout ?class ="org.apache.log4j.PatternLayout" > ?? ????????????< param ?name ="ConversionPattern" ?? ?? ????????????????value ="[%d{dd?HH:mm:ss,SSS\}?%-5p]?[%t]?%c{2\}?-?%m%n" ?/> ?? ????????</ layout > ?? ?????????? ????????< filter ?class ="org.apache.log4j.varia.LevelRangeFilter" > ?? ????????????< param ?name ="levelMin" ?value ="debug" ?/> ?? ????????????< param ?name ="levelMax" ?value ="warn" ?/> ?? ????????????< param ?name ="AcceptOnMatch" ?value ="true" ?/> ?? ????????</ filter > ?? ????</ appender > ?? ?? ????< appender ?name ="myFile" ?class ="org.apache.log4j.RollingFileAppender" > ?? ?? ????????< param ?name ="File" ?value ="D:/output.log" ?/> ?? ?????????? ????????< param ?name ="Append" ?value ="true" ?/> ?? ????????< param ?name ="MaxBackupIndex" ?value ="10" ?/> ?? ????????< layout ?class ="org.apache.log4j.PatternLayout" > ?? ????????????< param ?name ="ConversionPattern" ?value ="%p?(%c:%L)-?%m%n" ?/> ?? ????????</ layout > ?? ????</ appender > ?? ? ?? ????< appender ?name ="activexAppender" ?class ="org.apache.log4j.DailyRollingFileAppender" > ?? ????????< param ?name ="File" ?value ="E:/activex.log" ?/> ? ?? ????????< param ?name ="DatePattern" ?value ="'.'yyyy-MM-dd'.log'" ?/> ? ?? ????????< layout ?class ="org.apache.log4j.PatternLayout" > ?? ?????????< param ?name ="ConversionPattern" ? ?? ????????????value ="[%d{MMdd?HH:mm:ss?SSS\}?%-5p]?[%t]?%c{3\}?-?%m%n" ?/> ?? ????????</ layout > ? ?? ????</ appender > ?? ?? ?? ?????? ????< logger ?name ="com.runway.bssp.activeXdemo" ?additivity ="false" > ?? ????????< priority ?value ?="info" /> ? ?? ????????< appender-ref ?ref ="activexAppender" ?/> ? ?? ????</ logger > ?? ?? ?????? ????< root > ?? ????????< priority ?value ?="debug" /> ?? ????????< appender-ref ?ref ="myConsole" /> ?? ????????< appender-ref ?ref ="myFile" /> ?? ?? ????</ root > ?? </ log4j:configuration > ??
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"><log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/' ><appender name="myConsole" class="org.apache.log4j.ConsoleAppender"><layout class="org.apache.log4j.PatternLayout"><param name="ConversionPattern" value="[%d{dd HH:mm:ss,SSS\} %-5p] [%t] %c{2\} - %m%n" /></layout><!--過濾器設置輸出的級別--><filter class="org.apache.log4j.varia.LevelRangeFilter"><param name="levelMin" value="debug" /><param name="levelMax" value="warn" /><param name="AcceptOnMatch" value="true" /></filter></appender><appender name="myFile" class="org.apache.log4j.RollingFileAppender"> <param name="File" value="D:/output.log" /><!-- 設置日志輸出文件名 --><!-- 設置是否在重新啟動服務時,在原有日志的基礎添加新日志 --><param name="Append" value="true" /><param name="MaxBackupIndex" value="10" /><layout class="org.apache.log4j.PatternLayout"><param name="ConversionPattern" value="%p (%c:%L)- %m%n" /></layout></appender><appender name="activexAppender" class="org.apache.log4j.DailyRollingFileAppender"><param name="File" value="E:/activex.log" /> <param name="DatePattern" value="'.'yyyy-MM-dd'.log'" /> <layout class="org.apache.log4j.PatternLayout"><param name="ConversionPattern" value="[%d{MMdd HH:mm:ss SSS\} %-5p] [%t] %c{3\} - %m%n" /></layout> </appender><!-- 指定logger的設置,additivity指示是否遵循缺省的繼承機制--><logger name="com.runway.bssp.activeXdemo" additivity="false"><priority value ="info"/> <appender-ref ref="activexAppender" /> </logger><!-- 根logger的設置--><root><priority value ="debug"/><appender-ref ref="myConsole"/><appender-ref ref="myFile"/> </root>
</log4j:configuration>
?
(1). 輸出方式appender一般有5種:
???????????? org.apache.log4j.RollingFileAppender(滾動文件,自動記錄最新日志) ???????????? org.apache.log4j.ConsoleAppender (控制臺)?? ???????????? org.apache.log4j.FileAppender (文件) ???????????? org.apache.log4j.DailyRollingFileAppender (每天產生一個日志文件) ???????????? org.apache.log4j.WriterAppender (將日志信息以流格式發(fā)送到任意指定的地方)
?
(2). 日記記錄的優(yōu)先級priority,優(yōu)先級由高到低分為 ??????????? OFF ,FATAL ,ERROR ,WARN ,INFO ,DEBUG ,ALL。 ??????????? Log4j建議只使用FATAL ,ERROR ,WARN ,INFO ,DEBUG這五個級別。
?
(3). 格式說明layout中的參數都以%開始,后面不同的參數代表不同的格式化信息(參數按字母表順序列出): ??????????????? %c??????? 輸出所屬類的全名,可在修改為 %d{Num} ,Num類名輸出的維(如:"org.apache.elathen.ClassName",%C{2}將輸出elathen.ClassName) ??????????????? %d?????? 輸出日志時間其格式為 %d{yyyy-MM-dd HH:mm:ss,SSS},可指定格式 如 %d{HH:mm:ss} ??????????????? %l??????? 輸出日志事件發(fā)生位置,包括類目名、發(fā)生線程,在代碼中的行數 ??????????????? %n?????? 換行符 ??????????????? %m????? 輸出代碼指定信息,如info(“message”),輸出message ??????????????? %p?????? 輸出優(yōu)先級,即 FATAL ,ERROR 等 ??????????????? %r??????? 輸出從啟動到顯示該log信息所耗費的毫秒數 ??????????????? %t??????? 輸出產生該日志事件的線程名
?
?
============
http://jessehu.wordpress.com/2009/11/17/log4j-levels-all-trace-debug-info-warn-error-fatal-off/
Loggers may be assigned levels. The set of possible levels, that is DEBUG, INFO, WARN, ERROR and FATAL are defined in the org.apache.log4j.Level class. If a given logger is not assigned a level, then it inherits one from its closest ancestor with an assigned level.
The root logger resides at the top of the logger hierarchy. It always exists and always has an assigned level.
The logger is the core component of the logging process. In log4j, there are 5 normal levels Levels of logger available (not including custom Levels), the following is borrowed from the log4j API (http://jakarta.apache.org/log4j/docs/api/index.html): DEBUG - The DEBUG Level designates fine-grained informational events that are most useful to debug an application. INFO – The INFO level designates informational messages that highlight the progress of the application at coarse-grained level. WARN – The WARN level designates potentially harmful situations. ERROR – The ERROR level designates error events that might still allow the application to continue running.
TRACE - The TRACE Level designates finer-grained informational events than the DEBUG FATAL – The FATAL level designates very severe error events that will presumably lead the application to abort. In addition, there are two special levels of logging available: (descriptions borrowed from the log4j API http://jakarta.apache.org/log4j/docs/api/index.html): ALL -The ALL Level has the lowest possible rank and is intended to turn on all logging. OFF – The OFF Level has the highest possible rank and is intended to turn off logging.
?
?
總結
以上是生活随笔 為你收集整理的log4j xml配置详解 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。