cablelabs / lpwanserver

LPWAN Provisioning & Management Server
https://lpwanserver.com
Apache License 2.0
37 stars 11 forks source link

Update logging #258

Closed rhythnic closed 5 years ago

rhythnic commented 5 years ago

Overview

Update logger to better log JSON objects. Use a log_level config variable and pass to winston config.

All logs are JSON objects. Log all errors to one file and all logs to another file. Use a docker-compose volume so that these logs are available in the local repo during development (assuming development is happening in docker).

Logger configuration file

// See example usages of logger instance
// https://github.com/winstonjs/winston/blob/master/examples/quick-start.js
const { createLogger, format, transports } = require('winston')
const config = require('./config')

const logger = createLogger({
  level: config.LOG_LEVEL,
  format: format.combine(
    format.timestamp({
      format: 'YYYY-MM-DD HH:mm:ss'
    }),
    format.errors({ stack: true }),
    format.splat(),
    format.json()
  ),
  transports: [
    new transports.File({ filename: 'data/logs/error.log', level: 'error' }),
    new transports.File({ filename: 'data/logs/combined.log', level: config.LOG_LEVEL })
  ]
});

//
// If we're not in production then log to the `console` with the format:
// `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
// 
if (config.NODE_ENV !== 'production') {
  logger.add(new transports.Console({
    format: format.simple()
  }));
}

module.exports = logger