fix #105: export addStream and addSerializers
This commit is contained in:
parent
a689ca171e
commit
334e36cdfd
1 changed files with 119 additions and 89 deletions
148
lib/bunyan.js
148
lib/bunyan.js
|
@ -337,7 +337,85 @@ function Logger(options, _childOptions, _childSimple) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helpers
|
// Helpers
|
||||||
function addStream(s) {
|
|
||||||
|
// Handle *config* options (i.e. options that are not just plain data
|
||||||
|
// for log records).
|
||||||
|
if (options.stream) {
|
||||||
|
self.addStream({
|
||||||
|
type: 'stream',
|
||||||
|
stream: options.stream,
|
||||||
|
closeOnExit: false,
|
||||||
|
level: (options.level ? resolveLevel(options.level) : INFO)
|
||||||
|
});
|
||||||
|
} else if (options.streams) {
|
||||||
|
options.streams.forEach(function(s) {
|
||||||
|
s.level = s.level || s.level;
|
||||||
|
self.addStream(s);
|
||||||
|
});
|
||||||
|
} else if (parent && options.level) {
|
||||||
|
this.level(options.level);
|
||||||
|
} else if (!parent) {
|
||||||
|
self.addStream({
|
||||||
|
type: 'stream',
|
||||||
|
stream: process.stdout,
|
||||||
|
closeOnExit: false,
|
||||||
|
level: (options.level ? resolveLevel(options.level) : INFO)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (options.serializers) {
|
||||||
|
self.addSerializers(options.serializers);
|
||||||
|
}
|
||||||
|
if (options.src) {
|
||||||
|
this.src = true;
|
||||||
|
}
|
||||||
|
xxx('Logger: ', self)
|
||||||
|
|
||||||
|
// Fields.
|
||||||
|
// These are the default fields for log records (minus the attributes
|
||||||
|
// removed in this constructor). To allow storing raw log records
|
||||||
|
// (unrendered), `this.fields` must never be mutated. Create a copy for
|
||||||
|
// any changes.
|
||||||
|
var fields = objCopy(options);
|
||||||
|
delete fields.stream;
|
||||||
|
delete fields.level;
|
||||||
|
delete fields.streams;
|
||||||
|
delete fields.serializers;
|
||||||
|
delete fields.src;
|
||||||
|
if (this.serializers) {
|
||||||
|
this._applySerializers(fields);
|
||||||
|
}
|
||||||
|
if (!fields.hostname) {
|
||||||
|
fields.hostname = os.hostname();
|
||||||
|
}
|
||||||
|
if (!fields.pid) {
|
||||||
|
fields.pid = process.pid;
|
||||||
|
}
|
||||||
|
Object.keys(fields).forEach(function (k) {
|
||||||
|
self.fields[k] = fields[k];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
util.inherits(Logger, EventEmitter);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a stream
|
||||||
|
*
|
||||||
|
* @param stream {Object}. Object with these fields:
|
||||||
|
* - `type`: The stream type. See README.md for full details.
|
||||||
|
* Often this is implied by the other fields. Examples are
|
||||||
|
* 'file', 'stream' and "raw".
|
||||||
|
* - `level`: Defaults to 'info'.
|
||||||
|
* - `path` or `stream`: The specify the file path or writeable
|
||||||
|
* stream to which log records are written. E.g.
|
||||||
|
* `stream: process.stdout`.
|
||||||
|
* - `closeOnExit` (boolean): Optional. Default is true for a
|
||||||
|
* 'file' stream when `path` is given, false otherwise.
|
||||||
|
* See README.md for full details.
|
||||||
|
*/
|
||||||
|
Logger.prototype.addStream = function(s) {
|
||||||
|
var self = this;
|
||||||
|
|
||||||
s = objCopy(s);
|
s = objCopy(s);
|
||||||
|
|
||||||
// Implicit 'type' from other args.
|
// Implicit 'type' from other args.
|
||||||
|
@ -353,8 +431,6 @@ function Logger(options, _childOptions, _childSimple) {
|
||||||
|
|
||||||
if (s.level) {
|
if (s.level) {
|
||||||
s.level = resolveLevel(s.level);
|
s.level = resolveLevel(s.level);
|
||||||
} else if (options.level) {
|
|
||||||
s.level = resolveLevel(options.level);
|
|
||||||
} else {
|
} else {
|
||||||
s.level = INFO;
|
s.level = INFO;
|
||||||
}
|
}
|
||||||
|
@ -407,7 +483,16 @@ function Logger(options, _childOptions, _childSimple) {
|
||||||
self.streams.push(s);
|
self.streams.push(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
function addSerializers(serializers) {
|
|
||||||
|
/**
|
||||||
|
* Add serializers
|
||||||
|
*
|
||||||
|
* @param serializers {Object} Optional. Object mapping log record field names to
|
||||||
|
* serializing functions. See README.md for details.
|
||||||
|
*/
|
||||||
|
Logger.prototype.addSerializers = function(serializers) {
|
||||||
|
var self = this;
|
||||||
|
|
||||||
if (!self.serializers) {
|
if (!self.serializers) {
|
||||||
self.serializers = {};
|
self.serializers = {};
|
||||||
}
|
}
|
||||||
|
@ -423,61 +508,6 @@ function Logger(options, _childOptions, _childSimple) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle *config* options (i.e. options that are not just plain data
|
|
||||||
// for log records).
|
|
||||||
if (options.stream) {
|
|
||||||
addStream({
|
|
||||||
type: 'stream',
|
|
||||||
stream: options.stream,
|
|
||||||
closeOnExit: false,
|
|
||||||
level: (options.level ? resolveLevel(options.level) : INFO)
|
|
||||||
});
|
|
||||||
} else if (options.streams) {
|
|
||||||
options.streams.forEach(addStream);
|
|
||||||
} else if (parent && options.level) {
|
|
||||||
this.level(options.level);
|
|
||||||
} else if (!parent) {
|
|
||||||
addStream({
|
|
||||||
type: 'stream',
|
|
||||||
stream: process.stdout,
|
|
||||||
closeOnExit: false,
|
|
||||||
level: (options.level ? resolveLevel(options.level) : INFO)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if (options.serializers) {
|
|
||||||
addSerializers(options.serializers);
|
|
||||||
}
|
|
||||||
if (options.src) {
|
|
||||||
this.src = true;
|
|
||||||
}
|
|
||||||
xxx('Logger: ', self)
|
|
||||||
|
|
||||||
// Fields.
|
|
||||||
// These are the default fields for log records (minus the attributes
|
|
||||||
// removed in this constructor). To allow storing raw log records
|
|
||||||
// (unrendered), `this.fields` must never be mutated. Create a copy for
|
|
||||||
// any changes.
|
|
||||||
var fields = objCopy(options);
|
|
||||||
delete fields.stream;
|
|
||||||
delete fields.level;
|
|
||||||
delete fields.streams;
|
|
||||||
delete fields.serializers;
|
|
||||||
delete fields.src;
|
|
||||||
if (this.serializers) {
|
|
||||||
this._applySerializers(fields);
|
|
||||||
}
|
|
||||||
if (!fields.hostname) {
|
|
||||||
fields.hostname = os.hostname();
|
|
||||||
}
|
|
||||||
if (!fields.pid) {
|
|
||||||
fields.pid = process.pid;
|
|
||||||
}
|
|
||||||
Object.keys(fields).forEach(function (k) {
|
|
||||||
self.fields[k] = fields[k];
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
util.inherits(Logger, EventEmitter);
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue