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

Add configurable simple option for child loggers #93

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ The `prefix` and `suffix` for each component is also customizable:
var logger = require('debugnyan')('foo', {}, { suffix: 'module' });
```

When creating a _child_ logger you may also override the default `simple` behavior:

```js
var logger = require('debugnyan')('foo:bar', {}, { simple: false });
```

## Tests

```
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"should": "^11.1.0"
},
"engines": {
"node": ">=4"
"node": ">=6"
},
"nyc": {
"include": [
Expand Down
15 changes: 7 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@ const level = bunyan.FATAL + 1;
* Export `debugnyan`.
*/

export default function debugnyan(name, options, config) {
export default function debugnyan(name, options, {
prefix = 'sub',
simple = true,
suffix = 'component'
} = {}) {
const components = name.split(':');
const [root] = components;

config = Object.assign({
prefix: 'sub',
suffix: 'component'
}, config);

if (!loggers[root]) {
loggers[root] = bunyan.createLogger(Object.assign({}, options, { level, name: root }));
}
Expand All @@ -49,11 +48,11 @@ export default function debugnyan(name, options, config) {
}

options = Object.assign({}, options, {
[`${config.prefix.repeat(i - 1)}${config.suffix}`]: current,
[`${prefix.repeat(i - 1)}${suffix}`]: current,
level
});

child = next.child(options, true);
child = next.child(options, simple);

loggers[childName] = child;
}
Expand Down
6 changes: 6 additions & 0 deletions test/index_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ describe('debugnyan', () => {
logger.fields.component.should.equal('bar');
});

it('should accept the `simple` option', () => {
const logger = debugnyan('foo:biz', {}, { simple: false });

logger.should.not.have.property('_isSimpleChild');
});

it('should be on the debug level if `DEBUG` matches logger name', () => {
debug.enable('foo:bar');

Expand Down