mirror of
https://github.com/ONLYOFFICE/server.git
synced 2026-04-07 14:04:35 +08:00
Add JSON logger
This commit is contained in:
@ -35,6 +35,32 @@
|
||||
var config = require('config');
|
||||
|
||||
var log4js = require('log4js');
|
||||
|
||||
// https://stackoverflow.com/a/36643588
|
||||
var dateToJSONWithTZ = function (d) {
|
||||
var timezoneOffsetInHours = -(d.getTimezoneOffset() / 60); //UTC minus local time
|
||||
var sign = timezoneOffsetInHours >= 0 ? '+' : '-';
|
||||
var leadingZero = (Math.abs(timezoneOffsetInHours) < 10) ? '0' : '';
|
||||
|
||||
//It's a bit unfortunate that we need to construct a new Date instance
|
||||
//(we don't want _d_ Date instance to be modified)
|
||||
var correctedDate = new Date(d.getFullYear(), d.getMonth(),
|
||||
d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds(),
|
||||
d.getMilliseconds());
|
||||
correctedDate.setHours(d.getHours() + timezoneOffsetInHours);
|
||||
var iso = correctedDate.toISOString().replace('Z', '');
|
||||
return iso + sign + leadingZero + Math.abs(timezoneOffsetInHours).toString() + ':00';
|
||||
};
|
||||
|
||||
log4js.addLayout('json', function(config) {
|
||||
return function(logEvent) {
|
||||
logEvent['startTime'] = dateToJSONWithTZ(logEvent['startTime']);
|
||||
logEvent['message'] = util.format(...logEvent['data']);
|
||||
delete logEvent['data'];
|
||||
return JSON.stringify(logEvent);
|
||||
}
|
||||
});
|
||||
|
||||
log4js.configure(config.get('log.filePath'));
|
||||
|
||||
var logger = log4js.getLogger('nodeJS');
|
||||
|
||||
Reference in New Issue
Block a user