Ensure the log directory is created before instanciate the logger
[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 config = require('config')
5 const mkdirp = require('mkdirp')
6 const path = require('path')
7 const winston = require('winston')
8 winston.emitErrs = true
9
10 const logDir = path.join(__dirname, '..', '..', config.get('storage.logs'))
11
12 // Create the directory if it does not exist
13 mkdirp.sync(logDir)
14
15 const logger = new winston.Logger({
16   transports: [
17     new winston.transports.File({
18       level: 'debug',
19       filename: path.join(logDir, 'all-logs.log'),
20       handleExceptions: true,
21       json: true,
22       maxsize: 5242880,
23       maxFiles: 5,
24       colorize: false
25     }),
26     new winston.transports.Console({
27       level: 'debug',
28       handleExceptions: true,
29       humanReadableUnhandledException: true,
30       json: false,
31       colorize: true
32     })
33   ],
34   exitOnError: true
35 })
36
37 logger.stream = {
38   write: function (message, encoding) {
39     logger.info(message)
40   }
41 }
42
43 // ---------------------------------------------------------------------------
44
45 module.exports = logger