星期二, 9月 09, 2014

[JavaScript] 快快樂樂學log4javascript

log4javascript 是基於Apache log4j的log系統,基本上用過log4j的人應該很快就可以上手。
有興趣的話可以看一下之前的筆記: [Java] 快快樂樂學會log4j

Appender 

提供以下幾種Appender

Appender
AlertAppender =>將log透過alert秀出來
AjaxAppender => 將log寫到Server存起來
PopUpAppender
InPageAppender
BrowserConsoleAppender => 將log印在console上


Level

提供以下的log等級設定
  1. log4javascript.Level.ALL
  2. log4javascript.Level.TRACE
  3. log4javascript.Level.DEBUG
  4. log4javascript.Level.INFO
  5. log4javascript.Level.WARN
  6. log4javascript.Level.ERROR
  7. log4javascript.Level.FATAL
  8. log4javascript.Level.OFF

Pattern Layout

提供以下語法供使用者客製化layout的輸出

onversion CharacterEffect
aOutputs log messages specified as an array.
Behaves exactly like %m, except that multiple log messages are assumed to have been specified in the logging call as an array rather than as multiple parameters.
Since: 1.4
cOutputs the logger name.
dOutputs the date of the logging event. The date conversion specifier may be followed by a date format specifier enclosed between braces. For example, %d{HH:mm:ss,SSS} or %d{dd MMM yyyy HH:mm:ss,SSS}. If no date format specifier is given then ISO8601 format is assumed.
The date format specifier is the same as that used by Java's SimpleDateFormat. log4javascript includes a full implementation of SimpleDateFormat's format method, with the exception of the pattern letter 'z', (string representation of the timezone) for which the information is not available in JavaScript.
fOutputs the value of a custom field set on the layout. If present, the specifier gives the index in the array of custom fields to use; otherwise, the first custom field in the array is used.
Since: 1.3
mOutputs the log messages of the logging event (i.e. the log messages supplied by the client code).
As of version 1.4, multiple log messages may be supplied to logging calls. %m displays each log message (using the rules below) one after another, separated by spaces.
As of version 1.3, an object may be specified as the log message and will be expanded to show its properties in the output, provided that a specifier containing the number of levels to expand is provided. If no specifier is provided then the message will be treated as a string regardless of its type. For example, %m{1} will display an expansion of the object one level deep, i.e. each property of the object will be displayed but if the property value is itself an object it will not be expanded and will appear as [object Object].
nOutputs a line separator.
pOutputs the level of the logging event.
rOutputs the number of milliseconds since log4javascript was initialized.
%The sequence %% outputs a single percent sign.

範例

簡單的操作方法請參考以下的gist
https://gist.githubusercontent.com/iambigd/373cbcbab4a31bb73163/raw/86b1c542859bd85df4032b5055cf4f25b35b683e/gistfile1.html



結論

用起來幾乎跟Apache log4j差不多,但目前好像找不到可以提供xml或其他設定的方式來設定appender。不過為了團隊方便使用的話,自已再包一層省略appener的設定方式會比較統一

沒有留言:

張貼留言

留個話吧:)

其他你感興趣的文章

Related Posts with Thumbnails