From d25e0180b78c6612b1aa61d9436d81806dcd1b88 Mon Sep 17 00:00:00 2001 From: Andris Reinman Date: Thu, 18 Oct 2018 11:11:53 +0300 Subject: [PATCH] update --- lib/errors.js | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/lib/errors.js b/lib/errors.js index cd91a651..3361ae91 100644 --- a/lib/errors.js +++ b/lib/errors.js @@ -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); }