2011年4月10日 星期日

[Configuration] log4j.xml

* 有 property and xml 兩種設定方式

* Log level
FATAL      0  
  ERROR      3  
  WARN       4  
  INFO       6  
  DEBUG      7 

* Appender: log 輸出位置
org.apache.log4j.ConsoleAppender: 控制台

org.apache.log4j.FileAppender: 文件
 - . Threshold: log 最低 levle
. ImmediateFlush: default is true, 所有 log 都會馬上被 output
. Target: default is System.out, output on console,
   to choose which console stream to print messages to, System.out or System.err.

org.apache.log4j.DailyRollingFileAppender: 每天產生一個 log file
- . Append: default is true, 將 log 增加到指定 file, false is 覆蓋 log
. File: 指定文件
......

org.apache.log4j.RollingFileAppender: 文件大小到達指定大小的时候產生一個新的文件

org.apache.log4j.WriterAppender: 將 log 信息以串流格式發送到任意指定的地方

* Layout: log 輸出格式
org.apache.log4j.HTMLLayout: 以HTML表格形式布局

org.apache.log4j.PatternLayout: 可任意自定格式

org.apache.log4j.SimpleLayout: 包含 log 信息的级别和信息字符串

org.apache.log4j.TTCCLayout: 包含產生 log 的時間、thread、class 等等信息

* 列印參數的格式類似於 C
%m   程式中的 message

  %p   log level

  %r   啟動應用到輸出 log 所花費的毫秒數

  %c   class name

  %t   產生該 log 的 thread

  %n   換行

  %d   日期或時間,ex: %d{yyy MMM dd HH:mm:ss , SSS}

  %l   log 的發生位置,包括 class name, thread, line number,ex: Testlog4.main(TestLog4.java: 10 )。

* Logger: 對應 package 和 appender
additivity: 是否往上查找未設定的屬性
appender-ref: appender reference,可以有多個
level: log level priority,default is "debug"
  //  若沒有設定則是繼承 root logger 的設定
  // everything of spring log level is default "info"

* root: root Logger
以 root 的 LEVEL 為主,ex: 假設 root level 為 INFO,那麼即使設為 DEBUG 也只會輸出到 INFO level。

* API
  • PatternLayout
    • The goal of this class is to format a LoggingEvent and return the results as a String. The results depend on the conversion pattern.
    • The left justification flag which is just the minus (-) character comes the optional minimum field width modifier. This is a decimal constant that represents the minimum number of characters to output.
    • The padding character is space. If the data item is larger than the minimum field width, the field is expanded to accommodate the data. The value is never truncated.
    • This behavior can be changed using the maximum field width modifier which is designated by a period followed by a decimal constant. If the data item is longer than the maximum field, then the extra characters are removed from the beginning of the data item and not from the end.
  • ConsoleAppender
    • Appends log events to System.out or System.err using a layout specified by the user. The default target is System.out.
  • LevelRangeFilter
    • A very simple filter based on level matching, which can be used to reject messages with priorities outside a certain range.
    • The filter admits three options LevelMin, LevelMax and AcceptOnMatch.
    • If LevelMinw/LevelMax is not defined, then there is no minimum/maximum acceptable level (ie a level is never rejected for being too "low"/unimportant// "high"/important).
  • DailyRollingFileAppender
    • The rolling schedule is specified by the DatePattern option. This pattern should follow the SimpleDateFormat conventions. In particular, you must escape literal text within a pair of single quotes. A formatted version of the date pattern is used as the suffix for the rolled file name.
  • Logger
  • Level
    • Defines the minimum set of levels recognized by the system, that is OFFFATALERRORWARNINFODEBUG and ALL.
    • TRACE: The TRACE Level designates finer-grained informational events than the DEBUG

* Reference
- log4j 基本配置
- Apache Log4j 1.2.16 API
- log4j使用指南 ***
- Apache Log4j 学习笔记 **
- org.apache.log4j.Logger 详解 **
- wiki

沒有留言:

張貼留言