spserver/lib/logger.js

47 lines
1.3 KiB
JavaScript
Raw Permalink Normal View History

2015-02-02 09:52:31 +00:00
'use strict';
var _ = require('lodash');
2018-07-22 12:06:44 +00:00
var bunyan = require('bunyan-lite');
2015-02-02 09:52:31 +00:00
//Get the config
var config = require('./config');
//Create our variables
var env = config.get('NODE_ENV');
var output;
if (config.get('bunyan') || config.get(env + ':use_bunyan')) {
var settings = _.cloneDeep(config.get(env + ':bunyan'));
// Stream can be specified either in settings.streams[ix] or globally in settings.stream, but not
// both. Since the defaults specify stetings.stream, if the user specifies anything of vaulue
// in settings.streams, we should delete the global defaults, because bunyan gets angry if there
// are multiple keys
if (_.has(settings, 'streams')) {
if (settings.streams) {
delete settings.stream;
delete settings.level;
} else {
delete settings.streams;
}
}
2016-12-14 23:23:30 +00:00
_([settings.streams, settings])
.flatten()
.compact()
2016-12-14 23:23:30 +00:00
.forEach(function (settingObj) {
if (settingObj.stream === 'process.stdout') {
settingObj.stream = process.stdout;
} else if (settingObj.stream === 'process.stderr') {
settingObj.stream = process.stderr;
}
});
2015-02-02 09:52:31 +00:00
output = bunyan.createLogger(settings);
} else {
output = console;
output.debug = console.log.bind(console);
2015-02-02 09:52:31 +00:00
}
module.exports = output;