fcc1789fd53875978bb654c9a9a9788f4030b4a4
[oweals/peertube.git] / server / helpers / logger.js
1 // Thanks http://tostring.it/2014/06/23/advanced-logging-with-nodejs/
2 'use strict'
3
4 const mkdirp = require('mkdirp')
5 const path = require('path')
6 const winston = require('winston')
7 winston.emitErrs = true
8
9 const constants = require('../initializers/constants')
10
11 const label = constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT
12
13 // Create the directory if it does not exist
14 mkdirp.sync(constants.CONFIG.STORAGE.LOG_DIR)
15
16 const logger = new winston.Logger({
17   transports: [
18     new winston.transports.File({
19       level: 'debug',
20       filename: path.join(constants.CONFIG.STORAGE.LOG_DIR, 'all-logs.log'),
21       handleExceptions: true,
22       json: true,
23       maxsize: 5242880,
24       maxFiles: 5,
25       colorize: false
26     }),
27     new winston.transports.Console({
28       level: 'debug',
29       label: label,
30       handleExceptions: true,
31       humanReadableUnhandledException: true,
32       json: false,
33       colorize: true
34     })
35   ],
36   exitOnError: true
37 })
38
39 logger.stream = {
40   write: function (message, encoding) {
41     logger.info(message)
42   }
43 }
44
45 // ---------------------------------------------------------------------------
46
47 module.exports = logger