logger.js
1.9 KB
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
66
67
68
69
70
// # boot - logger
var _ = require('underscore')
var mergeDefaults = require('../utils/merge-defaults')
var winston = require('winston')
exports = module.exports = function (settings) {
if (!_.isObject(settings.logger))
throw new Error('Settings did not have a `logger` object')
if (!_.isObject(settings.output))
throw new Error('Settings did not have a `output` object')
var transports = []
if (settings.logger['console'])
transports.push(
new winston.transports.Console({
level: 'debug',
timestamp: true,
colorize: true
})
)
if (settings.logger.file)
transports.push(
new winston.transports.File(
mergeDefaults(
settings.output,
settings.logger.file
)
)
)
var logger = winston.createLogger({
format: winston.format.combine(
winston.format.splat(),
winston.format.json()
),
transports: transports
})
// if (settings.server.env === 'development')
// logger = expandErrors(logger)
return logger
// Extend a winston by making it expand errors when passed in as the
// second argument (the first argument is the log level).
// <https://gist.github.com/johndgiese/59bd96360ce411042294>
// <https://gist.github.com/getvega/6211610>
function expandErrors(logger) {
var oldLogFunc = logger.log
logger.log = function () {
var args = Array.prototype.slice.call(arguments, 0)
if (args.length >= 2 && args[1] instanceof Error) {
args[1] = args[1].stack
}
return oldLogFunc.apply(this, args)
};
return logger
}
}
exports['@singleton'] = true
exports['@require'] = ['igloo/settings']