Skip to content

Commit

Permalink
Merge pull request #93 from seegno/enhancement/add-configurable-simpl…
Browse files Browse the repository at this point in the history
…e-option-for-child-loggers

Add configurable `simple` option for child loggers
  • Loading branch information
ruimarinho authored Mar 23, 2017
2 parents 239b4e0 + c0ba04f commit 532a10c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
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

0 comments on commit 532a10c

Please sign in to comment.