Files
server/Common/sources/logger.js
2023-03-01 21:16:52 +03:00

100 lines
3.9 KiB
JavaScript

/*
* (c) Copyright Ascensio System SIA 2010-2023
*
* This program is a free software product. You can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License (AGPL)
* version 3 as published by the Free Software Foundation. In accordance with
* Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect
* that Ascensio System SIA expressly excludes the warranty of non-infringement
* of any third-party rights.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
* details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
*
* You can contact Ascensio System SIA at 20A-6 Ernesta Birznieka-Upish
* street, Riga, Latvia, EU, LV-1050.
*
* The interactive user interfaces in modified source and object code versions
* of the Program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU AGPL version 3.
*
* Pursuant to Section 7(b) of the License you must retain the original Product
* logo when distributing the program. Pursuant to Section 7(e) we decline to
* grant you any rights under trademark law for use of our trademarks.
*
* All the Product's GUI elements, including illustrations and icon sets, as
* well as technical writing content are licensed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International. See the License
* terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
*
*/
'use strict';
var config = require('config');
var util = require('util');
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');
if (config.get('log.options.replaceConsole')) {
console.log = logger.info.bind(logger);
console.info = logger.info.bind(logger);
console.warn = logger.warn.bind(logger);
console.error = logger.error.bind(logger);
console.debug = logger.debug.bind(logger);
}
exports.getLogger = function (){
return log4js.getLogger.apply(log4js, Array.prototype.slice.call(arguments));
};
exports.trace = function (){
return logger.trace.apply(logger, Array.prototype.slice.call(arguments));
};
exports.debug = function (){
return logger.debug.apply(logger, Array.prototype.slice.call(arguments));
};
exports.info = function (){
return logger.info.apply(logger, Array.prototype.slice.call(arguments));
};
exports.warn = function (){
return logger.warn.apply(logger, Array.prototype.slice.call(arguments));
};
exports.error = function (){
return logger.error.apply(logger, Array.prototype.slice.call(arguments));
};
exports.fatal = function (){
return logger.fatal.apply(logger, Array.prototype.slice.call(arguments));
};
exports.shutdown = function (callback) {
return log4js.shutdown(callback);
};