2020-11-15 21:33:16 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:19:34 +01:00
|
|
|
async log(info, callback) {
|
2020-11-15 21:33:16 +01:00
|
|
|
try {
|
2020-12-12 13:19:34 +01:00
|
|
|
await this._db.ref(`log-${new Date().getTime()}`).set(info)
|
2020-11-15 21:33:16 +01:00
|
|
|
callback(null, info)
|
|
|
|
this.emit('logged', info)
|
|
|
|
} catch (err) {
|
|
|
|
callback(error)
|
|
|
|
this.emit('error', error)
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|