Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Emit all arguments when first argument is not a string #7

Merged
merged 4 commits into from Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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