feature: add loggers and save logs to DB when using Firebase

This commit is contained in:
Ondřej Synáček 2020-11-15 21:33:16 +01:00
parent 8afa1a9b3b
commit c55774eb39
5 changed files with 96 additions and 13 deletions

6
database.rules.json Normal file
View File

@ -0,0 +1,6 @@
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}

View File

@ -21,5 +21,8 @@
"function": "app"
}
]
},
"database": {
"rules": "database.rules.json"
}
}

View File

@ -2,19 +2,21 @@ const functions = require('firebase-functions');
const admin = require('firebase-admin')
const { configureApplication } = require('./lib/app')
// const { createAppLogger } = require('./lib/log-utils')
// const {
// createRouteLogger,
// createErrorLogger,
// } = require('./lib/middlewares')
const {
createRouteLogger,
createErrorLogger,
createAppLogger,
} = require('./logger')
admin.initializeApp()
const isDevelopment = process.env.NODE_ENV === 'development'
// const appLogger = createAppLogger({ dev: isDevelopment })
// const errorLogger = createErrorLogger({ dev: isDevelopment })
// const routeLogger = isDevelopment ? createRouteLogger({ dev: isDevelopment }) : null
const db = admin.database()
const appLogger = createAppLogger({ db })
const errorLogger = createErrorLogger({ db })
const routeLogger = createRouteLogger({ db })
const corsOptions = isDevelopment ? {
origin: 'http://localhost:5000',
} : null
@ -22,6 +24,9 @@ const corsOptions = isDevelopment ? {
const app = configureApplication({
rateLimitEnabled: false,
corsOptions,
appLogger,
errorLogger,
routeLogger,
})
exports.app = functions.https.onRequest(app)

68
functions/logger.js Normal file
View File

@ -0,0 +1,68 @@
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
}
log(info, callback) {
try {
this._db.ref(`log-${new Date().getTime()}`).set(info)
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,
}

View File

@ -13,8 +13,6 @@
},
"main": "index.js",
"dependencies": {
"firebase-admin": "^9.2.0",
"firebase-functions": "^3.11.0",
"body-parser": "^1.19.0",
"cheerio": "^1.0.0-rc.3",
"cors": "^2.8.5",
@ -22,12 +20,15 @@
"ejs": "^2.7.1",
"express": "^4.17.1",
"express-rate-limit": "^5.0.0",
"express-winston": "^4.0.1",
"express-winston": "^4.0.5",
"firebase-admin": "^9.2.0",
"firebase-functions": "^3.11.0",
"ics": "^2.22.1",
"request": "^2.88.0",
"serve-favicon": "^2.5.0",
"winston": "^3.2.1",
"winston-daily-rotate-file": "^4.2.1"
"winston": "^3.3.3",
"winston-daily-rotate-file": "^4.2.1",
"winston-transport": "^4.4.0"
},
"devDependencies": {
"firebase-functions-test": "^0.2.0"