Skip to content

Commit ada6e37

Browse files
author
pooya parsa
committed
feat: custom logger
BREAKING CHANGE: console is replaced by consola by default
1 parent cc85aec commit ada6e37

File tree

5 files changed

+24
-25
lines changed

5 files changed

+24
-25
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ export default class Foo extends Hookable {
3333
constructor() {
3434
// Call to parent to initialize
3535
super()
36+
// Initialize Hookable with custom logger
37+
// super(consola)
3638
}
3739

3840
async someFunction() {
@@ -62,6 +64,15 @@ lib.hookObj({
6264

6365
## Hookable class
6466

67+
### `constructor(logger)`
68+
69+
Custom logger. Default logger is `console` but you can use your own or [consola](https://github.com/nuxt/consola).
70+
71+
It should be an object implementing following functions:
72+
- log
73+
- warn
74+
- error
75+
- debug
6576

6677
### `hook (name, fn)`
6778

lib/hookable.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const consola = require('consola')
21
const { serial, flatHooks } = require('./utils')
32

43
module.exports = class Hookable {
5-
constructor () {
4+
constructor (logger = console) {
5+
this.logger = logger
66
this._hooks = {}
77
this._deprecatedHooks = {}
88

@@ -16,7 +16,7 @@ module.exports = class Hookable {
1616
}
1717

1818
if (this._deprecatedHooks[name]) {
19-
consola.warn(`${name} hook has been deprecated, please use ${this._deprecatedHooks[name]}`)
19+
this.logger.warn(`${name} hook has been deprecated, please use ${this._deprecatedHooks[name]}`)
2020
name = this._deprecatedHooks[name]
2121
}
2222

@@ -43,7 +43,7 @@ module.exports = class Hookable {
4343
await serial(this._hooks[name], fn => fn(...args))
4444
} catch (err) {
4545
name !== 'error' && await this.callHook('error', err)
46-
consola.error(err)
46+
this.logger.error(err)
4747
}
4848
}
4949

package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@
2626
"prepublish": "yarn test",
2727
"release": "standard-version && git push --follow-tags && npm publish"
2828
},
29-
"dependencies": {
30-
"consola": "^2.4.0"
31-
},
3229
"devDependencies": {
3330
"codecov": "^3.0.0",
3431
"eslint": "^5.13.0",

test/hookable.test.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
const consola = require('consola')
21
const Hookable = require('..')
32
const { flatHooks } = require('../lib/utils')
43

54
describe('core: hookable', () => {
65
beforeEach(() => {
7-
consola.mockTypes(() => jest.fn())
6+
['log', 'warn', 'error', 'debug'].forEach(l => {
7+
console[l] = jest.fn()
8+
})
89
})
910

1011
test('should construct hook object', () => {
@@ -51,26 +52,26 @@ describe('core: hookable', () => {
5152

5253
hook.hook('test:hook', () => { })
5354

54-
expect(consola.warn).toBeCalledWith('test:hook hook has been deprecated, please use test:before')
55+
expect(console.warn).toBeCalledWith('test:hook hook has been deprecated, please use test:before')
5556
expect(hook._hooks['test:hook']).toBeUndefined()
5657
expect(hook._hooks['test:before']).toEqual([expect.any(Function)])
5758
})
5859

5960
test('should call registered hook', async () => {
6061
const hook = new Hookable()
61-
hook.hook('test:hook', () => consola.log('test:hook called'))
62+
hook.hook('test:hook', () => console.log('test:hook called'))
6263

6364
await hook.callHook('test:hook')
6465

65-
expect(consola.log).toBeCalledWith('test:hook called')
66+
expect(console.log).toBeCalledWith('test:hook called')
6667
})
6768

6869
test('should ignore unregistered hook', async () => {
6970
const hook = new Hookable()
7071

7172
await hook.callHook('test:hook')
7273

73-
expect(consola.debug).not.toBeCalled()
74+
expect(console.debug).not.toBeCalled()
7475
})
7576

7677
test('should report hook error', async () => {
@@ -80,7 +81,7 @@ describe('core: hookable', () => {
8081

8182
await hook.callHook('test:hook')
8283

83-
expect(consola.error).toBeCalledWith(error)
84+
expect(console.error).toBeCalledWith(error)
8485
})
8586

8687
test('should call error hook', async () => {
@@ -92,7 +93,7 @@ describe('core: hookable', () => {
9293
await hook.callHook('test:hook')
9394

9495
expect(hook._hooks.error[0]).toBeCalledWith(error)
95-
expect(consola.error).toBeCalledWith(error)
96+
expect(console.error).toBeCalledWith(error)
9697
})
9798

9899
test('should clear registered hooks', () => {

yarn.lock

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -845,11 +845,6 @@ concat-stream@^1.6.0:
845845
readable-stream "^2.2.2"
846846
typedarray "^0.0.6"
847847

848-
consola@^2.4.0:
849-
version "2.5.6"
850-
resolved "https://registry.yarnpkg.com/consola/-/consola-2.5.6.tgz#5ce14dbaf6f5b589c8a258ef80ed97b752fa57d5"
851-
integrity sha512-DN0j6ewiNWkT09G3ZoyyzN3pSYrjxWcx49+mHu+oDI5dvW5vzmyuzYsqGS79+yQserH9ymJQbGzeqUejfssr8w==
852-
853848
contains-path@^0.1.0:
854849
version "0.1.0"
855850
resolved "https://registry.yarnpkg.com/contains-path/-/contains-path-0.1.0.tgz#fe8cf184ff6670b6baef01a9d4861a5cbec4120a"
@@ -2302,11 +2297,6 @@ istanbul-reports@^2.1.1:
23022297
dependencies:
23032298
handlebars "^4.1.0"
23042299

2305-
items-promise@^1.0.0:
2306-
version "1.0.0"
2307-
resolved "https://registry.yarnpkg.com/items-promise/-/items-promise-1.0.0.tgz#a71ff03063078eb82db468f1e2f12df4da34dc01"
2308-
integrity sha512-t+3w4E51IlviTn1b/gesEsH9Jf0gEs2+VhSbwgMaVyK2IxXYSwZkL6sOmr+FBhMT0sSFzcc4Rn5X7sz6NvS2GQ==
2309-
23102300
jest-changed-files@^24.5.0:
23112301
version "24.5.0"
23122302
resolved "https://registry.yarnpkg.com/jest-changed-files/-/jest-changed-files-24.5.0.tgz#4075269ee115d87194fd5822e642af22133cf705"

0 commit comments

Comments
 (0)