This commit is contained in:
Andris Reinman 2018-10-18 11:11:53 +03:00
parent 8a52635171
commit d25e0180b7

View file

@ -2,16 +2,78 @@
'use strict';
const config = require('wild-config');
const Gelf = require('gelf');
const os = require('os');
let bugsnag;
let gelf;
let loggelf;
let component;
let hostname;
if (config.bugsnagCode) {
bugsnag = require('bugsnag');
bugsnag.register(config.bugsnagCode);
} else if (config.log.gelf.enabled) {
component = config.log.gelf.component || 'wildduck';
hostname = config.log.gelf.hostname || os.hostname();
gelf =
config.log.gelf && config.log.gelf.enabled
? new Gelf(config.log.gelf.options)
: {
// placeholder
emit: () => false
};
loggelf = message => {
if (typeof message === 'string') {
message = {
short_message: message
};
}
message = message || {};
message.facility = component; // facility is deprecated but set by the driver if not provided
message.host = hostname;
message.timestamp = Date.now() / 1000;
message._component = component;
Object.keys(message).forEach(key => {
if (!message[key]) {
delete message[key];
}
});
gelf.emit('gelf.log', message);
};
}
module.exports.notify = (...args) => {
if (bugsnag) {
bugsnag.notify(...args);
} else if (gelf) {
let err = args[0] || {};
let extra = args[1] || {};
let message = {
short_message: component.toUpperCase() + ' [Exception] ' + (err.message || ''),
full_message: err.stack,
_exception: 'yes',
_error: err.message
};
Object.keys(err).forEach(key => {
let vKey = '_' + key;
if (!message[vKey]) {
message[vKey] = (err[key] || '').toString().trim();
}
});
Object.keys(extra).forEach(key => {
let vKey = '_' + key;
if (!message[vKey]) {
message[vKey] = (err[key] || '').toString().trim();
}
});
loggelf(message);
} else {
console.error(...args);
}