[node.js] log4js PatternLayout 사용하기


셈플코드

var log4js = require("log4js");
/**
 * PatternLayout
 * Format for specifiers is %[padding].[truncation][field]{[format]}
 * e.g. %5.10p - left pad the log level by 5 characters, up to a max of 10
 * Fields can be any of:
 * - %r time in toLocaleTimeString format
 * - %p log level
 * - %c log category
 * - %h hostname
 * - %m log data
 * - %d date in various formats
 * - %% %
 * - %n newline
 * - %x{<tokenname>} add dynamic tokens to your log. Tokens are specified in the tokens parameter
 * You can use %[ and %] to define a colored block.
 *
 * Tokens are specified as simple key:value objects. 
 * The key represents the token name whereas the value can be a string or function
 * which is called to extract the value to put in the log message. If token is not
 * found, it doesn't replace the field.
 *
 * A sample token would be: { "pid" : function() { return process.pid; } }
 *
 * Takes a pattern string, array of tokens and returns a layout function.
 * @param {String} Log format pattern String
 * @param {object} map object of different tokens
 * @return {Function}
 * @author Stephan Strittmatter
 * @author Jan Schmidle
 */

log4js.configure({
 appenders:[
 {
 type : "console", 
 layout: {
 type : "pattern",
 pattern:"%d %x{pid} %0.1p %c - %m",
 tokens :{ "pid" : function() { return process.pid; } }
 }
 }
 ]
}); 
log4js.replaceConsole();

console.log("test %s", "vv");

참고

"JavaScript / Node.JS" 분류의 다른 글

[node.js] ajax로 타 도메인과 연동 문제 / CORS(Cross-Origin Resource Sharing) (0)2013/12/20
[node.js] cluster를 이용한 멀티코어(multi-core) 사용 (0)2013/10/22
[node.js] traceback (0)2013/10/22
[node.js] Strict mode (0)2013/10/21


Powered by Textcube