@@ -5,12 +5,33 @@ const formatTime = (time) => `@ ${pad(time.getHours(), 2)}:${pad(time.getMinutes
5
5
// Use the new performance api to get better precision if available
6
6
const timer = typeof performance !== `undefined` && typeof performance . now === `function` ? performance : Date ;
7
7
8
+
9
+ /**
10
+ * parse the level option of createLogger
11
+ *
12
+ * @property {string | function | object } level - console[level]
13
+ * @property {object } action
14
+ * @property {array } payload
15
+ * @property {string } type
16
+ */
17
+
18
+ function getLogLevel ( level , action , payload , type ) {
19
+ switch ( typeof level ) {
20
+ case `object` :
21
+ return typeof level [ type ] === `function` ? level [ type ] ( ...payload ) : level [ type ] ;
22
+ case `function` :
23
+ return level ( action ) ;
24
+ default :
25
+ return level ;
26
+ }
27
+ }
28
+
8
29
/**
9
30
* Creates logger with followed options
10
31
*
11
32
* @namespace
12
33
* @property {object } options - options for logger
13
- * @property {string } options.level - console[level]
34
+ * @property {string | function | object } options.level - console[level]
14
35
* @property {boolean } options.duration - print duration of each action?
15
36
* @property {boolean } options.timestamp - print timestamp with each action?
16
37
* @property {object } options.colors - custom colors
@@ -85,19 +106,30 @@ function createLogger(options = {}) {
85
106
logger . log ( title ) ;
86
107
}
87
108
88
- if ( colors . prevState ) logger [ level ] ( `%c prev state` , `color: ${ colors . prevState ( prevState ) } ; font-weight: bold` , prevState ) ;
89
- else logger [ level ] ( `prev state` , prevState ) ;
109
+ const prevStateLevel = getLogLevel ( level , formattedAction , [ prevState ] , `prevState` ) ;
110
+ const actionLevel = getLogLevel ( level , formattedAction , [ formattedAction ] , `action` ) ;
111
+ const errorLevel = getLogLevel ( level , formattedAction , [ error , prevState ] , `error` ) ;
112
+ const nextStateLevel = getLogLevel ( level , formattedAction , [ nextState ] , `nextState` ) ;
113
+
114
+ if ( prevStateLevel ) {
115
+ if ( colors . prevState ) logger [ level ] ( `%c prev state` , `color: ${ colors . prevState ( prevState ) } ; font-weight: bold` , prevState ) ;
116
+ else logger [ level ] ( `prev state` , prevState ) ;
117
+ }
90
118
91
- if ( colors . action ) logger [ level ] ( `%c action` , `color: ${ colors . action ( formattedAction ) } ; font-weight: bold` , formattedAction ) ;
92
- else logger [ level ] ( `action` , formattedAction ) ;
119
+ if ( actionLevel ) {
120
+ if ( colors . action ) logger [ level ] ( `%c action` , `color: ${ colors . action ( formattedAction ) } ; font-weight: bold` , formattedAction ) ;
121
+ else logger [ level ] ( `action` , formattedAction ) ;
122
+ }
93
123
94
- if ( error ) {
124
+ if ( error && errorLevel ) {
95
125
if ( colors . error ) logger [ level ] ( `%c error` , `color: ${ colors . error ( error , prevState ) } ; font-weight: bold` , error ) ;
96
126
else logger [ level ] ( `error` , error ) ;
97
127
}
98
128
99
- if ( colors . nextState ) logger [ level ] ( `%c next state` , `color: ${ colors . nextState ( nextState ) } ; font-weight: bold` , nextState ) ;
100
- else logger [ level ] ( `next state` , nextState ) ;
129
+ if ( nextStateLevel ) {
130
+ if ( colors . nextState ) logger [ level ] ( `%c next state` , `color: ${ colors . nextState ( nextState ) } ; font-weight: bold` , nextState ) ;
131
+ else logger [ level ] ( `next state` , nextState ) ;
132
+ }
101
133
102
134
try {
103
135
logger . groupEnd ( ) ;
0 commit comments