Skip to content

Commit

Permalink
feat: Emit all arguments when first argument is not a string (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Mar 10, 2024
1 parent ad09a43 commit 578a2aa
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
18 changes: 13 additions & 5 deletions README.md
Expand Up @@ -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.**

Expand All @@ -103,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
Expand Down
7 changes: 4 additions & 3 deletions index.js
Expand Up @@ -19,10 +19,11 @@ 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 {
var args = Array.prototype.slice.call(arguments);
self.emit.apply(self, [level].concat(args));
}

self.emit(level, msg);
};
}

Expand Down
12 changes: 12 additions & 0 deletions test/index.js
Expand Up @@ -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;

Expand Down

0 comments on commit 578a2aa

Please sign in to comment.