Merge pull request #138 from andreineculau/patch-1
fix #105: export addStream and addSerializers
This commit is contained in:
commit
eaa88a74b6
1 changed files with 119 additions and 89 deletions
150
lib/bunyan.js
150
lib/bunyan.js
|
@ -338,7 +338,85 @@ function Logger(options, _childOptions, _childSimple) {
|
|||
}
|
||||
|
||||
// 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);
|
||||
|
||||
// Implicit 'type' from other args.
|
||||
|
@ -354,8 +432,6 @@ function Logger(options, _childOptions, _childSimple) {
|
|||
|
||||
if (s.level) {
|
||||
s.level = resolveLevel(s.level);
|
||||
} else if (options.level) {
|
||||
s.level = resolveLevel(options.level);
|
||||
} else {
|
||||
s.level = INFO;
|
||||
}
|
||||
|
@ -406,9 +482,18 @@ function Logger(options, _childOptions, _childSimple) {
|
|||
}
|
||||
|
||||
self.streams.push(s);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
function addSerializers(serializers) {
|
||||
if (!self.serializers) {
|
||||
self.serializers = {};
|
||||
}
|
||||
|
@ -422,63 +507,8 @@ function Logger(options, _childOptions, _childSimple) {
|
|||
self.serializers[field] = serializer;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 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