Skip to content

Instantly share code, notes, and snippets.

@nicolasdao
Last active June 13, 2018 07:10
Show Gist options
  • Save nicolasdao/f3c7526e4211a4207fef9ff8d00a4078 to your computer and use it in GitHub Desktop.
Save nicolasdao/f3c7526e4211a4207fef9ff8d00a4078 to your computer and use it in GitHub Desktop.
// INIT THE LOGGER
// All environment and all projects logs to the same papertrail account (defined by its specific ACCOUNT_PORT).
// The papertrail paradigm is to log everything to the same container, and then use filters (aka searches) to
// containerize your logs on the fly.
const winston = require('winston')
require('winston-papertrail').Papertrail
const ACCOUNT_PORT = 1234
const PROJECT = 'your-project-name'
const getLogger = (env) => {
if (!env) {
return {
info: console.log,
error: console.error
}
}
else {
const winstonPapertrail = new winston.transports.Papertrail({
host: 'logs.papertrailapp.com',
port: ACCOUNT_PORT,
hostname: PROJECT,
program: env
})
winstonPapertrail.on('error', console.error)
const logger = new winston.Logger({ transports: [winstonPapertrail] })
return logger
}
}
const logIt = (transactionId, data, logFn) => {
const transId =
data == undefined ? 'undefined' :
transactionId && transactionId.__transactionId ? transactionId.__transactionId : transactionId
const d = data !=undefined ? data : transactionId
logFn(JSON.stringify({ transactionId: transId, data: d }))
}
let logger
const get = env => {
if (!logger) {
const _logger = getLogger(env)
logger = {}
logger.info = (transactionId, data) => logIt(transactionId, data, _logger.info)
logger.error = (transactionId, data) => logIt(transactionId, data, _logger.error)
}
return logger
}
module.exports = { 'get': get }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment