Skip to content

Commit

Permalink
Added MDC support for logger indirect logger creation
Browse files Browse the repository at this point in the history
  • Loading branch information
etki committed Aug 15, 2017
1 parent 7c94fe8 commit 0cd51b6
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ var settings = {
logger: {
level: SDK.Logger.Level.Info,
name: 'custom-name',
// mapping diagnostic context as described above
mdc: {
entity: 'call#123'
},
instance: new MyCustomLogger()
}
};
Expand Down
1 change: 1 addition & 0 deletions lib/logger/_definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,5 @@
* @property {(ILoggerContext|null)} context
* @property {(String|null)} name
* @property {(Level|null)} level
* @property {(Object|null)} mdc
*/
10 changes: 8 additions & 2 deletions lib/logger/slf4j.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@ function Slf4j (name, context) {

this.attachAll = function (values) { mdc = values }

this.detachAll = function () { mdc = {} }
this.detachAll = function () {
var b = mdc
mdc = {}
return b
}

/**
* @function Slf4j#trace
Expand Down Expand Up @@ -245,7 +249,9 @@ Slf4j.factory = function (options, name, context, level) {
return options.instance
}
context = options.context || context || Slf4j
return context.create(options.name || name, level || options.level)
var logger = context.create(options.name || name, level || options.level)
logger.attachAll(options.mdc || {})
return logger
}

module.exports = {
Expand Down
6 changes: 6 additions & 0 deletions test/suites/integration/logger/slf4j.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,12 @@ describe('Integration', function () {
var mockingbird = {}
expect(Slf4j.factory({instance: mockingbird})).to.eq(mockingbird)
})

it('allows setting mapping diagnostic context', function () {
var mdc = {alpha: 'beta'}
var logger = Slf4j.factory({mdc: mdc})
expect(logger.detachAll()).to.deep.eq(mdc)
})
})
})
})
Expand Down

0 comments on commit 0cd51b6

Please sign in to comment.