Skip to content

Commit f631ee0

Browse files
committed
Facade for every opts.logger method
1 parent d057f2f commit f631ee0

File tree

4 files changed

+69
-21
lines changed

4 files changed

+69
-21
lines changed

src/base.js

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module.exports = function () {
1313
this.opts = Logdown._normalizeOpts(prefix, opts)
1414
this.state = Logdown._getInitialState(this.opts)
1515

16+
Logdown._decorateLoggerMethods(this)
1617
Logdown._instances.push(this)
1718

1819
return this
@@ -86,26 +87,23 @@ module.exports = function () {
8687
return isEnabled
8788
}
8889

89-
//
90-
// Instance
91-
//
92-
93-
var methods = ['debug', 'log', 'info', 'warn', 'error']
94-
methods.forEach(function (method) {
95-
Logdown.prototype[method] = function () {
96-
if (!this.state.isEnabled) {
97-
return
98-
}
90+
Logdown._decorateLoggerMethods = function (instance) {
91+
Object.keys(instance.opts.logger).forEach(function (method) {
92+
instance[method] = function () {
93+
if (!this.state.isEnabled) {
94+
return
95+
}
9996

100-
var args = toArray(arguments)
101-
var preparedOutput = this._prepareOutput(args, method)
97+
var args = toArray(arguments)
98+
var preparedOutput = this._prepareOutput(args, method)
10299

103-
;(this.opts.logger[method] || this.opts.logger.log).apply(
104-
this.opts.logger,
105-
preparedOutput
106-
)
107-
}
108-
}, this)
100+
;(this.opts.logger[method] || this.opts.logger.log).apply(
101+
this.opts.logger,
102+
preparedOutput
103+
)
104+
}
105+
})
106+
}
109107

110108
return Logdown
111109
}

test/browser/logging-methods.test.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ globalObject.localStorage = localStorage
1111
const logdown = require('../../src/browser')
1212
const markdown = require('../../src/markdown/browser')
1313

14-
const methods = ['debug', 'log', 'info', 'warn', 'error']
15-
methods.forEach((method) => {
14+
Object.keys(console).forEach((method) => {
1615
describe('logdown.' + method, () => {
1716
beforeEach(() => {
1817
console[method] = console[method] || console.log
@@ -83,5 +82,14 @@ methods.forEach((method) => {
8382

8483
expect(console[method]).not.toHaveBeenCalled()
8584
})
85+
86+
it('has a facade for every method on opts.logger', () => {
87+
const consoleKeys = Object.keys(console)
88+
const foo = logdown('foo', { logger: console })
89+
90+
consoleKeys.forEach(consoleMethod => {
91+
expect(typeof foo[consoleMethod]).toBe('function')
92+
})
93+
})
8694
})
8795
})

test/common/logging-methods.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* eslint-env jest */
2+
3+
const logdown = require('../../src/node')
4+
5+
//
6+
// Tests
7+
//
8+
9+
Object.keys(console).forEach(method => {
10+
describe(`logdown.${method}`, () => {
11+
beforeEach(() => {
12+
console[method] = console[method] || console.log
13+
console[method] = jest.fn()
14+
15+
logdown._instances = []
16+
process.env.NODE_DEBUG = 'foo'
17+
logdown._setPrefixRegExps()
18+
})
19+
20+
afterEach(() => {
21+
console[method].mockClear()
22+
})
23+
24+
it('has a facade for every method on opts.logger', () => {
25+
const consoleKeys = Object.keys(console)
26+
const foo = logdown('foo', { logger: console })
27+
28+
consoleKeys.forEach(consoleMethod => {
29+
expect(typeof foo[consoleMethod]).toBe('function')
30+
})
31+
})
32+
})
33+
})

test/node/logging-methods.test.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const markdown = require('../../src/markdown/node')
77
// Tests
88
//
99

10-
;['debug', 'log', 'info', 'warn', 'error'].forEach(method => {
10+
Object.keys(console).forEach(method => {
1111
describe(`logdown.${method}`, () => {
1212
beforeEach(() => {
1313
console[method] = console[method] || console.log
@@ -104,5 +104,14 @@ const markdown = require('../../src/markdown/node')
104104

105105
expect(console[method]).not.toHaveBeenCalled()
106106
})
107+
108+
it('has a facade for every method on opts.logger', () => {
109+
const consoleKeys = Object.keys(console)
110+
const foo = logdown('foo', { logger: console })
111+
112+
consoleKeys.forEach(consoleMethod => {
113+
expect(typeof foo[consoleMethod]).toBe('function')
114+
})
115+
})
107116
})
108117
})

0 commit comments

Comments
 (0)