forked from LogRocket/redux-logger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.js
146 lines (121 loc) · 4.34 KB
/
core.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { formatTime } from './helpers';
import diffLogger from './diff';
/**
* Get log level string based on supplied params
*
* @param {string | function | object} level - console[level]
* @param {object} action - selected action
* @param {array} payload - selected payload
* @param {string} type - log entry type
*
* @returns {string} level
*/
function getLogLevel(level, action, payload, type) {
switch (typeof level) {
case 'object':
return typeof level[type] === 'function' ? level[type](...payload) : level[type];
case 'function':
return level(action);
default:
return level;
}
}
function defaultTitleFormatter(options) {
const { timestamp, duration } = options;
return (action, time, took) => {
const parts = ['action'];
parts.push(`%c${String(action.type)}`);
if (timestamp) parts.push(`%c@ ${time}`);
if (duration) parts.push(`%c(in ${took.toFixed(2)} ms)`);
return parts.join(' ');
};
}
function printBuffer(buffer, options) {
const {
logger,
actionTransformer,
titleFormatter = defaultTitleFormatter(options),
collapsed,
colors,
level,
diff,
} = options;
const isUsingDefaultFormatter = typeof options.titleFormatter === 'undefined';
buffer.forEach((logEntry, key) => {
const { started, startedTime, action, prevState, error } = logEntry;
let { took, nextState } = logEntry;
const nextEntry = buffer[key + 1];
if (nextEntry) {
nextState = nextEntry.prevState;
took = nextEntry.started - started;
}
// Message
const formattedAction = actionTransformer(action);
const isCollapsed = typeof collapsed === 'function'
? collapsed(() => nextState, action, logEntry)
: collapsed;
const formattedTime = formatTime(startedTime);
const titleCSS = colors.title ? `color: ${colors.title(formattedAction)};` : '';
const headerCSS = ['color: gray; font-weight: lighter;'];
headerCSS.push(titleCSS);
if (options.timestamp) headerCSS.push('color: gray; font-weight: lighter;');
if (options.duration) headerCSS.push('color: gray; font-weight: lighter;');
const title = titleFormatter(formattedAction, formattedTime, took);
// Render
try {
if (isCollapsed) {
if (colors.title && isUsingDefaultFormatter) {
logger.groupCollapsed(`%c ${title}`, ...headerCSS);
} else logger.groupCollapsed(title);
} else if (colors.title && isUsingDefaultFormatter) {
logger.group(`%c ${title}`, ...headerCSS);
} else {
logger.group(title);
}
} catch (e) {
logger.log(title);
}
const prevStateLevel = getLogLevel(level, formattedAction, [prevState], 'prevState');
const actionLevel = getLogLevel(level, formattedAction, [formattedAction], 'action');
const errorLevel = getLogLevel(level, formattedAction, [error, prevState], 'error');
const nextStateLevel = getLogLevel(level, formattedAction, [nextState], 'nextState');
if (prevStateLevel) {
if (colors.prevState) {
const styles = `color: ${colors.prevState(prevState)}; font-weight: bold`;
logger[prevStateLevel]('%c prev state', styles, prevState);
} else logger[prevStateLevel]('prev state', prevState);
}
if (actionLevel) {
if (colors.action) {
const styles = `color: ${colors.action(formattedAction)}; font-weight: bold`;
logger[actionLevel]('%c action ', styles, formattedAction);
} else logger[actionLevel]('action ', formattedAction);
}
if (error && errorLevel) {
if (colors.error) {
const styles = `color: ${colors.error(error, prevState)}; font-weight: bold;`;
logger[errorLevel]('%c error ', styles, error);
} else logger[errorLevel]('error ', error);
}
if (nextStateLevel) {
if (colors.nextState) {
const styles = `color: ${colors.nextState(nextState)}; font-weight: bold`;
logger[nextStateLevel]('%c next state', styles, nextState);
} else logger[nextStateLevel]('next state', nextState);
}
if (logger.withTrace) {
logger.groupCollapsed(`TRACE`);
logger.trace();
logger.groupEnd();
}
if (diff) {
diffLogger(prevState, nextState, logger, isCollapsed);
}
try {
logger.groupEnd();
} catch (e) {
logger.log('—— log end ——');
}
});
}
export default printBuffer;