2013-06-04 06:17:36 +08:00
|
|
|
"use strict";
|
2011-07-12 11:03:17 +08:00
|
|
|
var dateFormat = require('./date_format')
|
2013-06-04 06:17:36 +08:00
|
|
|
, os = require('os')
|
|
|
|
, eol = os.EOL || '\n'
|
|
|
|
, 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;
|
|
|
|
var tokens = config.tokens || undefined;
|
|
|
|
return patternLayout(pattern, tokens);
|
2013-02-08 21:54:18 +08:00
|
|
|
}
|
2013-06-04 06:17:36 +08:00
|
|
|
}
|
|
|
|
, 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:12:29 +08:00
|
|
|
function formatLogData(logData) {
|
2013-06-04 06:17:36 +08:00
|
|
|
var output = ""
|
|
|
|
, data = Array.isArray(logData) ? logData.slice() : Array.prototype.slice.call(arguments)
|
|
|
|
, format = data.shift();
|
|
|
|
|
|
|
|
if (typeof format === "string") {
|
|
|
|
output = format.replace(replacementRegExp, function(match) {
|
|
|
|
switch (match) {
|
|
|
|
case "%s":
|
|
|
|
return data.shift().toString();
|
|
|
|
case "%d":
|
|
|
|
return new Number(data.shift());
|
|
|
|
case "%j":
|
|
|
|
return JSON.stringify(data.shift());
|
|
|
|
default:
|
|
|
|
return match;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
//put it back, it's not a format string
|
|
|
|
data.unshift(format);
|
|
|
|
}
|
2011-07-12 11:03:17 +08:00
|
|
|
|
2013-06-04 06:17:36 +08:00
|
|
|
data.forEach(function (item) {
|
|
|
|
if (output) {
|
|
|
|
output += ' ';
|
2011-07-12 11:03:17 +08:00
|
|
|
}
|
2013-06-04 06:17:36 +08:00
|
|
|
output += util.inspect(item);
|
|
|
|
if (item && item.stack) {
|
|
|
|
output += "\n" + item.stack;
|
|
|
|
}
|
|
|
|
});
|
2011-07-13 16:12:29 +08:00
|
|
|
|
2013-06-04 06:17:36 +08:00
|
|
|
return output;
|
2011-07-12 11:03:17 +08:00
|
|
|
}
|
|
|
|
|
2012-11-23 05:14:13 +08:00
|
|
|
var styles = {
|
|
|
|
//styles
|
2013-06-04 06:17:36 +08:00
|
|
|
'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]
|
2012-11-23 05:14:13 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
function colorizeStart(style) {
|
2013-06-04 06:17:36 +08:00
|
|
|
return style ? '\x1B[' + styles[style][0] + 'm' : '';
|
2012-11-23 05:14:13 +08:00
|
|
|
}
|
|
|
|
function colorizeEnd(style) {
|
2013-06-04 06:17:36 +08:00
|
|
|
return style ? '\x1B[' + styles[style][1] + 'm' : '';
|
2012-11-23 05:14:13 +08:00
|
|
|
}
|
2011-07-12 11:03:17 +08:00
|
|
|
/**
|
|
|
|
* Taken from masylum's fork (https://github.com/masylum/log4js-node)
|
|
|
|
*/
|
|
|
|
function colorize (str, style) {
|
2013-06-04 06:17:36 +08:00
|
|
|
return colorizeStart(style) + str + colorizeEnd(style);
|
2011-07-13 16:12:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function timestampLevelAndCategory(loggingEvent, colour) {
|
2013-06-04 06:17:36 +08:00
|
|
|
var output = colorize(
|
|
|
|
formatLogData(
|
|
|
|
'[%s] [%s] %s - '
|
|
|
|
, dateFormat.asString(loggingEvent.startTime)
|
|
|
|
, loggingEvent.level
|
|
|
|
, loggingEvent.categoryName
|
|
|
|
)
|
|
|
|
, colour
|
|
|
|
);
|
|
|
|
return output;
|
2011-07-13 16:12:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2013-06-04 06:17:36 +08:00
|
|
|
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) {
|
2013-06-04 06:17:36 +08:00
|
|
|
return timestampLevelAndCategory(
|
|
|
|
loggingEvent,
|
|
|
|
colours[loggingEvent.level.toString()]
|
|
|
|
) + formatLogData(loggingEvent.data);
|
2011-07-12 11:03:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function messagePassThroughLayout (loggingEvent) {
|
2013-06-04 06:17:36 +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
|
2013-02-08 23:15:51 +08:00
|
|
|
* - %x{<tokenname>} add dynamic tokens to your log. Tokens are specified in the tokens parameter
|
2012-11-23 05:14:13 +08:00
|
|
|
* You can use %[ and %] to define a colored block.
|
2013-02-08 23:15:51 +08:00
|
|
|
*
|
|
|
|
* 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}
|
2011-07-12 11:03:17 +08:00
|
|
|
* @author Stephan Strittmatter
|
2013-02-08 23:15:51 +08:00
|
|
|
* @author Jan Schmidle
|
2011-07-12 11:03:17 +08:00
|
|
|
*/
|
2013-02-08 21:54:18 +08:00
|
|
|
function patternLayout (pattern, tokens) {
|
2013-06-04 06:17:36 +08:00
|
|
|
var TTCC_CONVERSION_PATTERN = "%r %p %c - %m%n";
|
|
|
|
var regex = /%(-?[0-9]+)?(\.?[0-9]+)?([\[\]cdmnprx%])(\{([^\}]+)\})?|([^%]+)/;
|
|
|
|
|
|
|
|
pattern = pattern || TTCC_CONVERSION_PATTERN;
|
2013-06-05 06:02:10 +08:00
|
|
|
|
|
|
|
function categoryName(loggingEvent, specifier) {
|
|
|
|
var loggerName = loggingEvent.categoryName;
|
|
|
|
if (specifier) {
|
|
|
|
var precision = parseInt(specifier, 10);
|
|
|
|
var loggerNameBits = loggerName.split(".");
|
|
|
|
if (precision < loggerNameBits.length) {
|
|
|
|
loggerName = loggerNameBits.slice(loggerNameBits.length - precision).join(".");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return loggerName;
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatAsDate(loggingEvent, specifier) {
|
|
|
|
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
|
|
|
|
return dateFormat.asString(format, loggingEvent.startTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatMessage(loggingEvent) {
|
|
|
|
return formatLogData(loggingEvent.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
function endOfLine() {
|
|
|
|
return eol;
|
|
|
|
}
|
|
|
|
|
|
|
|
function logLevel(loggingEvent) {
|
|
|
|
return loggingEvent.level.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
function startTime(loggingEvent) {
|
|
|
|
return "" + loggingEvent.startTime.toLocaleTimeString();
|
|
|
|
}
|
|
|
|
|
|
|
|
function startColour(loggingEvent) {
|
|
|
|
return colorizeStart(colours[loggingEvent.level.toString()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
function endColour(loggingEvent) {
|
|
|
|
return colorizeEnd(colours[loggingEvent.level.toString()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
function percent() {
|
|
|
|
return '%';
|
|
|
|
}
|
|
|
|
|
|
|
|
function userDefined(loggingEvent, specifier) {
|
|
|
|
if (typeof(tokens[specifier]) !== 'undefined') {
|
|
|
|
if (typeof(tokens[specifier]) === 'function') {
|
|
|
|
return tokens[specifier](loggingEvent);
|
|
|
|
} else {
|
|
|
|
return tokens[specifier];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var replacers = {
|
|
|
|
'c': categoryName,
|
|
|
|
'd': formatAsDate,
|
|
|
|
'm': formatMessage,
|
|
|
|
'n': endOfLine,
|
|
|
|
'p': logLevel,
|
|
|
|
'r': startTime,
|
|
|
|
'[': startColour,
|
|
|
|
']': endColour,
|
|
|
|
'%': percent,
|
|
|
|
'x': userDefined
|
|
|
|
};
|
|
|
|
|
|
|
|
function replaceToken(conversionCharacter, loggingEvent, specifier) {
|
|
|
|
var replacer = replacers[conversionCharacter];
|
|
|
|
if (replacer) {
|
|
|
|
return replacer(loggingEvent, specifier);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function truncate(truncation, toTruncate) {
|
|
|
|
var len;
|
|
|
|
if (truncation) {
|
|
|
|
len = parseInt(truncation.substr(1), 10);
|
|
|
|
return toTruncate.substring(0, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
return toTruncate;
|
|
|
|
}
|
|
|
|
|
|
|
|
function pad(padding, toPad) {
|
|
|
|
var len;
|
|
|
|
if (padding) {
|
|
|
|
if (padding.charAt(0) == "-") {
|
|
|
|
len = parseInt(padding.substr(1), 10);
|
|
|
|
// Right pad with spaces
|
|
|
|
while (toPad.length < len) {
|
|
|
|
toPad += " ";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
len = parseInt(padding, 10);
|
|
|
|
// Left pad with spaces
|
|
|
|
while (toPad.length < len) {
|
|
|
|
toPad = " " + toPad;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return toPad;
|
|
|
|
}
|
2013-06-04 06:17:36 +08:00
|
|
|
|
|
|
|
return function(loggingEvent) {
|
|
|
|
var formattedString = "";
|
|
|
|
var result;
|
|
|
|
var searchString = pattern;
|
|
|
|
|
|
|
|
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
|
2013-06-05 06:02:10 +08:00
|
|
|
var replacement =
|
|
|
|
replaceToken(conversionCharacter, loggingEvent, specifier) ||
|
|
|
|
matchedString;
|
|
|
|
|
2013-06-04 06:17:36 +08:00
|
|
|
// Format the replacement according to any padding or
|
|
|
|
// truncation specified
|
2013-06-05 06:02:10 +08:00
|
|
|
replacement = truncate(truncation, replacement);
|
|
|
|
replacement = pad(padding, replacement);
|
2013-06-04 06:17:36 +08:00
|
|
|
formattedString += replacement;
|
|
|
|
}
|
|
|
|
searchString = searchString.substr(result.index + result[0].length);
|
|
|
|
}
|
|
|
|
return formattedString;
|
|
|
|
};
|
2011-07-12 11:03:17 +08:00
|
|
|
|
2013-06-04 06:17:36 +08:00
|
|
|
}
|
2011-07-12 11:03:17 +08:00
|
|
|
|
|
|
|
module.exports = {
|
2013-06-04 06:17:36 +08:00
|
|
|
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
|
|
|
};
|