log4j xml配置详解

http://zhangxiang390.iteye.com/blog/258455

熟读一个典型的log4j配置文件:

 

Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">  
  3.      
  4. <log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/' >  
  5.      
  6.     <appender name="myConsole" class="org.apache.log4j.ConsoleAppender">  
  7.         <layout class="org.apache.log4j.PatternLayout">  
  8.             <param name="ConversionPattern"     
  9.                 value="[%d{dd HH:mm:ss,SSS\} %-5p] [%t] %c{2\} - %m%n" />  
  10.         </layout>  
  11.         <!--过滤器设置输出的级别-->  
  12.         <filter class="org.apache.log4j.varia.LevelRangeFilter">  
  13.             <param name="levelMin" value="debug" />  
  14.             <param name="levelMax" value="warn" />  
  15.             <param name="AcceptOnMatch" value="true" />  
  16.         </filter>  
  17.     </appender>  
  18.   
  19.     <appender name="myFile" class="org.apache.log4j.RollingFileAppender">     
  20.         <param name="File" value="D:/output.log" /><!-- 设置日志输出文件名 -->  
  21.         <!-- 设置是否在重新启动服务时,在原有日志的基础添加新日志 -->  
  22.         <param name="Append" value="true" />  
  23.         <param name="MaxBackupIndex" value="10" />  
  24.         <layout class="org.apache.log4j.PatternLayout">  
  25.             <param name="ConversionPattern" value="%p (%c:%L)- %m%n" />  
  26.         </layout>  
  27.     </appender>  
  28.     
  29.     <appender name="activexAppender" class="org.apache.log4j.DailyRollingFileAppender">  
  30.         <param name="File" value="E:/activex.log" />    
  31.         <param name="DatePattern" value="'.'yyyy-MM-dd'.log'" />    
  32.         <layout class="org.apache.log4j.PatternLayout">  
  33.          <param name="ConversionPattern"    
  34.             value="[%d{MMdd HH:mm:ss SSS\} %-5p] [%t] %c{3\} - %m%n" />  
  35.         </layout>    
  36.     </appender>  
  37.      
  38.     <!-- 指定logger的设置,additivity指示是否遵循缺省的继承机制-->  
  39.     <logger name="com.runway.bssp.activeXdemo" additivity="false">  
  40.         <priority value ="info"/>    
  41.         <appender-ref ref="activexAppender" />    
  42.     </logger>  
  43.   
  44.     <!-- 根logger的设置-->  
  45.     <root>  
  46.         <priority value ="debug"/>  
  47.         <appender-ref ref="myConsole"/>  
  48.         <appender-ref ref="myFile"/>     
  49.     </root>  
  50. </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 (将日志信息以流格式发送到任意指定的地方)

 

(2). 日记记录的优先级priority,优先级由高到低分为
            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        输出日志事件发生位置,包括类目名、发生线程,在代码中的行数
                %n       换行符
                %m      输出代码指定信息,如info(“message”),输出message
                %p       输出优先级,即 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.

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值