Skip to content

Commit ade1557

Browse files
committed
feat: log levels
You can configure `pwc` to output logs for only certain levels. By default, the log level is a `0` which also represents `"log"` or `"debug"` which outputs logs from all levels. You can set the log level on `pwc` before it is used like this: ```javascript pwc.level = 0 // 'log', 'debug' => all logs pwc.level = 1 // 'info' => info, warn, and error logs pwc.level = 2 // 'warn' => warn and error logs pwc.level = 3 // 'error' => only error logs pwc.level = -1 // 'none' => no logs // after setting a custom log level, you can proceed to logging pwc().blue().log('hi') ``` For custom loggers, a 3rd argument (in addition to the message and css styles object) is passed which is the log level.
1 parent 79cbbd3 commit ade1557

8 files changed

Lines changed: 228 additions & 42 deletions

File tree

README.md

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,21 +92,38 @@ pwc({ shadow: '4px 4px 5px green' }).large().error('large error msg with green s
9292
pwc({ weight: 'bold', color: '#00f' }).size(20).log('bold, blue, and 20px msg')
9393
```
9494

95-
Have fun with making your logs pretty!
96-
9795
## Log Levels
9896

9997
* `.log()`
98+
* `.debug()`
99+
* `.info()`
100100
* `.warn()`
101101
* `.error()`
102-
* `.info()`
103-
* `.debug()`
102+
103+
You can configure `pwc` to output logs for only certain levels. By default, the log level is a `0` which also represents `"log"` or `"debug"` which outputs logs from all levels. You can set the log level on `pwc` before it is used like this:
104+
105+
```javascript
106+
pwc.level = 0 // 'log', 'debug' => all logs
107+
108+
pwc.level = 1 // 'info' => info, warn, and error logs
109+
110+
pwc.level = 2 // 'warn' => warn and error logs
111+
112+
pwc.level = 3 // 'error' => only error logs
113+
114+
pwc.level = -1 // 'none' => no logs
115+
116+
// after setting a custom log level, you can proceed to logging
117+
pwc().blue().log('hi')
118+
```
104119

105120
## Custom Logger
106121

107122
By default, the logger used is the standard browser console. But, you may pass in your own logger if you want.
108123

109-
For example, let's say you have a logger function named `customWarnFn`. You just need to pass it to the log function as the second argument i.e. `pwc().blue().bold().underline().warn('hi', customWarnFn)`. This passes the message and a css styles object as arguments to your custom logger. The styles object for the example would look like this:
124+
For example, let's say you have a logger function named `customWarnFn`. You just need to pass it to the log function as the second argument i.e. `pwc().blue().bold().underline().warn('hi', customWarnFn)`. This passes the **message**, a **css styles object**, and the **log level** as arguments to your custom logger.
125+
126+
The styles object for the example looks like this:
110127

111128
```javascript
112129
{

src/Store.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1+
import getLevel from './getLevel'
2+
13
class Store {
24
constructor () {
35
this.style = ''
46
this.styleFns = {}
7+
this.logLevel = 0
58
}
69

710
appendStyle (newStyle) {
811
this.style += newStyle
912
}
13+
14+
set level (val) {
15+
this.logLevel = getLevel(val)
16+
}
1017
}
1118

1219
export default Store

src/getLevel.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { LOG, DEBUG, INFO, WARN, ERROR, NONE } from './levels'
2+
3+
function getLevel (val) {
4+
let level = LOG
5+
6+
if (typeof val === 'string') {
7+
switch (val.toLowerCase()) {
8+
case 'debug':
9+
level = DEBUG; break
10+
case 'info':
11+
level = INFO; break
12+
case 'warn':
13+
level = WARN; break
14+
case 'error':
15+
level = ERROR; break
16+
case 'none':
17+
level = NONE; break
18+
}
19+
} else {
20+
const eVal = +val
21+
if (eVal >= NONE && eVal <= ERROR) {
22+
level = eVal
23+
}
24+
}
25+
26+
return level
27+
}
28+
29+
export default getLevel

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import readConfig from './readConfig'
44

55
function pwc (config) {
66
const store = new Store()
7+
store.level = pwc.level
78
if (typeof config === 'object') {
89
readConfig(store, config)
910
}

src/levels.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const [LOG, DEBUG, INFO, WARN, ERROR, NONE] = [0, 0, 1, 2, 3, -1]
2+
3+
export { LOG, DEBUG, INFO, WARN, ERROR, NONE }

src/loggers.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,41 @@
1+
import getLevel from './getLevel'
2+
13
function loggers (store) {
2-
function out (defaultPrint) {
3-
return (val, print) => {
4-
if (!print) {
4+
function out (defaultPrint, defaultLogLevel) {
5+
return (val, print, logLevel) => {
6+
if (store.logLevel < 0) return
7+
8+
if (!print && getLevel(defaultLogLevel) >= store.logLevel) {
59
defaultPrint(`%c${val}`, store.style)
6-
} else if (print.isSinonProxy) {
7-
print(`%c${val}`, store.style)
8-
} else {
9-
print(val, parse(store.style))
10+
} else if (print && getLevel(logLevel) >= store.logLevel) {
11+
if (print.isSinonProxy) {
12+
print(`%c${val}`, store.style)
13+
} else {
14+
print(val, parseStyles(store.style), store.logLevel)
15+
}
1016
}
1117
}
1218
}
1319

14-
function parse (styleStr) {
20+
function parseStyles (styleStr) {
1521
const cssMap = {}
1622
const styles = styleStr.split(';')
23+
1724
styles.slice(0, styles.length - 1)
1825
.forEach(style => {
1926
const pair = style.split(':')
2027
cssMap[pair[0]] = pair[1]
2128
})
29+
2230
return cssMap
2331
}
2432

2533
return {
26-
log: out(console.log),
27-
warn: out(console.warn),
28-
error: out(console.error),
29-
info: out(console.info),
30-
debug: out(console.debug)
34+
log: out(console.log, 'log'),
35+
debug: out(console.debug, 'debug'),
36+
info: out(console.info, 'info'),
37+
warn: out(console.warn, 'warn'),
38+
error: out(console.error, 'error')
3139
}
3240
}
3341

src/readConfig.js

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,29 @@ function readConfig (store, config) {
88
const val = styleVal in nameMap ? nameMap[styleVal] : styleVal
99
switch (styleName) {
1010
case 'color':
11-
appendColor(store, val)
12-
break
11+
appendColor(store, val); break
1312
case 'weight':
14-
appendWeight(store, val)
15-
break
13+
appendWeight(store, val); break
1614
case 'bg':
17-
appendBg(store, val)
18-
break
15+
appendBg(store, val); break
1916
case 'size':
20-
appendSize(store, val)
21-
break
17+
appendSize(store, val); break
2218
case 'decorate':
23-
appendDecorate(store, val)
24-
break
19+
appendDecorate(store, val); break
2520
case 'family':
26-
appendFamily(store, val)
27-
break
21+
appendFamily(store, val); break
2822
case 'style':
29-
appendStyle(store, val)
30-
break
23+
appendStyle(store, val); break
3124
case 'transform':
32-
appendTransform(store, val)
33-
break
25+
appendTransform(store, val); break
3426
case 'shadow':
35-
appendShadow(store, val)
36-
break
27+
appendShadow(store, val); break
3728
case 'padding':
38-
appendPadding(store, val)
39-
break
29+
appendPadding(store, val); break
4030
case 'margin':
41-
appendMargin(store, val)
42-
break
31+
appendMargin(store, val); break
4332
case 'css':
44-
appendCss(store, val)
45-
break
33+
appendCss(store, val); break
4634
}
4735
})
4836
}

test/levels.js

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import test from 'ava'
2+
import { spy } from 'sinon'
3+
import pwc from './../dist/pretty-web-console'
4+
5+
test('pwc.level = "debug"|"log"|0', t => {
6+
t.plan(5)
7+
8+
let logLevel = 'log'
9+
pwc().log(logLevel, logSpy, logLevel)
10+
t.true(logSpy.calledWith(`%c${logLevel}`))
11+
12+
logLevel = 'debug'
13+
pwc().debug(logLevel, logSpy, logLevel)
14+
t.true(logSpy.calledWith(`%c${logLevel}`))
15+
16+
logLevel = 'info'
17+
pwc().info(logLevel, logSpy, logLevel)
18+
t.true(logSpy.calledWith(`%c${logLevel}`))
19+
20+
logLevel = 'warn'
21+
pwc().warn(logLevel, logSpy, logLevel)
22+
t.true(logSpy.calledWith(`%c${logLevel}`))
23+
24+
logLevel = 'error'
25+
pwc().error(logLevel, logSpy, logLevel)
26+
t.true(logSpy.calledWith(`%c${logLevel}`))
27+
})
28+
29+
test('pwc.level = "info"|1', t => {
30+
t.plan(5)
31+
pwc.level = 'info'
32+
33+
let logLevel = 'log'
34+
pwc().log(logLevel, logSpy, logLevel)
35+
t.false(logSpy.calledWith(`%c${logLevel}`))
36+
37+
logLevel = 'debug'
38+
pwc().debug(logLevel, logSpy, logLevel)
39+
t.false(logSpy.calledWith(`%c${logLevel}`))
40+
41+
logLevel = 'info'
42+
pwc().info(logLevel, logSpy, logLevel)
43+
t.true(logSpy.calledWith(`%c${logLevel}`))
44+
45+
logLevel = 'warn'
46+
pwc().warn(logLevel, logSpy, logLevel)
47+
t.true(logSpy.calledWith(`%c${logLevel}`))
48+
49+
logLevel = 'error'
50+
pwc().error(logLevel, logSpy, logLevel)
51+
t.true(logSpy.calledWith(`%c${logLevel}`))
52+
})
53+
54+
test('pwc.level = "warn"|2', t => {
55+
t.plan(5)
56+
pwc.level = 'warn'
57+
58+
let logLevel = 'log'
59+
pwc().log(logLevel, logSpy, logLevel)
60+
t.false(logSpy.calledWith(`%c${logLevel}`))
61+
62+
logLevel = 'debug'
63+
pwc().debug(logLevel, logSpy, logLevel)
64+
t.false(logSpy.calledWith(`%c${logLevel}`))
65+
66+
logLevel = 'info'
67+
pwc().info(logLevel, logSpy, logLevel)
68+
t.false(logSpy.calledWith(`%c${logLevel}`))
69+
70+
logLevel = 'warn'
71+
pwc().warn(logLevel, logSpy, logLevel)
72+
t.true(logSpy.calledWith(`%c${logLevel}`))
73+
74+
logLevel = 'error'
75+
pwc().error(logLevel, logSpy, logLevel)
76+
t.true(logSpy.calledWith(`%c${logLevel}`))
77+
})
78+
79+
test('pwc.level = "error"|3', t => {
80+
t.plan(5)
81+
pwc.level = 'error'
82+
83+
let logLevel = 'log'
84+
pwc().log(logLevel, logSpy, logLevel)
85+
t.false(logSpy.calledWith(`%c${logLevel}`))
86+
87+
logLevel = 'debug'
88+
pwc().debug(logLevel, logSpy, logLevel)
89+
t.false(logSpy.calledWith(`%c${logLevel}`))
90+
91+
logLevel = 'info'
92+
pwc().info(logLevel, logSpy, logLevel)
93+
t.false(logSpy.calledWith(`%c${logLevel}`))
94+
95+
logLevel = 'warn'
96+
pwc().warn(logLevel, logSpy, logLevel)
97+
t.false(logSpy.calledWith(`%c${logLevel}`))
98+
99+
logLevel = 'error'
100+
pwc().error(logLevel, logSpy, logLevel)
101+
t.true(logSpy.calledWith(`%c${logLevel}`))
102+
})
103+
104+
test('pwc.level = "none"|-1', t => {
105+
t.plan(5)
106+
pwc.level = 'none'
107+
108+
let logLevel = 'log'
109+
pwc().log(logLevel, logSpy, logLevel)
110+
t.false(logSpy.calledWith(`%c${logLevel}`))
111+
112+
logLevel = 'debug'
113+
pwc().debug(logLevel, logSpy, logLevel)
114+
t.false(logSpy.calledWith(`%c${logLevel}`))
115+
116+
logLevel = 'info'
117+
pwc().info(logLevel, logSpy, logLevel)
118+
t.false(logSpy.calledWith(`%c${logLevel}`))
119+
120+
logLevel = 'warn'
121+
pwc().warn(logLevel, logSpy, logLevel)
122+
t.false(logSpy.calledWith(`%c${logLevel}`))
123+
124+
logLevel = 'error'
125+
pwc().error(logLevel, logSpy, logLevel)
126+
t.false(logSpy.calledWith(`%c${logLevel}`))
127+
})
128+
129+
let logSpy
130+
test.beforeEach('setup', () => {
131+
logSpy = spy()
132+
pwc.level = 0
133+
})

0 commit comments

Comments
 (0)