Merge pull request #282 from qbrandon/master
Add appender-level timezone offset config
This commit is contained in:
commit
8360bb732c
@ -2,10 +2,10 @@
|
|||||||
var layouts = require('../layouts')
|
var layouts = require('../layouts')
|
||||||
, consoleLog = console.log.bind(console);
|
, consoleLog = console.log.bind(console);
|
||||||
|
|
||||||
function consoleAppender (layout) {
|
function consoleAppender (layout, timezoneOffset) {
|
||||||
layout = layout || layouts.colouredLayout;
|
layout = layout || layouts.colouredLayout;
|
||||||
return function(loggingEvent) {
|
return function(loggingEvent) {
|
||||||
consoleLog(layout(loggingEvent));
|
consoleLog(layout(loggingEvent, timezoneOffset));
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -14,7 +14,7 @@ function configure(config) {
|
|||||||
if (config.layout) {
|
if (config.layout) {
|
||||||
layout = layouts.layout(config.layout.type, config.layout);
|
layout = layouts.layout(config.layout.type, config.layout);
|
||||||
}
|
}
|
||||||
return consoleAppender(layout);
|
return consoleAppender(layout, config.timezoneOffset);
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.appender = consoleAppender;
|
exports.appender = consoleAppender;
|
||||||
|
@ -20,8 +20,9 @@ process.on('exit', function() {
|
|||||||
* @pattern the format that will be added to the end of filename when rolling,
|
* @pattern the format that will be added to the end of filename when rolling,
|
||||||
* also used to check when to roll files - defaults to '.yyyy-MM-dd'
|
* also used to check when to roll files - defaults to '.yyyy-MM-dd'
|
||||||
* @layout layout function for log messages - defaults to basicLayout
|
* @layout layout function for log messages - defaults to basicLayout
|
||||||
|
* @timezoneOffset optional timezone offset in minutes - defaults to system local
|
||||||
*/
|
*/
|
||||||
function appender(filename, pattern, alwaysIncludePattern, layout) {
|
function appender(filename, pattern, alwaysIncludePattern, layout, timezoneOffset) {
|
||||||
layout = layout || layouts.basicLayout;
|
layout = layout || layouts.basicLayout;
|
||||||
|
|
||||||
var logFile = new streams.DateRollingFileStream(
|
var logFile = new streams.DateRollingFileStream(
|
||||||
@ -32,7 +33,7 @@ function appender(filename, pattern, alwaysIncludePattern, layout) {
|
|||||||
openFiles.push(logFile);
|
openFiles.push(logFile);
|
||||||
|
|
||||||
return function(logEvent) {
|
return function(logEvent) {
|
||||||
logFile.write(layout(logEvent) + eol, "utf8");
|
logFile.write(layout(logEvent, timezoneOffset) + eol, "utf8");
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -52,7 +53,7 @@ function configure(config, options) {
|
|||||||
config.filename = path.join(options.cwd, config.filename);
|
config.filename = path.join(options.cwd, config.filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
return appender(config.filename, config.pattern, config.alwaysIncludePattern, layout);
|
return appender(config.filename, config.pattern, config.alwaysIncludePattern, layout, config.timezoneOffset);
|
||||||
}
|
}
|
||||||
|
|
||||||
function shutdown(cb) {
|
function shutdown(cb) {
|
||||||
|
@ -26,8 +26,10 @@ process.on('exit', function() {
|
|||||||
* if not provided then logs won't be rotated.
|
* if not provided then logs won't be rotated.
|
||||||
* @param numBackups - the number of log files to keep after logSize
|
* @param numBackups - the number of log files to keep after logSize
|
||||||
* has been reached (default 5)
|
* has been reached (default 5)
|
||||||
|
* @param compress - flag that controls log file compression
|
||||||
|
* @param timezoneOffset - optional timezone offset in minutes (default system local)
|
||||||
*/
|
*/
|
||||||
function fileAppender (file, layout, logSize, numBackups, compress) {
|
function fileAppender (file, layout, logSize, numBackups, compress, timezoneOffset) {
|
||||||
var bytesWritten = 0;
|
var bytesWritten = 0;
|
||||||
file = path.normalize(file);
|
file = path.normalize(file);
|
||||||
layout = layout || layouts.basicLayout;
|
layout = layout || layouts.basicLayout;
|
||||||
@ -64,7 +66,7 @@ function fileAppender (file, layout, logSize, numBackups, compress) {
|
|||||||
openFiles.push(logFile);
|
openFiles.push(logFile);
|
||||||
|
|
||||||
return function(loggingEvent) {
|
return function(loggingEvent) {
|
||||||
logFile.write(layout(loggingEvent) + eol, "utf8");
|
logFile.write(layout(loggingEvent, timezoneOffset) + eol, "utf8");
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -79,7 +81,7 @@ function configure(config, options) {
|
|||||||
config.filename = path.join(options.cwd, config.filename);
|
config.filename = path.join(options.cwd, config.filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
return fileAppender(config.filename, layout, config.maxLogSize, config.backups, config.compress);
|
return fileAppender(config.filename, layout, config.maxLogSize, config.backups, config.compress, config.timezoneOffset);
|
||||||
}
|
}
|
||||||
|
|
||||||
function shutdown(cb) {
|
function shutdown(cb) {
|
||||||
|
@ -127,8 +127,10 @@ RollingFileSync.prototype.write = function(chunk, encoding) {
|
|||||||
* if not provided then logs won't be rotated.
|
* if not provided then logs won't be rotated.
|
||||||
* @param numBackups - the number of log files to keep after logSize
|
* @param numBackups - the number of log files to keep after logSize
|
||||||
* has been reached (default 5)
|
* has been reached (default 5)
|
||||||
|
* @param timezoneOffset - optional timezone offset in minutes
|
||||||
|
* (default system local)
|
||||||
*/
|
*/
|
||||||
function fileAppender (file, layout, logSize, numBackups) {
|
function fileAppender (file, layout, logSize, numBackups, timezoneOffset) {
|
||||||
debug("fileSync appender created");
|
debug("fileSync appender created");
|
||||||
var bytesWritten = 0;
|
var bytesWritten = 0;
|
||||||
file = path.normalize(file);
|
file = path.normalize(file);
|
||||||
@ -166,7 +168,7 @@ function fileAppender (file, layout, logSize, numBackups) {
|
|||||||
var logFile = openTheStream(file, logSize, numBackups);
|
var logFile = openTheStream(file, logSize, numBackups);
|
||||||
|
|
||||||
return function(loggingEvent) {
|
return function(loggingEvent) {
|
||||||
logFile.write(layout(loggingEvent) + eol);
|
logFile.write(layout(loggingEvent, timezoneOffset) + eol);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -180,7 +182,7 @@ function configure(config, options) {
|
|||||||
config.filename = path.join(options.cwd, config.filename);
|
config.filename = path.join(options.cwd, config.filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
return fileAppender(config.filename, layout, config.maxLogSize, config.backups);
|
return fileAppender(config.filename, layout, config.maxLogSize, config.backups, config.timezoneOffset);
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.appender = fileAppender;
|
exports.appender = fileAppender;
|
||||||
|
@ -28,7 +28,7 @@ function smtpAppender(config, layout) {
|
|||||||
var firstEvent = logEventBuffer[0];
|
var firstEvent = logEventBuffer[0];
|
||||||
var body = "";
|
var body = "";
|
||||||
while (logEventBuffer.length > 0) {
|
while (logEventBuffer.length > 0) {
|
||||||
body += layout(logEventBuffer.shift()) + "\n";
|
body += layout(logEventBuffer.shift(), config.timezoneOffset) + "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
var msg = {
|
var msg = {
|
||||||
|
@ -21,9 +21,9 @@ function addZero(vNumber) {
|
|||||||
* Thanks to http://www.svendtofte.com/code/date_format/
|
* Thanks to http://www.svendtofte.com/code/date_format/
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
function offset(date) {
|
function offset(timezoneOffset) {
|
||||||
// Difference to Greenwich time (GMT) in hours
|
// Difference to Greenwich time (GMT) in hours
|
||||||
var os = Math.abs(date.getTimezoneOffset());
|
var os = Math.abs(timezoneOffset);
|
||||||
var h = String(Math.floor(os/60));
|
var h = String(Math.floor(os/60));
|
||||||
var m = String(os%60);
|
var m = String(os%60);
|
||||||
if (h.length == 1) {
|
if (h.length == 1) {
|
||||||
@ -32,26 +32,32 @@ function offset(date) {
|
|||||||
if (m.length == 1) {
|
if (m.length == 1) {
|
||||||
m = "0" + m;
|
m = "0" + m;
|
||||||
}
|
}
|
||||||
return date.getTimezoneOffset() < 0 ? "+"+h+m : "-"+h+m;
|
return timezoneOffset < 0 ? "+"+h+m : "-"+h+m;
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.asString = function(/*format,*/ date) {
|
exports.asString = function(/*format,*/ date, timezoneOffset) {
|
||||||
var format = exports.ISO8601_FORMAT;
|
var format = exports.ISO8601_FORMAT;
|
||||||
if (typeof(date) === "string") {
|
if (typeof(date) === "string") {
|
||||||
format = arguments[0];
|
format = arguments[0];
|
||||||
date = arguments[1];
|
date = arguments[1];
|
||||||
|
timezoneOffset = arguments[2];
|
||||||
}
|
}
|
||||||
|
// make the date independent of the system timezone by working with UTC
|
||||||
var vDay = addZero(date.getDate());
|
if (timezoneOffset === undefined) {
|
||||||
var vMonth = addZero(date.getMonth()+1);
|
timezoneOffset = date.getTimezoneOffset();
|
||||||
var vYearLong = addZero(date.getFullYear());
|
}
|
||||||
var vYearShort = addZero(date.getFullYear().toString().substring(2,4));
|
date.setUTCMinutes(date.getUTCMinutes() - timezoneOffset);
|
||||||
|
var vDay = addZero(date.getUTCDate());
|
||||||
|
var vMonth = addZero(date.getUTCMonth()+1);
|
||||||
|
var vYearLong = addZero(date.getUTCFullYear());
|
||||||
|
var vYearShort = addZero(date.getUTCFullYear().toString().substring(2,4));
|
||||||
var vYear = (format.indexOf("yyyy") > -1 ? vYearLong : vYearShort);
|
var vYear = (format.indexOf("yyyy") > -1 ? vYearLong : vYearShort);
|
||||||
var vHour = addZero(date.getHours());
|
var vHour = addZero(date.getUTCHours());
|
||||||
var vMinute = addZero(date.getMinutes());
|
var vMinute = addZero(date.getUTCMinutes());
|
||||||
var vSecond = addZero(date.getSeconds());
|
var vSecond = addZero(date.getUTCSeconds());
|
||||||
var vMillisecond = padWithZeros(date.getMilliseconds(), 3);
|
var vMillisecond = padWithZeros(date.getUTCMilliseconds(), 3);
|
||||||
var vTimeZone = offset(date);
|
var vTimeZone = offset(timezoneOffset);
|
||||||
|
date.setUTCMinutes(date.getUTCMinutes() + timezoneOffset);
|
||||||
var formatted = format
|
var formatted = format
|
||||||
.replace(/dd/g, vDay)
|
.replace(/dd/g, vDay)
|
||||||
.replace(/MM/g, vMonth)
|
.replace(/MM/g, vMonth)
|
||||||
|
@ -71,11 +71,11 @@ function colorize (str, style) {
|
|||||||
return colorizeStart(style) + str + colorizeEnd(style);
|
return colorizeStart(style) + str + colorizeEnd(style);
|
||||||
}
|
}
|
||||||
|
|
||||||
function timestampLevelAndCategory(loggingEvent, colour) {
|
function timestampLevelAndCategory(loggingEvent, colour, timezoneOffest) {
|
||||||
var output = colorize(
|
var output = colorize(
|
||||||
formatLogData(
|
formatLogData(
|
||||||
'[%s] [%s] %s - '
|
'[%s] [%s] %s - '
|
||||||
, dateFormat.asString(loggingEvent.startTime)
|
, dateFormat.asString(loggingEvent.startTime, timezoneOffest)
|
||||||
, loggingEvent.level
|
, loggingEvent.level
|
||||||
, loggingEvent.categoryName
|
, loggingEvent.categoryName
|
||||||
)
|
)
|
||||||
@ -93,18 +93,19 @@ function timestampLevelAndCategory(loggingEvent, colour) {
|
|||||||
*
|
*
|
||||||
* @author Stephan Strittmatter
|
* @author Stephan Strittmatter
|
||||||
*/
|
*/
|
||||||
function basicLayout (loggingEvent) {
|
function basicLayout (loggingEvent, timezoneOffset) {
|
||||||
return timestampLevelAndCategory(loggingEvent) + formatLogData(loggingEvent.data);
|
return timestampLevelAndCategory(loggingEvent, undefined, timezoneOffset) + formatLogData(loggingEvent.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* colouredLayout - taken from masylum's fork.
|
* colouredLayout - taken from masylum's fork.
|
||||||
* same as basicLayout, but with colours.
|
* same as basicLayout, but with colours.
|
||||||
*/
|
*/
|
||||||
function colouredLayout (loggingEvent) {
|
function colouredLayout (loggingEvent, timezoneOffset) {
|
||||||
return timestampLevelAndCategory(
|
return timestampLevelAndCategory(
|
||||||
loggingEvent,
|
loggingEvent,
|
||||||
colours[loggingEvent.level.toString()]
|
colours[loggingEvent.level.toString()],
|
||||||
|
timezoneOffset
|
||||||
) + formatLogData(loggingEvent.data);
|
) + formatLogData(loggingEvent.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -139,11 +140,12 @@ function messagePassThroughLayout (loggingEvent) {
|
|||||||
* Takes a pattern string, array of tokens and returns a layout function.
|
* Takes a pattern string, array of tokens and returns a layout function.
|
||||||
* @param {String} Log format pattern String
|
* @param {String} Log format pattern String
|
||||||
* @param {object} map object of different tokens
|
* @param {object} map object of different tokens
|
||||||
|
* @param {number} timezone offset in minutes
|
||||||
* @return {Function}
|
* @return {Function}
|
||||||
* @author Stephan Strittmatter
|
* @author Stephan Strittmatter
|
||||||
* @author Jan Schmidle
|
* @author Jan Schmidle
|
||||||
*/
|
*/
|
||||||
function patternLayout (pattern, tokens) {
|
function patternLayout (pattern, tokens, timezoneOffset) {
|
||||||
var TTCC_CONVERSION_PATTERN = "%r %p %c - %m%n";
|
var TTCC_CONVERSION_PATTERN = "%r %p %c - %m%n";
|
||||||
var regex = /%(-?[0-9]+)?(\.?[0-9]+)?([\[\]cdhmnprzxy%])(\{([^\}]+)\})?|([^%]+)/;
|
var regex = /%(-?[0-9]+)?(\.?[0-9]+)?([\[\]cdhmnprzxy%])(\{([^\}]+)\})?|([^%]+)/;
|
||||||
|
|
||||||
@ -177,7 +179,7 @@ function patternLayout (pattern, tokens) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Format the date
|
// Format the date
|
||||||
return dateFormat.asString(format, loggingEvent.startTime);
|
return dateFormat.asString(format, loggingEvent.startTime, timezoneOffset);
|
||||||
}
|
}
|
||||||
|
|
||||||
function hostname() {
|
function hostname() {
|
||||||
@ -197,7 +199,7 @@ function patternLayout (pattern, tokens) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function startTime(loggingEvent) {
|
function startTime(loggingEvent) {
|
||||||
return "" + loggingEvent.startTime.toLocaleTimeString();
|
return dateFormat.asString('hh:mm:ss', loggingEvent.startTime, timezoneOffset);
|
||||||
}
|
}
|
||||||
|
|
||||||
function startColour(loggingEvent) {
|
function startColour(loggingEvent) {
|
||||||
|
@ -3,11 +3,13 @@ var vows = require('vows')
|
|||||||
, assert = require('assert')
|
, assert = require('assert')
|
||||||
, dateFormat = require('../lib/date_format');
|
, dateFormat = require('../lib/date_format');
|
||||||
|
|
||||||
|
function createFixedDate() {
|
||||||
|
return new Date(2010, 0, 11, 14, 31, 30, 5);
|
||||||
|
}
|
||||||
|
|
||||||
vows.describe('date_format').addBatch({
|
vows.describe('date_format').addBatch({
|
||||||
'Date extensions': {
|
'Date extensions': {
|
||||||
topic: function() {
|
topic: createFixedDate,
|
||||||
return new Date(2010, 0, 11, 14, 31, 30, 5);
|
|
||||||
},
|
|
||||||
'should format a date as string using a pattern': function(date) {
|
'should format a date as string using a pattern': function(date) {
|
||||||
assert.equal(
|
assert.equal(
|
||||||
dateFormat.asString(dateFormat.DATETIME_FORMAT, date),
|
dateFormat.asString(dateFormat.DATETIME_FORMAT, date),
|
||||||
@ -20,13 +22,16 @@ vows.describe('date_format').addBatch({
|
|||||||
'2010-01-11 14:31:30.005'
|
'2010-01-11 14:31:30.005'
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
'should provide a ISO8601 with timezone offset format': function(date) {
|
'should provide a ISO8601 with timezone offset format': function() {
|
||||||
|
var date = createFixedDate();
|
||||||
|
date.setMinutes(date.getMinutes() - date.getTimezoneOffset() - 660);
|
||||||
date.getTimezoneOffset = function() { return -660; };
|
date.getTimezoneOffset = function() { return -660; };
|
||||||
assert.equal(
|
assert.equal(
|
||||||
dateFormat.asString(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, date),
|
dateFormat.asString(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, date),
|
||||||
"2010-01-11T14:31:30+1100"
|
"2010-01-11T14:31:30+1100"
|
||||||
);
|
);
|
||||||
|
date = createFixedDate();
|
||||||
|
date.setMinutes(date.getMinutes() - date.getTimezoneOffset() + 120);
|
||||||
date.getTimezoneOffset = function() { return 120; };
|
date.getTimezoneOffset = function() { return 120; };
|
||||||
assert.equal(
|
assert.equal(
|
||||||
dateFormat.asString(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, date),
|
dateFormat.asString(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, date),
|
||||||
@ -40,7 +45,9 @@ vows.describe('date_format').addBatch({
|
|||||||
'14:31:30.005'
|
'14:31:30.005'
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
'should provide a custom format': function(date) {
|
'should provide a custom format': function() {
|
||||||
|
var date = createFixedDate();
|
||||||
|
date.setMinutes(date.getMinutes() - date.getTimezoneOffset() + 120);
|
||||||
date.getTimezoneOffset = function() { return 120; };
|
date.getTimezoneOffset = function() { return 120; };
|
||||||
assert.equal(
|
assert.equal(
|
||||||
dateFormat.asString("O.SSS.ss.mm.hh.dd.MM.yy", date),
|
dateFormat.asString("O.SSS.ss.mm.hh.dd.MM.yy", date),
|
||||||
|
@ -179,7 +179,7 @@ vows.describe('log4js layouts').addBatch({
|
|||||||
topic: function() {
|
topic: function() {
|
||||||
var event = {
|
var event = {
|
||||||
data: ['this is a test'],
|
data: ['this is a test'],
|
||||||
startTime: new Date(2010, 11, 5, 14, 18, 30, 45),
|
startTime: new Date('2010-12-05T14:18:30.045Z'), //new Date(2010, 11, 5, 14, 18, 30, 45),
|
||||||
categoryName: "multiple.levels.of.tests",
|
categoryName: "multiple.levels.of.tests",
|
||||||
level: {
|
level: {
|
||||||
toString: function() { return "DEBUG"; }
|
toString: function() { return "DEBUG"; }
|
||||||
|
Loading…
Reference in New Issue
Block a user