lamassu-server/lib/pg-transport.js
2021-10-11 16:22:59 +01:00

43 lines
1.1 KiB
JavaScript

const Transport = require('winston-transport')
const eventBus = require('./event-bus')
//
// Inherit from `winston-transport` so you can take advantage
// of the base functionality and `.exceptions.handle()`.
//
module.exports = class CustomTransport extends Transport {
constructor (opts) {
super(opts)
//
// Consume any custom options here. e.g.:
// - Connection information for databases
// - Authentication information for APIs (e.g. loggly, papertrail,
// logentries, etc.).
//
this.tableName = opts.tableName || 'winston_logs'
if (!opts.connectionString) {
throw new Error('You have to define connectionString')
}
this.connectionString = opts.connectionString
}
log (level, message, meta, callback) {
if (!callback) callback = () => {}
setImmediate(() => {
this.emit('logged', level, message, meta)
})
// Perform the writing to the remote service
eventBus.publish('log', { level, message, meta })
callback()
}
logException (msg, meta, callback) {
this.log('error', msg, meta, callback)
}
}