diff --git a/index.js b/index.js index f10a96f..722884a 100644 --- a/index.js +++ b/index.js @@ -3,80 +3,9 @@ const winston = require('winston'); const Elasticsearch = require('winston-elasticsearch'); const config = require('./config.json'); const {sendEmbedMessage} = require('./helpers.js'); +const { initialiseLogger } = require('./logger.js'); 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: 'info', - indexPrefix: 'logs_denbot', - clientOpts: { - host: config.elasticsearch.elasticsearch_address, - log: 'info' - }, - transformer: logData => { - return { - timestamp: new Date().toISOString(), - severity: logData.level, - message: logData.message, - meta: { - app: 'denBot', - env: env, - stack: logData.meta.stack - } - } - } -}; - -const logger = winston.createLogger({ - level: 'info', - 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: [ - new winston.transports.File({ filename: 'error.log', level: 'error' }), - new winston.transports.File({ filename: 'combined.log' }), - new Elasticsearch(esTransportOpts) - ], -}); - -// -// If we're not in production then log to the `console` with the format: -// `${info.level}: ${info.message} JSON.stringify({ ...rest }) ` -// -//if (process.env.NODE_ENV !== 'production') { - logger.add(new winston.transports.Console({ - format: winston.format.simple(), - })); -//} +const logger = initialiseLogger(config, winston, Elasticsearch); let playerQueue = []; let playerVotes = []; @@ -485,3 +414,4 @@ The commands available to you are: message.channel.send(msg); } }); + diff --git a/logger.js b/logger.js new file mode 100644 index 0000000..9c17e64 --- /dev/null +++ b/logger.js @@ -0,0 +1,78 @@ +module.exports = { + initialiseLogger: function (config, winston, Elasticsearch) { + 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: 'info', + indexPrefix: 'logs_denbot', + clientOpts: { + host: config.elasticsearch.elasticsearch_address, + log: 'info' + }, + transformer: logData => { + return { + timestamp: new Date().toISOString(), + severity: logData.level, + message: logData.message, + meta: { + app: 'denbot', + env: env, + stack: logData.meta.stack + } + } + } + }; + + const logger = winston.createLogger({ + level: 'info', + 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: [ + new winston.transports.File({ filename: 'error.log', level: 'error' }), + new winston.transports.File({ filename: 'combined.log' }), + new Elasticsearch(esTransportOpts) + ], + }); + + // + // If we're not in production then log to the `console` with the format: + // `${info.level}: ${info.message} JSON.stringify({ ...rest }) ` + // + //if (process.env.NODE_ENV !== 'production') { + logger.add(new winston.transports.Console({ + format: winston.format.simple(), + })); + //} + + return logger; + }, + };