Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 1 | (function () { |
| 2 | 'use strict'; |
| 3 | |
| 4 | const winston = require('winston'); |
| 5 | const fs = require('fs'); |
| 6 | const path = require('path'); |
| 7 | const level = process.env.LOG_LEVEL || 'warn'; |
| 8 | winston.level = level; |
| 9 | |
| 10 | const logFile = path.join(__dirname, '../../logs/xos-nb-rest'); |
| 11 | |
| 12 | // clear old logs |
| 13 | ['error', 'debug'].forEach(l => { |
| 14 | try { |
| 15 | fs.statSync(`${logFile}.${l}.log`) |
| 16 | fs.unlinkSync(`${logFile}.${l}.log`); |
| 17 | } |
| 18 | catch(e) { |
| 19 | // log does not exist |
| 20 | } |
| 21 | }); |
| 22 | |
| 23 | // create a custom logger with colorized console and persistance to file |
| 24 | const logger = new (winston.Logger)({ |
| 25 | transports: [ |
| 26 | new (winston.transports.Console)({level: level, colorize: true}), |
| 27 | new (winston.transports.File)({name: 'error-log', level: 'error', filename: `${logFile}.error.log`}), |
| 28 | new (winston.transports.File)({name: 'debug-log', level: 'debug', filename: `${logFile}.debug.log`}) |
| 29 | ] |
| 30 | }); |
| 31 | |
| 32 | module.exports = logger; |
| 33 | |
| 34 | })(); |