Skip to content

Commit d4ddfa8

Browse files
committedOct 16, 2017
Added the configurable option to display the method name from where the $log method is called.
Updated the README.md.
1 parent d03a86d commit d4ddfa8

File tree

5 files changed

+119
-54
lines changed

5 files changed

+119
-54
lines changed
 

‎.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
/node_modules/
33
.idea
44
coverage/
5-
package-lock.json
5+
package-lock.json
6+
sample-project

‎README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ const options = {
5656
// optional : defaults to false if not specified
5757
stringifyArguments : false,
5858
// optional : defaults to false if not specified
59-
showLogLevel : false
59+
showLogLevel : false,
60+
// optional : defaults to false if not specified
61+
showMethodName : false
6062
}
6163

6264
Vue.use(VueLogger, options)

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "vuejs-logger",
33
"author": "Justin Kames",
4-
"version": "1.1.4",
4+
"version": "1.2.0",
55
"description": "Vuejs-logger, provides customizable logging functionality for Vue.js.",
66
"main": "dist/index.js",
77
"repository": {

‎src/logger.js

+35-19
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,36 @@
1-
export default (() => {
1+
export default (function () {
22

33
const logLevels = ['debug', 'info', 'warn', 'error', 'fatal']
44

55
function initLoggerInstance (options, logLevels) {
66
const logger = {}
77
logLevels.forEach(logLevel => {
8-
if (logLevels.indexOf(logLevel) >= logLevels.indexOf(options.logLevel)) {
9-
logger[logLevel] = (...args) => {
10-
const prefix = options.showLogLevel ? logLevel + ' | ' : ''
11-
const formattedArguments = options.stringifyArguments ? args.map(a => JSON.stringify(a)) : args
12-
print(logLevel, prefix, formattedArguments)
13-
}
14-
} else {
15-
logger[logLevel] = () => {}
16-
}
17-
})
8+
if (logLevels.indexOf(logLevel) >= logLevels.indexOf(options.logLevel)) {
9+
logger[logLevel] = (...args) => {
10+
let methodName = getMethodName()
11+
const methodNamePrefix = options.showMethodName ? methodName + ' | ' : ''
12+
const logLevelPrefix = options.showLogLevel ? logLevel + ' | ' : ''
13+
const formattedArguments = options.stringifyArguments ? args.map(a => JSON.stringify(a)) : args
14+
print(logLevel, logLevelPrefix, methodNamePrefix, formattedArguments)
15+
}
16+
}
17+
else {
18+
logger[logLevel] = () => {}
19+
}
20+
}
21+
)
1822
return logger
1923
}
2024

21-
function print (logLevel, prefix, formattedArguments) {
25+
function print (logLevel = false, logLevelPrefix = false, methodNamePrefix = false, formattedArguments = false) {
2226
if (logLevel === 'warn' || logLevel === 'error' || logLevel === 'fatal') {
23-
console[logLevel === 'fatal' ? 'error' : logLevel](prefix, ...formattedArguments)
27+
console[logLevel === 'fatal' ? 'error' : logLevel](logLevelPrefix, methodNamePrefix, ...formattedArguments)
2428
} else {
25-
console.log(prefix, ...formattedArguments)
29+
console.log(logLevelPrefix, methodNamePrefix, ...formattedArguments)
2630
}
2731
}
2832

29-
function validateOptions (options, logLevels) {
33+
function isValidOptions (options, logLevels) {
3034
if (!(options.logLevel && typeof options.logLevel === 'string' && logLevels.indexOf(options.logLevel) > -1)) {
3135
return false
3236
}
@@ -36,23 +40,35 @@ export default (() => {
3640
if (options.showLogLevel && typeof options.showLogLevel !== 'boolean') {
3741
return false
3842
}
39-
return true
43+
return !(options.showMethodName && typeof options.showMethodName !== 'boolean')
4044
}
4145

4246
function install (Vue, options) {
43-
if (validateOptions(options, logLevels)) {
47+
if (isValidOptions(options, logLevels)) {
4448
Vue.$log = initLoggerInstance(options, logLevels)
4549
Vue.prototype.$log = Vue.$log
4650
} else {
4751
throw new Error('Provided options for vuejs-logger are not valid.')
4852
}
4953
}
5054

55+
function getMethodName () {
56+
let stackTrace = Error().stack.split('\n')[3]
57+
if (/ /.test(stackTrace)) {
58+
stackTrace = stackTrace.trim().split(' ')[1]
59+
}
60+
if (stackTrace.includes('.')) {
61+
stackTrace = stackTrace.split('.')[1]
62+
}
63+
return stackTrace
64+
}
65+
5166
return {
5267
install,
53-
validateOptions,
68+
isValidOptions,
5469
print,
5570
initLoggerInstance,
5671
logLevels
5772
}
58-
})()
73+
})
74+
()

‎test/logger.js

+78-32
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,49 @@
1-
import chai from 'chai'
2-
import Vue from 'vue'
1+
import Vue from 'vue/dist/vue.min'
32
import VueLogger from '../src/logger'
3+
import chai from 'chai'
44

5+
const {print, isValidOptions, initLoggerInstance, logLevels, install} = VueLogger
56
const expect = chai.expect
6-
const {print, validateOptions, initLoggerInstance, logLevels, install} = VueLogger
77

88
describe('Logger.js', () => {
9-
10-
describe('install()', () => {
11-
12-
it('install() should work with the correct params.', () => {
9+
describe('getMethodName()', () => {
10+
it('Should show the parent function.', () => {
1311
const options = {
1412
logLevel: 'debug',
1513
stringifyByDefault: false,
1614
showLogLevel: false,
15+
showMethodName: true
1716
}
18-
1917
Vue.use({install}, options)
2018

2119
new Vue({
22-
created() {
23-
expect(this.$log).to.be.a('object')
24-
expect(this.$log.debug).to.be.a('function')
25-
expect(this.$log.info).to.be.a('function')
26-
expect(this.$log.warn).to.be.a('function')
27-
expect(this.$log.error).to.be.a('function')
28-
expect(this.$log.fatal).to.be.a('function')
20+
created () {
21+
this.foo()
22+
},
23+
template: '<bar></bar>',
24+
methods: {
25+
foo () {
26+
this.$log.fatal('first level call.')
27+
externalFunction()
28+
}
2929
}
3030
})
3131

32+
function externalFunction () {
33+
Vue.$log.fatal('calling external function.')
34+
}
35+
})
36+
})
37+
describe('install()', () => {
38+
39+
it('install() should work with the correct params.', () => {
40+
const options = {
41+
logLevel: 'debug',
42+
stringifyByDefault: false,
43+
showLogLevel: false,
44+
showMethodName: false
45+
}
46+
Vue.use({install}, options)
3247
expect(Vue.$log).to.be.a('object')
3348
expect(Vue.$log.debug).to.be.a('function')
3449
expect(Vue.$log.info).to.be.a('function')
@@ -37,6 +52,15 @@ describe('Logger.js', () => {
3752
expect(Vue.$log.fatal).to.be.a('function')
3853
})
3954

55+
it('install() should throw an error with the an incorrect log showMethodName parameter.', () => {
56+
const options = {
57+
showMethodName: 'foo'
58+
}
59+
expect(() => { install(Vue, options) })
60+
.to
61+
.throw(Error, 'Provided options for vuejs-logger are not valid.')
62+
})
63+
4064
it('install() should throw an error with the an incorrect log level.', () => {
4165
const options = {
4266
logLevel: 'foo'
@@ -76,17 +100,26 @@ describe('Logger.js', () => {
76100

77101
describe('initLoggerInstance()', () => {
78102

79-
it('initLoggerInstance() should ignore logLevels that are more restrictive than set logLevel.', () => {
103+
it('initLoggerInstance() should ignore logLevels that are more restrictive than set logLevel.', function testFn () {
80104
// with options enabled
81-
const a = initLoggerInstance({logLevel: 'info', showLogLevel: true, stringifyArguments: true}, logLevels)
105+
const a = initLoggerInstance({
106+
logLevel: 'info',
107+
showLogLevel: true,
108+
stringifyArguments: true,
109+
showMethodName: false
110+
}, logLevels)
82111
a.debug('test')
83112
a.info('test', ['test args'], ['test args'])
84113
a.warn('test', ['test args'])
85114
a.error('test', 'test args')
86115
a.fatal('test', ['test args'], 'test args', 'test args')
87-
88116
// with options disabled
89-
const b = initLoggerInstance({logLevel: 'info', showLogLevel: false, stringifyArguments: false}, logLevels)
117+
const b = initLoggerInstance({
118+
logLevel: 'info',
119+
showLogLevel: false,
120+
stringifyArguments: false,
121+
showMethodName: true
122+
}, logLevels)
90123
b.debug('test')
91124
b.info('test', ['test args'], ['test args'])
92125
b.warn('test', ['test args'])
@@ -95,49 +128,62 @@ describe('Logger.js', () => {
95128
})
96129
})
97130

98-
describe('validateOptions()', () => {
131+
describe('isValidOptions()', () => {
99132

100-
it('validateOptions() should pass with correct options.', () => {
101-
expect(validateOptions({
133+
it('isValidOptions() should pass with correct options.', () => {
134+
expect(isValidOptions({
102135
logLevel: 'debug',
103136
stringifyByDefault: false,
104137
showLogLevel: false,
138+
showMethodName: true
105139
}, logLevels)).to.equal(true)
106140
})
107141

108-
it('validateOptions() should fail with incorrect options.', () => {
109-
expect(validateOptions({
142+
it('isValidOptions() should fail with incorrect options.', () => {
143+
144+
expect(isValidOptions({
145+
logLevel: 'debug',
146+
stringifyArguments: false,
147+
showLogLevel: false,
148+
showMethodName: 'TEST',
149+
}, logLevels)).to.equal(false)
150+
151+
expect(isValidOptions({
110152
logLevel: 'debug',
111153
stringifyArguments: 'TEST',
112154
showLogLevel: false,
155+
showMethodName: false,
113156
}, logLevels)).to.equal(false)
114157

115-
expect(validateOptions({
158+
expect(isValidOptions({
116159
logLevel: 'debug',
117160
stringifyArguments: false,
118161
showLogLevel: 'TEST',
162+
showMethodName: false,
119163
}, logLevels)).to.equal(false)
120164

121-
expect(validateOptions({
165+
expect(isValidOptions({
122166
logLevel: 'TEST',
123167
stringifyArguments: false,
124168
showLogLevel: false,
169+
showMethodName: false,
125170
}, logLevels)).to.equal(false)
171+
172+
expect(isValidOptions({
173+
logLevel: 'debug',
174+
}, logLevels)).to.equal(true)
126175
})
127176
})
128177

129178
describe('print()', () => {
130-
131179
it('print() should use console.error() with fatal logLevel', () => {
132-
print('fatal', 'fatal |', ['test'])
180+
print('fatal', 'fatal |', 'METHOD NAME | ', ['test'])
133181
})
134-
135182
it('print() should use console.error() with error logLevel', () => {
136-
print('error', 'error |', ['test'])
183+
print('error', 'error |', 'METHOD NAME |', ['test'])
137184
})
138-
139185
it('print() should use console.log() with any other logLevel', () => {
140-
print('debug', 'debug |', ['test'])
186+
print('debug', 'debug |', 'METHOD NAME |', ['test'])
141187
})
142188
})
143189
})

0 commit comments

Comments
 (0)
Failed to load comments.