Facebook-Events-iCal-Converter/functions/logger.js

69 lines
1.4 KiB
JavaScript
Raw Normal View History

const winston = require('winston')
const expressWinston = require('express-winston')
const Transport = require('winston-transport')
class FirebaseTransport extends Transport {
constructor(options) {
super(options)
this._db = options.db
}
async log(info, callback) {
try {
await this._db.ref(`log-${new Date().getTime()}`).set(info)
callback(null, info)
this.emit('logged', info)
} catch (err) {
2020-12-23 22:19:29 +01:00
callback(err)
this.emit('error', err)
}
return info
}
}
const createRouteLogger = ({ db }) => {
return expressWinston.logger({
transports: [
new FirebaseTransport({ db })
],
format: winston.format.combine(
winston.format.timestamp(),
winston.format.json()
),
meta: true,
expressFormat: true,
})
}
const createErrorLogger = ({ db }) => {
return expressWinston.errorLogger({
transports: [
new FirebaseTransport({ db })
],
format: winston.format.combine(
winston.format.timestamp(),
winston.format.json()
),
})
}
const createAppLogger = ({ db }) => {
return winston.createLogger({
format: winston.format.combine(
winston.format.timestamp(),
winston.format.json()
),
transports: [
new FirebaseTransport({ db }),
new winston.transports.Console(),
]
})
}
module.exports = {
createRouteLogger,
createErrorLogger,
createAppLogger,
}