garage-it / SmartHouse-backend

9 stars 2 forks source link

Backend logging #63

Open Appius opened 7 years ago

Appius commented 7 years ago

Recently I faced a problem with unhandled exception in backend project. It was a problem with the code in backend project, and all I saw in the browser console is "500 internal error" without any stacktrace or other helpful details.

Right now we have logging in backend project only to console, what is not much helpful for production mode (e.g. we had a bug what was not reproducible locally, only on rasp).

What do you think about adding another transport level for winston logger to write errors to MongoDB?

It could be performed by the following steps:

  1. Add npm package winston-mongodb;
  2. Add new transport level in winston.js file:
    if (config.logToDB) {
    logger.add(winston.transports.MongoDB, {
        level: config.logOptions.level || 'error',
        db: config.db,
        collection: 'logs',
        cappedMax: 1000,
        handleExceptions: true
    });
    }

Part 2 (optional): Add API to backend and page to frontend where we can load and see these logs (before it, it's fine just to query them directly via Mongo Shell).

P.S. Also, it's possible to write logs into the file - it would be easier to implement, but harder to get without API / separate page.