mirror of
https://github.com/jcreek/denBot.git
synced 2026-07-13 10:53:45 +00:00
fix(*): Further fixes to elasticsearch logging to get the stacktrace coming through correctly
This commit is contained in:
@@ -4,11 +4,31 @@ const Elasticsearch = require('winston-elasticsearch');
|
|||||||
const config = require('./config.json');
|
const config = require('./config.json');
|
||||||
const client = new Discord.Client();
|
const client = new Discord.Client();
|
||||||
|
|
||||||
|
const { format } = winston;
|
||||||
|
const { combine, timestamp } = format;
|
||||||
|
|
||||||
let env = 'dev';
|
let env = 'dev';
|
||||||
if (process.env.NODE_ENV === 'production') {
|
if (process.env.NODE_ENV === 'production') {
|
||||||
env = 'prod';
|
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 = {
|
const esTransportOpts = {
|
||||||
level: 'error',
|
level: 'error',
|
||||||
indexPrefix: 'logs_denbot',
|
indexPrefix: 'logs_denbot',
|
||||||
@@ -23,7 +43,8 @@ const esTransportOpts = {
|
|||||||
message: logData.message,
|
message: logData.message,
|
||||||
meta: {
|
meta: {
|
||||||
app: 'denBot',
|
app: 'denBot',
|
||||||
env: env
|
env: env,
|
||||||
|
stack: logData.meta.stack
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -31,8 +52,11 @@ const esTransportOpts = {
|
|||||||
|
|
||||||
const logger = winston.createLogger({
|
const logger = winston.createLogger({
|
||||||
level: 'info',
|
level: 'info',
|
||||||
format: winston.format.combine(
|
format: combine(
|
||||||
|
winston.format.errors({ stack: true }), // <-- use errors format
|
||||||
winston.format.timestamp(),
|
winston.format.timestamp(),
|
||||||
|
winston.format.prettyPrint(),
|
||||||
|
myFormat,
|
||||||
winston.format.json()
|
winston.format.json()
|
||||||
),
|
),
|
||||||
defaultMeta: { service: 'user-service' },
|
defaultMeta: { service: 'user-service' },
|
||||||
|
|||||||
Reference in New Issue
Block a user