From 788ad7cefe2c82886eafd395a38876735722104d Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Tue, 2 Feb 2016 23:45:31 -0800 Subject: [PATCH] fix `LOG.child(...)` to *not* override the "hostname" field of the parent Fixes #291. Fixes #303. --- AUTHORS | 1 + CHANGES.md | 4 ++++ lib/bunyan.js | 2 +- test/child-behaviour.test.js | 25 +++++++++++++++++++++++++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index 87df086..b8e1ae2 100644 --- a/AUTHORS +++ b/AUTHORS @@ -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) diff --git a/CHANGES.md b/CHANGES.md index 972b4ae..8fecc2f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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.) diff --git a/lib/bunyan.js b/lib/bunyan.js index bda7006..9c1955f 100644 --- a/lib/bunyan.js +++ b/lib/bunyan.js @@ -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) { diff --git a/test/child-behaviour.test.js b/test/child-behaviour.test.js index f29ec7a..ddc2896 100644 --- a/test/child-behaviour.test.js +++ b/test/child-behaviour.test.js @@ -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(); +});