From 59275efac98bb71f4366633c61b6ab481faed185 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Sat, 9 Mar 2024 19:42:28 -0700 Subject: [PATCH 1/4] feat: Emit all arguments when first argument is not a string --- README.md | 16 ++++++++++++---- index.js | 6 +++--- test/index.js | 12 ++++++++++++ 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 65ca4ed..2691ce1 100644 --- a/README.md +++ b/README.md @@ -57,34 +57,42 @@ are passed through node's `util.format()` before being emitted. Other parts of a node program can get the logger by namespace and listen for the events to be emitted. -#### logger.debug(msg) +#### logger.debug(msg, ...args) Emits a `debug` event with the given `msg`. If the first argument is a string, all arguments are passed to node's `util.format()` before being emitted. -#### logger.info(msg) +If the first argument is not a string, all arguments will be emitted directly. + +#### logger.info(msg, ...args) Emits a `info` event with the given `msg`. If the first argument is a string, all arguments are passed to node's `util.format()` before being emitted. -#### logger.warn(msg) +If the first argument is not a string, all arguments will be emitted directly. + +#### logger.warn(msg, ...args) Emits a `warn` event with the given `msg`. If the first argument is a string, all arguments are passed to node's `util.format()` before being emitted. -#### logger.error(msg) +If the first argument is not a string, all arguments will be emitted directly. + +#### logger.error(msg, ...args) Emits a `error` event with the given `msg`. If the first argument is a string, all arguments are passed to node's `util.format()` before being emitted. +If the first argument is not a string, all arguments will be emitted directly. + **Note: You must handle this event in some way or the node process will crash when an `error` event is emitted.** diff --git a/index.js b/index.js index 546abec..509e36f 100644 --- a/index.js +++ b/index.js @@ -19,10 +19,10 @@ function getLogger(namespace) { function makeLogLevel(self, level) { return function (msg) { if (typeof msg === 'string') { - msg = format.apply(null, arguments); + self.emit(level, format.apply(null, arguments)); + } else { + self.emit.apply(self, arguments); } - - self.emit(level, msg); }; } diff --git a/test/index.js b/test/index.js index ef15d31..7875c57 100644 --- a/test/index.js +++ b/test/index.js @@ -73,6 +73,18 @@ describe('glogg', function () { logger.debug(expected); }); + it('emits all arguments to the log if non-string message', function (done) { + var expected = { test: 'something' }; + + logger.on('debug', function (msg, extra) { + expect(msg).toEqual(expected); + expect(extra).toEqual(true); + done(); + }); + + logger.debug(expected, true); + }); + it('allows you to "destructure" the individual log-level functions', function (done) { var debug = logger.debug; From b57f14c08d370892dd92ddd7d87ae7f3ccc0fc9e Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Sat, 9 Mar 2024 19:43:45 -0700 Subject: [PATCH 2/4] chore: Update badge --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2691ce1..8dac173 100644 --- a/README.md +++ b/README.md @@ -111,7 +111,7 @@ MIT [npm-image]: https://img.shields.io/npm/v/glogg.svg?style=flat-square [ci-url]: https://github.com/gulpjs/glogg/actions?query=workflow:dev -[ci-image]: https://img.shields.io/github/workflow/status/gulpjs/glogg/dev?style=flat-square +[ci-image]: https://img.shields.io/github/actions/workflow/status/gulpjs/glogg/dev.yml?style=flat-square [coveralls-url]: https://coveralls.io/r/gulpjs/glogg [coveralls-image]: https://img.shields.io/coveralls/gulpjs/glogg/master.svg?style=flat-square From a400bf423671e853e7067c34289f2c17e6fc024f Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Sat, 9 Mar 2024 19:49:42 -0700 Subject: [PATCH 3/4] try slicing arguments --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 509e36f..5d3daa0 100644 --- a/index.js +++ b/index.js @@ -21,7 +21,7 @@ function makeLogLevel(self, level) { if (typeof msg === 'string') { self.emit(level, format.apply(null, arguments)); } else { - self.emit.apply(self, arguments); + self.emit.apply(self, Array.prototype.slice.call(arguments)); } }; } From d9cf423750caef750895aa85a4c27d10b02f5cc5 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Sat, 9 Mar 2024 19:56:26 -0700 Subject: [PATCH 4/4] level needs to come first --- index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 5d3daa0..c2338e3 100644 --- a/index.js +++ b/index.js @@ -21,7 +21,8 @@ function makeLogLevel(self, level) { if (typeof msg === 'string') { self.emit(level, format.apply(null, arguments)); } else { - self.emit.apply(self, Array.prototype.slice.call(arguments)); + var args = Array.prototype.slice.call(arguments); + self.emit.apply(self, [level].concat(args)); } }; }