Skip to content

Commit 9c1b569

Browse files
committed
Merge branch 're2005-is-enabled-flag'
2 parents 9ecff59 + b1db886 commit 9c1b569

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

Diff for: README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ vuejs-logger is a logging library that enables logging for Vue applications. It
3434
logLevels : ['debug', 'info', 'warn', 'error', 'fatal']
3535
```
3636

37+
Option to disable the logger (for using it on PRODUCTION)
38+
39+
```js
40+
isEnabled : false
41+
```
42+
43+
3744
## Install
3845

3946
This project uses [node](http://nodejs.org) and [npm](https://npmjs.com). Go check them out if you don't have them locally installed!
@@ -52,8 +59,10 @@ Below you can find an example of how to use vuejs-logger :
5259
import VueLogger from 'vuejs-logger'
5360

5461
const options = {
62+
// optional : defaults to true if not specified
63+
isEnabled: true,
5564
// required ['debug', 'info', 'warn', 'error', 'fatal']
56-
logLevel : 'debug',
65+
logLevel : 'debug',
5766
// optional : defaults to false if not specified
5867
stringifyArguments : false,
5968
// optional : defaults to false if not specified

Diff for: src/logger.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export default (function () {
22

33
const defaultOptions = {
4+
isEnabled: true,
45
logLevel: 'debug',
56
separator: '|',
67
stringifyArguments: false,
@@ -14,7 +15,8 @@ export default (function () {
1415
function initLoggerInstance (options, logLevels) {
1516
const logger = {}
1617
logLevels.forEach(logLevel => {
17-
if (logLevels.indexOf(logLevel) >= logLevels.indexOf(options.logLevel)) {
18+
if (logLevels.indexOf(logLevel) >= logLevels.indexOf(options.logLevel) &&
19+
options.isEnabled) {
1820
logger[logLevel] = (...args) => {
1921
let methodName = getMethodName()
2022
const methodNamePrefix = options.showMethodName ? methodName + ` ${options.separator} ` : ''
@@ -55,6 +57,9 @@ export default (function () {
5557
if (options.separator && (typeof options.separator !== 'string' || (typeof options.separator === 'string' && options.separator.length > 3))) {
5658
return false
5759
}
60+
if (typeof options.isEnabled !== 'boolean') {
61+
return false
62+
}
5863
return !(options.showMethodName && typeof options.showMethodName !== 'boolean')
5964
}
6065

Diff for: test/logger.js

+60
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ describe('Logger.js', () => {
99
describe('getMethodName()', () => {
1010
it('Should show the parent function.', () => {
1111
const options = {
12+
isEnabled: true,
1213
logLevel: 'debug',
1314
stringifyByDefault: false,
1415
showLogLevel: false,
@@ -40,6 +41,7 @@ describe('Logger.js', () => {
4041

4142
it('install() should work with the correct params.', () => {
4243
const options = {
44+
isEnabled: true,
4345
logLevel: 'debug',
4446
stringifyByDefault: false,
4547
showLogLevel: false,
@@ -100,6 +102,15 @@ describe('Logger.js', () => {
100102
.to
101103
.throw(Error, 'Provided options for vuejs-logger are not valid.')
102104
})
105+
106+
it('install() should throw an error with the an incorrect isEnabled value', () => {
107+
const options = {
108+
isEnabled: null
109+
}
110+
expect(() => { install(Vue, options) })
111+
.to
112+
.throw(Error, 'Provided options for vuejs-logger are not valid.')
113+
})
103114
})
104115

105116
describe('initLoggerInstance()', () => {
@@ -140,6 +151,7 @@ describe('Logger.js', () => {
140151

141152
it('isValidOptions() should pass with correct options.', () => {
142153
expect(isValidOptions({
154+
isEnabled: true,
143155
logLevel: 'debug',
144156
stringifyByDefault: false,
145157
showLogLevel: false,
@@ -152,6 +164,7 @@ describe('Logger.js', () => {
152164
it('isValidOptions() should fail with incorrect options.', () => {
153165

154166
expect(isValidOptions({
167+
isEnabled: true,
155168
logLevel: 'debug',
156169
stringifyByDefault: false,
157170
showLogLevel: false,
@@ -161,6 +174,7 @@ describe('Logger.js', () => {
161174
}, logLevels)).to.equal(false)
162175

163176
expect(isValidOptions({
177+
isEnabled: true,
164178
logLevel: 'debug',
165179
stringifyByDefault: false,
166180
showLogLevel: false,
@@ -170,6 +184,7 @@ describe('Logger.js', () => {
170184
}, logLevels)).to.equal(false)
171185

172186
expect(isValidOptions({
187+
isEnabled: true,
173188
logLevel: 'debug',
174189
stringifyArguments: false,
175190
showLogLevel: false,
@@ -179,6 +194,7 @@ describe('Logger.js', () => {
179194
}, logLevels)).to.equal(false)
180195

181196
expect(isValidOptions({
197+
isEnabled: true,
182198
logLevel: 'debug',
183199
stringifyArguments: 'TEST',
184200
showLogLevel: false,
@@ -188,6 +204,7 @@ describe('Logger.js', () => {
188204
}, logLevels)).to.equal(false)
189205

190206
expect(isValidOptions({
207+
isEnabled: true,
191208
logLevel: 'debug',
192209
stringifyArguments: false,
193210
showLogLevel: 'TEST',
@@ -197,6 +214,7 @@ describe('Logger.js', () => {
197214
}, logLevels)).to.equal(false)
198215

199216
expect(isValidOptions({
217+
isEnabled: true,
200218
logLevel: 'TEST',
201219
stringifyArguments: false,
202220
showLogLevel: false,
@@ -207,13 +225,55 @@ describe('Logger.js', () => {
207225

208226
expect(isValidOptions({
209227
logLevel: 'debug',
228+
isEnabled: true,
210229
}, logLevels)).to.equal(true)
211230

212231
expect(isValidOptions({
232+
isEnabled: '',
213233
logLevel: 'debug',
214234
separator: '1234',
215235
}, logLevels)).to.equal(false)
216236

237+
expect(isValidOptions({
238+
isEnabled: true,
239+
logLevel: 'debug',
240+
stringifyArguments: false,
241+
showLogLevel: false,
242+
showMethodName: false,
243+
separator: '|',
244+
showConsoleColors: false,
245+
}, logLevels)).to.equal(true)
246+
247+
expect(isValidOptions({
248+
isEnabled: false,
249+
logLevel: 'debug',
250+
stringifyArguments: false,
251+
showLogLevel: false,
252+
showMethodName: false,
253+
separator: '|',
254+
showConsoleColors: false,
255+
}, logLevels)).to.equal(true)
256+
257+
expect(isValidOptions({
258+
isEnabled: '',
259+
logLevel: 'debug',
260+
stringifyArguments: false,
261+
showLogLevel: false,
262+
showMethodName: false,
263+
separator: '|',
264+
showConsoleColors: false,
265+
}, logLevels)).to.equal(false)
266+
267+
expect(isValidOptions({
268+
isEnabled: null,
269+
logLevel: 'debug',
270+
stringifyArguments: false,
271+
showLogLevel: false,
272+
showMethodName: false,
273+
separator: '|',
274+
showConsoleColors: false,
275+
}, logLevels)).to.equal(false)
276+
217277
})
218278
})
219279

0 commit comments

Comments
 (0)