fix `LOG.child(...)` to *not* override the "hostname" field of the parent

Fixes #291. Fixes #303.
master
Trent Mick 2016-02-02 23:45:31 -08:00
parent c3b76b1f15
commit 788ad7cefe
4 changed files with 31 additions and 1 deletions

View File

@ -26,3 +26,4 @@ Denis Izmaylov (https://github.com/DenisIzmaylov)
Guillermo Grau Panea (https://github.com/guigrpa)
Mark LeMerise (https://github.com/MarkLeMerise)
https://github.com/sometimesalready
Charly Koza (https://github.com/Cactusbone)

View File

@ -8,6 +8,10 @@ Known issues:
## 1.5.2 (not yet released)
- [pull #291, issue #303] Fix `LOG.child(...)` to *not* override the "hostname"
field of the parent. A use case is when one manually sets "hostname" to
something other than `os.hostname()`. (By github.com/Cactusbone.)
- [issue #325] Allow one to set `level: 0` in `createLogger` to turn on
logging for all levels. (Adapted from #336 by github.com/sometimesalready.)

View File

@ -478,7 +478,7 @@ function Logger(options, _childOptions, _childSimple) {
if (this.serializers) {
this._applySerializers(fields);
}
if (!fields.hostname) {
if (!fields.hostname && !self.fields.hostname) {
fields.hostname = os.hostname();
}
if (!fields.pid) {

View File

@ -134,3 +134,28 @@ test('child can set level of inherited streams and add streams', function (t) {
t.end();
});
// issue #291
test('child should not lose parent "hostname"', function (t) {
var stream = new CapturingStream();
var dad = bunyan.createLogger({
name: 'hostname-test',
hostname: 'bar0',
streams: [ {
type: 'raw',
stream: stream,
level: 'info'
} ]
});
var son = dad.child({component: 'son'});
dad.info('HI');
son.info('hi');
t.equal(stream.recs.length, 2);
t.equal(stream.recs[0].hostname, 'bar0');
t.equal(stream.recs[1].hostname, 'bar0');
t.equal(stream.recs[1].component, 'son');
t.end();
});