From 087fecf93a8916385444070a8be5c909e6fa8add Mon Sep 17 00:00:00 2001 From: Josh Creek Date: Thu, 5 Nov 2020 23:21:19 +0000 Subject: [PATCH] fix(*): Further fixes to elasticsearch logging to get the stacktrace coming through correctly --- index.js | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index d8f8371..795b9d0 100644 --- a/index.js +++ b/index.js @@ -4,11 +4,31 @@ const Elasticsearch = require('winston-elasticsearch'); const config = require('./config.json'); const client = new Discord.Client(); +const { format } = winston; +const { combine, timestamp } = format; + let env = 'dev'; if (process.env.NODE_ENV === 'production') { env = 'prod'; } +// Custom format function that will look for an error object and log out the stack and if +// its not production, the error itself +const myFormat = format.printf((info) => { + const { timestamp: tmsmp, level, message, error, ...rest } = info; + let log = `${tmsmp} - ${level}:\t${message}`; + // Only if there is an error + if ( error ) { + if ( error.stack) log = `${log}\n${error.stack}`; + if (process.env.NODE_ENV !== 'production') log = `${log}\n${JSON.stringify(error, null, 2)}`; + } + // Check if rest is object + if ( !( Object.keys(rest).length === 0 && rest.constructor === Object ) ) { + log = `${log}\n${JSON.stringify(rest, null, 2)}`; + } + return log; +}); + const esTransportOpts = { level: 'error', indexPrefix: 'logs_denbot', @@ -23,7 +43,8 @@ const esTransportOpts = { message: logData.message, meta: { app: 'denBot', - env: env + env: env, + stack: logData.meta.stack } } } @@ -31,9 +52,12 @@ const esTransportOpts = { const logger = winston.createLogger({ level: 'info', - format: winston.format.combine( - winston.format.timestamp(), - winston.format.json() + format: combine( + winston.format.errors({ stack: true }), // <-- use errors format + winston.format.timestamp(), + winston.format.prettyPrint(), + myFormat, + winston.format.json() ), defaultMeta: { service: 'user-service' }, transports: [