Add optional timezoneOffset config for appenders
Example: log4js.configure({ appenders: [{type: 'console', timezoneOffset: -540}], replaceConsole: true }); The expected value is the equivalent of (new Date).getTimezoneOffset() In this example, -540 is the value for JST. This allows machines members of world-wide-spread cluster to all report log time-stamps using the same timezone (or adapt the timezone to a local different from the system)
This commit is contained in:
parent
4a217afc37
commit
af69eddd1c
@ -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,31 @@ 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);
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user