-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.js
65 lines (55 loc) · 1.81 KB
/
logger.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*global module, process*/
/**
* Use this singleton instance of logger like:
* logger = require('Logger.js');
* logger.debug('your debug statement');
* logger.warn('your warning');
*/
var winston = require('winston');
var fs = require('fs');
var path = require('path');
var logDir = '/var/log/mindup'; // TODO: Or read from a configuration
var env = process.env.NODE_ENV || 'development';
// winston.setLevels(winston.config.npm.levels);
// winston.addColors(winston.config.npm.colors);
// TODO: handle error if the file can't be created.
if (!fs.existsSync(logDir)) {
// Create the directory if it does not exist
fs.mkdirSync(logDir);
}
var createLogger = function (filename) {
// Create a logger.
var logger = new(winston.Logger)({
transports: [
new winston.transports.Console({
level: env === 'development' ? 'debug' : 'info',
colorize: true
}),
new winston.transports.File({
level: env === 'development' ? 'debug' : 'info',
filename: logDir + '/mindup.log',
maxsize: 1024 * 1024 * 10, // 10MB
json: false
})
]
// exceptionHandlers: [
// new winston.transports.File({
// filename: 'log/exceptions.log'
// })
// ]
});
// Prepend filename in logs.
var logLevels = ['silly', 'debug', 'verbose', 'info', 'warn', 'error'];
var prependedStr = path.basename(filename);
logLevels.forEach(function (method) {
var oldMethod = logger[method].bind(logger);
logger[method] = function() {
var args = [];
Array.prototype.push.apply(args, arguments);
args[0] = prependedStr + ': ' + args[0];
oldMethod.apply(logger, args);
};
});
return logger;
};
module.exports = createLogger;