Skip to content

Commit 6521f85

Browse files
committed
Initial commit
0 parents  commit 6521f85

File tree

9 files changed

+353
-0
lines changed

9 files changed

+353
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/dist/
2+
/node_modules/
3+
.idea
4+
coverage/
5+
package-lock.json

.npmignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/src/
2+
.idea
3+
coverage

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
language: node_js
2+
node_js:
3+
- "7.7.1"
4+
cache:
5+
directories:
6+
- "node_modules"

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2017 Justin Kames
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# vuejs-logger
2+
![](https://travis-ci.org/justinkames/vuejs-logger.svg?branch=master)
3+
4+
> vue-js logger works Vue 2.
5+
6+
## Table of Contents
7+
8+
- [Introduction](#introduction)
9+
- [Demo](#demo)
10+
- [Install](#install)
11+
- [Usage](#usage)
12+
- [Maintainers](#maintainers)
13+
- [Contribute](#contribute)
14+
- [License](#license)
15+
16+
17+
## Introduction
18+
19+
vuejs-logger is a logging library that enables logging for Vue applications. It restricts log messages that are higher the specified log level. Features include :
20+
21+
- Colored console messages for $log.warning, $log.error and $log.fatal.
22+
- Possibility to automatically JSON.stringify() the properties passed to the logger.
23+
- Possibility to display the log level in the console.
24+
25+
```js
26+
logLevels : ['debug', 'info', 'warn', 'error', 'fatal']
27+
```
28+
## Demo
29+
30+
@ [https://www.webpackbin.com/bins/-KpB0UbGiG2PeFDmqjwi](https://www.webpackbin.com/bins/-KpB0UbGiG2PeFDmqjwi)
31+
32+
## Install
33+
34+
This project uses [node](http://nodejs.org) and [npm](https://npmjs.com). Go check them out if you don't have them locally installed.
35+
36+
```sh
37+
$ npm install vuejs-logger --save-exact
38+
```
39+
40+
## Usage
41+
42+
Below you can find an example of how to use vuejs-logger :
43+
44+
```js
45+
import VueLogger from 'vuejs-logger'
46+
47+
const options = {
48+
logLevel : 'debug',
49+
// optional : defaults to false if not specified
50+
stringifyArguments : false,
51+
// optional : defaults to false if not specified
52+
showLogLevel : false
53+
}
54+
55+
Vue.use(VueLogger, options)
56+
```
57+
58+
```js
59+
new Vue({
60+
data() {
61+
return {
62+
a : 'a',
63+
b : 'b'
64+
}
65+
},
66+
67+
created() {
68+
this.$log.debug("test", this.a, 123)
69+
this.$log.info("test", this.b)
70+
this.$log.warn("test")
71+
this.$log.error("test")
72+
this.$log.fatal("test")
73+
}
74+
})
75+
```
76+
77+
## Maintainers
78+
79+
[@justinkames](https://github.com/justinkames).
80+
81+
## Contribute
82+
83+
Feel free to dive in! [Open an issue]() or submit PRs.
84+
85+
vuejs-logger follows the [Contributor Covenant](http://contributor-covenant.org/version/1/3/0/) Code of Conduct.
86+
87+
## License
88+
89+
[MIT](LICENSE) © Justin Kames

package.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "vuejs-logger",
3+
"author": "Justin Kames",
4+
"version": "1.0.0",
5+
"description": "Vuejs-logger, a provides logging functionality for Vue.js.",
6+
"main": "dist/index.js",
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/justinkames/vuejs-logger.git"
10+
},
11+
"scripts": {
12+
"test": "istanbul cover _mocha -- --compilers js:babel-core/register -R spec --debug",
13+
"transpile": "node_modules/babel-cli/bin/babel.js src --out-dir dist && node_modules/uglify-js/bin/uglifyjs dist/logger.js -c -m -o dist/logger.js",
14+
"prepublish": "npm test && npm run transpile"
15+
},
16+
"license": "MIT",
17+
"babel": {
18+
"presets": [
19+
"es2015"
20+
]
21+
},
22+
"devDependencies": {
23+
"babel-cli": "6.24.1",
24+
"babel-core": "6.25.0",
25+
"babel-loader": "7.1.1",
26+
"babel-preset-es2015": "6.24.1"
27+
},
28+
"dependencies": {
29+
"chai": "4.1.0",
30+
"istanbul": "0.4.5",
31+
"mocha": "3.4.2",
32+
"uglify": "0.1.5",
33+
"vuejs-logger": "0.1.11"
34+
}
35+
}

src/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import Logger from './logger'
2+
const o = {
3+
install: Logger().install
4+
}
5+
window.VueLogger = o
6+
export default o

src/logger.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
export default function () {
2+
3+
const logLevels = ['debug', 'info', 'warn', 'error', 'fatal']
4+
5+
function initLoggerInstance (options, logLevels) {
6+
const logger = {}
7+
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+
})
18+
return logger
19+
}
20+
21+
function print (logLevel, prefix, formattedArguments) {
22+
if (logLevel === 'warn' || logLevel === 'error' || logLevel === 'fatal') {
23+
console[logLevel === 'fatal' ? 'error' : logLevel](prefix, ...formattedArguments)
24+
} else {
25+
console.log(prefix, ...formattedArguments)
26+
}
27+
}
28+
29+
function validateOptions (options, logLevels) {
30+
if (!(options.logLevel && typeof options.logLevel === 'string' && logLevels.indexOf(options.logLevel) > -1)) {
31+
return false
32+
}
33+
if (options.stringifyArguments && typeof options.stringifyArguments !== 'boolean') {
34+
return false
35+
}
36+
if (options.showLogLevel && typeof options.showLogLevel !== 'boolean') {
37+
return false
38+
}
39+
return true
40+
}
41+
42+
function install (Vue, options) {
43+
if (validateOptions(options, logLevels)) {
44+
Vue.prototype.$log = initLoggerInstance(options, logLevels)
45+
} else {
46+
throw new Error('Provided options for vuejs-logger are not valid.')
47+
}
48+
}
49+
50+
return {
51+
install,
52+
validateOptions,
53+
print,
54+
initLoggerInstance,
55+
logLevels
56+
}
57+
}

test/logger.js

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import chai from 'chai'
2+
const expect = chai.expect
3+
import VueLogger from '../src/logger'
4+
const {print, validateOptions, initLoggerInstance, logLevels, install} = VueLogger()
5+
6+
describe('Logger.js', () => {
7+
8+
describe('initLoggerInstance()', () => {
9+
10+
it('initLoggerInstance() should ignore logLevels that are more restrictive than set logLevel.', () => {
11+
// with options enabled
12+
const a = initLoggerInstance({logLevel: 'info', showLogLevel: true, stringifyArguments: true}, logLevels)
13+
a.debug('test')
14+
a.info('test', ['test args'], ['test args'])
15+
a.warn('test', ['test args'])
16+
a.error('test', 'test args')
17+
a.fatal('test', ['test args'], 'test args', 'test args')
18+
19+
// with options disabled
20+
const b = initLoggerInstance({logLevel: 'info', showLogLevel: false, stringifyArguments: false}, logLevels)
21+
b.debug('test')
22+
b.info('test', ['test args'], ['test args'])
23+
b.warn('test', ['test args'])
24+
b.error('test', 'test args')
25+
b.fatal('test', ['test args'], 'test args', 'test args')
26+
})
27+
})
28+
29+
describe('validateOptions()', () => {
30+
31+
it('validateOptions() should pass with correct options.', () => {
32+
expect(validateOptions({
33+
logLevel: 'debug',
34+
stringifyByDefault: false,
35+
showLogLevel: false,
36+
}, logLevels)).to.equal(true)
37+
})
38+
39+
it('validateOptions() should fail with incorrect options.', () => {
40+
expect(validateOptions({
41+
logLevel: 'debug',
42+
stringifyArguments: 'TEST',
43+
showLogLevel: false,
44+
}, logLevels)).to.equal(false)
45+
46+
expect(validateOptions({
47+
logLevel: 'debug',
48+
stringifyArguments: false,
49+
showLogLevel: 'TEST',
50+
}, logLevels)).to.equal(false)
51+
52+
expect(validateOptions({
53+
logLevel: 'TEST',
54+
stringifyArguments: false,
55+
showLogLevel: false,
56+
}, logLevels)).to.equal(false)
57+
})
58+
})
59+
60+
describe('print()', () => {
61+
62+
it('print() should use console.error() with fatal logLevel', () => {
63+
print('fatal', 'fatal |', ['test'])
64+
})
65+
66+
it('print() should use console.error() with error logLevel', () => {
67+
print('error', 'error |', ['test'])
68+
})
69+
70+
it('print() should use console.log() with any other logLevel', () => {
71+
print('debug', 'debug |', ['test'])
72+
})
73+
})
74+
75+
describe('install()', () => {
76+
77+
it('install() should work with the correct params.', () => {
78+
const Vue = () => {}
79+
const options = {
80+
logLevel: 'debug',
81+
stringifyByDefault: false,
82+
showLogLevel: false,
83+
}
84+
install(Vue, options)
85+
Vue()
86+
87+
expect(Vue.prototype.$log).to.be.a('object')
88+
expect(Vue.prototype.$log.debug).to.be.a('function')
89+
expect(Vue.prototype.$log.info).to.be.a('function')
90+
expect(Vue.prototype.$log.warn).to.be.a('function')
91+
expect(Vue.prototype.$log.error).to.be.a('function')
92+
expect(Vue.prototype.$log.fatal).to.be.a('function')
93+
})
94+
95+
it('install() should throw an error with the an incorrect log level.', () => {
96+
const options = {
97+
logLevel: 'foo'
98+
}
99+
expect(() => { install(() => {}, options) })
100+
.to
101+
.throw(Error, 'Provided options for vuejs-logger are not valid.')
102+
})
103+
104+
it('install() should throw an error with the an incorrect log level.', () => {
105+
const options = {
106+
logLevel: false
107+
}
108+
expect(() => { install(() => {}, options) })
109+
.to
110+
.throw(Error, 'Provided options for vuejs-logger are not valid.')
111+
})
112+
113+
it('install() should throw an error with the an incorrect log level.', () => {
114+
const options = {
115+
logLevel: undefined
116+
}
117+
expect(() => { install(() => {}, options) })
118+
.to
119+
.throw(Error, 'Provided options for vuejs-logger are not valid.')
120+
})
121+
122+
it('install() should throw an error with the an incorrect log level.', () => {
123+
const options = {
124+
logLevel: null
125+
}
126+
expect(() => { install(() => {}, options) })
127+
.to
128+
.throw(Error, 'Provided options for vuejs-logger are not valid.')
129+
})
130+
})
131+
})

0 commit comments

Comments
 (0)