2011-07-12 11:03:17 +08:00
|
|
|
var dateFormat = require('./date_format')
|
2012-07-04 06:53:09 +08:00
|
|
|
, os = require('os')
|
2012-07-04 07:25:08 +08:00
|
|
|
, eol = os.EOL || '\n'
|
2012-07-04 06:44:50 +08:00
|
|
|
, util = require('util')
|
|
|
|
, replacementRegExp = /%[sdj]/g
|
|
|
|
, layoutMakers = {
|
|
|
|
"messagePassThrough": function() { return messagePassThroughLayout; }
|
|
|
|
, "basic": function() { return basicLayout; }
|
|
|
|
, "colored": function() { return colouredLayout; }
|
|
|
|
, "coloured": function() { return colouredLayout; }
|
|
|
|
, "pattern": function (config) {
|
|
|
|
var pattern = config.pattern || undefined;
|
|
|
|
return patternLayout(pattern);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
, colours = {
|
|
|
|
ALL: "grey"
|
|
|
|
, TRACE: "blue"
|
|
|
|
, DEBUG: "cyan"
|
|
|
|
, INFO: "green"
|
|
|
|
, WARN: "yellow"
|
|
|
|
, ERROR: "red"
|
|
|
|
, FATAL: "magenta"
|
|
|
|
, OFF: "grey"
|
|
|
|
};
|
2011-07-12 11:03:17 +08:00
|
|
|
|
2011-07-13 16:29:53 +08:00
|
|
|
|
2011-07-13 16:12:29 +08:00
|
|
|
function formatLogData(logData) {
|
|
|
|
var output = ""
|
2012-07-04 06:44:50 +08:00
|
|
|
, data = Array.isArray(logData) ? logData.slice() : Array.prototype.slice.call(arguments)
|
|
|
|
, format = data.shift();
|
2011-07-12 11:03:17 +08:00
|
|
|
|
2011-07-13 16:12:29 +08:00
|
|
|
if (typeof format === "string") {
|
|
|
|
output = format.replace(replacementRegExp, function(match) {
|
|
|
|
switch (match) {
|
|
|
|
case "%s": return new String(data.shift());
|
|
|
|
case "%d": return new Number(data.shift());
|
|
|
|
case "%j": return JSON.stringify(data.shift());
|
|
|
|
default:
|
|
|
|
return match;
|
|
|
|
};
|
|
|
|
});
|
2011-07-22 10:28:02 +08:00
|
|
|
} else {
|
|
|
|
//put it back, it's not a format string
|
|
|
|
data.unshift(format);
|
2011-07-12 11:03:17 +08:00
|
|
|
}
|
2011-07-13 16:12:29 +08:00
|
|
|
|
|
|
|
data.forEach(function (item) {
|
2011-09-15 06:18:24 +08:00
|
|
|
if (output) {
|
|
|
|
output += ' ';
|
|
|
|
}
|
Update lib/layouts.js
Errors sometimes carry additional attributes on them as part of the passed error data.
A utility that utilizes it, for example - is called 'errs', which is in use for instance 'nano' - the couch-db driver.
when only the stack is printed - all the additional information that is augmented on the error object does not sink to the log and is lost.
consider the following code:
```
//the oups throwing utility
function oups(){
e = new Error();
extend(
{ message : "Oups error"
, description: "huston, we got a problem"
, status : "MESS"
, errorCode : 991
, arr :[1,2,3,4,{}]
, data:
{ c:{}
, d:{e:{}}
}
}
throw e;
}
var log = require('log4js')
try{
oups()
} catch( e ) {
log.error("error on oups", e );
}
```
output before the fix
```
error on oups Error: Oups error
at repl:1:11
at REPLServer.eval (repl.js:80:21)
at Interface.<anonymous> (repl.js:182:12)
at Interface.emit (events.js:67:17)
at Interface._onLine (readline.js:162:10)
at Interface._line (readline.js:426:8)
at Interface._ttyWrite (readline.js:603:14)
at ReadStream.<anonymous> (readline.js:82:12)
at ReadStream.emit (events.js:88:20)
```
output after the fix would be
```
error on oups { [Error: My error message]
name: 'Error',
description: 'huston, we got a problem',
status: 'MESS',
errorCode: 991,
arr: [ 1, 2, 3, 4, {} ],
data: { c: {}, d: { e: {} } } }
Error: Oups error
at repl:1:11
at REPLServer.eval (repl.js:80:21)
at Interface.<anonymous> (repl.js:182:12)
at Interface.emit (events.js:67:17)
at Interface._onLine (readline.js:162:10)
at Interface._line (readline.js:426:8)
at Interface._ttyWrite (readline.js:603:14)
at ReadStream.<anonymous> (readline.js:82:12)
at ReadStream.emit (events.js:88:20)
```
2012-07-31 19:32:03 +08:00
|
|
|
output += util.inspect(item);
|
2011-07-20 17:39:54 +08:00
|
|
|
if (item && item.stack) {
|
Update lib/layouts.js
Errors sometimes carry additional attributes on them as part of the passed error data.
A utility that utilizes it, for example - is called 'errs', which is in use for instance 'nano' - the couch-db driver.
when only the stack is printed - all the additional information that is augmented on the error object does not sink to the log and is lost.
consider the following code:
```
//the oups throwing utility
function oups(){
e = new Error();
extend(
{ message : "Oups error"
, description: "huston, we got a problem"
, status : "MESS"
, errorCode : 991
, arr :[1,2,3,4,{}]
, data:
{ c:{}
, d:{e:{}}
}
}
throw e;
}
var log = require('log4js')
try{
oups()
} catch( e ) {
log.error("error on oups", e );
}
```
output before the fix
```
error on oups Error: Oups error
at repl:1:11
at REPLServer.eval (repl.js:80:21)
at Interface.<anonymous> (repl.js:182:12)
at Interface.emit (events.js:67:17)
at Interface._onLine (readline.js:162:10)
at Interface._line (readline.js:426:8)
at Interface._ttyWrite (readline.js:603:14)
at ReadStream.<anonymous> (readline.js:82:12)
at ReadStream.emit (events.js:88:20)
```
output after the fix would be
```
error on oups { [Error: My error message]
name: 'Error',
description: 'huston, we got a problem',
status: 'MESS',
errorCode: 991,
arr: [ 1, 2, 3, 4, {} ],
data: { c: {}, d: { e: {} } } }
Error: Oups error
at repl:1:11
at REPLServer.eval (repl.js:80:21)
at Interface.<anonymous> (repl.js:182:12)
at Interface.emit (events.js:67:17)
at Interface._onLine (readline.js:162:10)
at Interface._line (readline.js:426:8)
at Interface._ttyWrite (readline.js:603:14)
at ReadStream.<anonymous> (readline.js:82:12)
at ReadStream.emit (events.js:88:20)
```
2012-07-31 19:32:03 +08:00
|
|
|
output += "\n" + item.stack;
|
2011-07-13 16:12:29 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-07-12 11:03:17 +08:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Taken from masylum's fork (https://github.com/masylum/log4js-node)
|
|
|
|
*/
|
|
|
|
function colorize (str, style) {
|
|
|
|
var styles = {
|
|
|
|
//styles
|
|
|
|
'bold' : [1, 22],
|
|
|
|
'italic' : [3, 23],
|
|
|
|
'underline' : [4, 24],
|
|
|
|
'inverse' : [7, 27],
|
|
|
|
//grayscale
|
|
|
|
'white' : [37, 39],
|
|
|
|
'grey' : [90, 39],
|
|
|
|
'black' : [90, 39],
|
|
|
|
//colors
|
|
|
|
'blue' : [34, 39],
|
|
|
|
'cyan' : [36, 39],
|
|
|
|
'green' : [32, 39],
|
|
|
|
'magenta' : [35, 39],
|
|
|
|
'red' : [31, 39],
|
|
|
|
'yellow' : [33, 39]
|
|
|
|
};
|
2011-07-13 16:12:29 +08:00
|
|
|
return style ? '\033[' + styles[style][0] + 'm' + str +
|
|
|
|
'\033[' + styles[style][1] + 'm' : str;
|
|
|
|
}
|
|
|
|
|
|
|
|
function timestampLevelAndCategory(loggingEvent, colour) {
|
|
|
|
var output = colorize(
|
|
|
|
formatLogData(
|
|
|
|
'[%s] [%s] %s - '
|
|
|
|
, dateFormat.asString(loggingEvent.startTime)
|
|
|
|
, loggingEvent.level
|
|
|
|
, loggingEvent.categoryName
|
|
|
|
)
|
|
|
|
, colour
|
|
|
|
);
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* BasicLayout is a simple layout for storing the logs. The logs are stored
|
|
|
|
* in following format:
|
|
|
|
* <pre>
|
|
|
|
* [startTime] [logLevel] categoryName - message\n
|
|
|
|
* </pre>
|
|
|
|
*
|
|
|
|
* @author Stephan Strittmatter
|
|
|
|
*/
|
|
|
|
function basicLayout (loggingEvent) {
|
|
|
|
return timestampLevelAndCategory(loggingEvent) + formatLogData(loggingEvent.data);
|
2011-07-12 11:03:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* colouredLayout - taken from masylum's fork.
|
|
|
|
* same as basicLayout, but with colours.
|
|
|
|
*/
|
|
|
|
function colouredLayout (loggingEvent) {
|
2011-07-13 16:29:53 +08:00
|
|
|
return timestampLevelAndCategory(loggingEvent, colours[loggingEvent.level.toString()]) + formatLogData(loggingEvent.data);
|
2011-07-12 11:03:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function messagePassThroughLayout (loggingEvent) {
|
2011-07-13 16:12:29 +08:00
|
|
|
return formatLogData(loggingEvent.data);
|
2011-07-12 11:03:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PatternLayout
|
2011-07-15 07:13:09 +08:00
|
|
|
* 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
|
|
|
|
* - %m log data
|
|
|
|
* - %d date in various formats
|
|
|
|
* - %% %
|
|
|
|
* - %n newline
|
2011-07-12 11:03:17 +08:00
|
|
|
* Takes a pattern string and returns a layout function.
|
|
|
|
* @author Stephan Strittmatter
|
|
|
|
*/
|
|
|
|
function patternLayout (pattern) {
|
|
|
|
var TTCC_CONVERSION_PATTERN = "%r %p %c - %m%n";
|
|
|
|
var regex = /%(-?[0-9]+)?(\.?[0-9]+)?([cdmnpr%])(\{([^\}]+)\})?|([^%]+)/;
|
|
|
|
|
2011-07-13 16:42:56 +08:00
|
|
|
pattern = pattern || TTCC_CONVERSION_PATTERN;
|
2011-07-12 11:03:17 +08:00
|
|
|
|
|
|
|
return function(loggingEvent) {
|
|
|
|
var formattedString = "";
|
|
|
|
var result;
|
2011-07-13 16:42:56 +08:00
|
|
|
var searchString = pattern;
|
2011-07-12 11:03:17 +08:00
|
|
|
|
|
|
|
while ((result = regex.exec(searchString))) {
|
|
|
|
var matchedString = result[0];
|
|
|
|
var padding = result[1];
|
|
|
|
var truncation = result[2];
|
|
|
|
var conversionCharacter = result[3];
|
|
|
|
var specifier = result[5];
|
|
|
|
var text = result[6];
|
|
|
|
|
|
|
|
// Check if the pattern matched was just normal text
|
|
|
|
if (text) {
|
|
|
|
formattedString += "" + text;
|
|
|
|
} else {
|
|
|
|
// Create a raw replacement string based on the conversion
|
|
|
|
// character and specifier
|
|
|
|
var replacement = "";
|
|
|
|
switch(conversionCharacter) {
|
|
|
|
case "c":
|
|
|
|
var loggerName = loggingEvent.categoryName;
|
|
|
|
if (specifier) {
|
|
|
|
var precision = parseInt(specifier, 10);
|
|
|
|
var loggerNameBits = loggingEvent.categoryName.split(".");
|
|
|
|
if (precision >= loggerNameBits.length) {
|
|
|
|
replacement = loggerName;
|
|
|
|
} else {
|
|
|
|
replacement = loggerNameBits.slice(loggerNameBits.length - precision).join(".");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
replacement = loggerName;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "d":
|
|
|
|
var format = dateFormat.ISO8601_FORMAT;
|
|
|
|
if (specifier) {
|
|
|
|
format = specifier;
|
|
|
|
// Pick up special cases
|
|
|
|
if (format == "ISO8601") {
|
|
|
|
format = dateFormat.ISO8601_FORMAT;
|
|
|
|
} else if (format == "ABSOLUTE") {
|
|
|
|
format = dateFormat.ABSOLUTETIME_FORMAT;
|
|
|
|
} else if (format == "DATE") {
|
|
|
|
format = dateFormat.DATETIME_FORMAT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Format the date
|
|
|
|
replacement = dateFormat.asString(format, loggingEvent.startTime);
|
|
|
|
break;
|
|
|
|
case "m":
|
2011-07-13 16:42:56 +08:00
|
|
|
replacement = formatLogData(loggingEvent.data);
|
2011-07-12 11:03:17 +08:00
|
|
|
break;
|
|
|
|
case "n":
|
2012-07-04 07:25:08 +08:00
|
|
|
replacement = eol;
|
2011-07-12 11:03:17 +08:00
|
|
|
break;
|
|
|
|
case "p":
|
|
|
|
replacement = loggingEvent.level.toString();
|
|
|
|
break;
|
|
|
|
case "r":
|
|
|
|
replacement = "" + loggingEvent.startTime.toLocaleTimeString();
|
|
|
|
break;
|
|
|
|
case "%":
|
|
|
|
replacement = "%";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
replacement = matchedString;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Format the replacement according to any padding or
|
|
|
|
// truncation specified
|
|
|
|
|
|
|
|
var len;
|
|
|
|
|
|
|
|
// First, truncation
|
|
|
|
if (truncation) {
|
|
|
|
len = parseInt(truncation.substr(1), 10);
|
|
|
|
replacement = replacement.substring(0, len);
|
|
|
|
}
|
|
|
|
// Next, padding
|
|
|
|
if (padding) {
|
|
|
|
if (padding.charAt(0) == "-") {
|
|
|
|
len = parseInt(padding.substr(1), 10);
|
|
|
|
// Right pad with spaces
|
|
|
|
while (replacement.length < len) {
|
|
|
|
replacement += " ";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
len = parseInt(padding, 10);
|
|
|
|
// Left pad with spaces
|
|
|
|
while (replacement.length < len) {
|
|
|
|
replacement = " " + replacement;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
formattedString += replacement;
|
|
|
|
}
|
|
|
|
searchString = searchString.substr(result.index + result[0].length);
|
|
|
|
}
|
|
|
|
return formattedString;
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
basicLayout: basicLayout
|
|
|
|
, messagePassThroughLayout: messagePassThroughLayout
|
|
|
|
, patternLayout: patternLayout
|
|
|
|
, colouredLayout: colouredLayout
|
|
|
|
, coloredLayout: colouredLayout
|
|
|
|
, layout: function(name, config) {
|
|
|
|
return layoutMakers[name] && layoutMakers[name](config);
|
|
|
|
}
|
2012-07-04 06:44:50 +08:00
|
|
|
};
|