Skip to content

Commit 18be128

Browse files
authored
feat(logger): createLogger can optionally log actions (#987)
1 parent 52b753b commit 18be128

File tree

4 files changed

+81
-30
lines changed

4 files changed

+81
-30
lines changed

dist/logger.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ export interface LoggerOption<S> {
1010
filter?: <P extends Payload>(mutation: P, stateBefore: S, stateAfter: S) => boolean;
1111
transformer?: (state: S) => any;
1212
mutationTransformer?: <P extends Payload>(mutation: P) => any;
13+
actionFilter?: <P extends Payload>(action: P, state: S) => boolean;
14+
actionTransformer?: <P extends Payload>(action: P) => any;
15+
logActions?: boolean;
16+
logMutations?: boolean;
1317
}
1418

1519
export default function createLogger<S>(option?: LoggerOption<S>): Plugin<S>;

docs/guide/plugins.md

+11
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ const logger = createLogger({
109109
// `mutation` is a `{ type, payload }`
110110
return mutation.type !== "aBlacklistedMutation"
111111
},
112+
actionFilter (action, state) {
113+
// same as `filter` but for actions
114+
// `action` is a `{ type, payload }`
115+
return action.type !== "aBlacklistedAction"
116+
},
112117
transformer (state) {
113118
// transform the state before logging it.
114119
// for example return only a specific sub-tree
@@ -119,6 +124,12 @@ const logger = createLogger({
119124
// we can format it any way we want.
120125
return mutation.type
121126
},
127+
actionTransformer (action) {
128+
// Same as mutationTransformer but for actions
129+
return action.type
130+
},
131+
logActions: true, // Log Actions
132+
logMutations: true, // Log mutations
122133
logger: console, // implementation of the `console` API, default `console`
123134
})
124135
```

src/plugins/devtool.js

+4
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,8 @@ export default function devtoolPlugin (store) {
1919
store.subscribe((mutation, state) => {
2020
devtoolHook.emit('vuex:mutation', mutation, state)
2121
})
22+
23+
store.subscribeAction((action, state) => {
24+
devtoolHook.emit('vuex:action', action, state)
25+
})
2226
}

src/plugins/logger.js

+62-30
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,81 @@ export default function createLogger ({
77
filter = (mutation, stateBefore, stateAfter) => true,
88
transformer = state => state,
99
mutationTransformer = mut => mut,
10+
actionFilter = (action, state) => true,
11+
actionTransformer = act => act,
12+
logActions = true,
13+
logMutations = true,
1014
logger = console
1115
} = {}) {
1216
return store => {
1317
let prevState = deepCopy(store.state)
1418

15-
store.subscribe((mutation, state) => {
16-
if (typeof logger === 'undefined') {
17-
return
18-
}
19-
const nextState = deepCopy(state)
20-
21-
if (filter(mutation, prevState, nextState)) {
22-
const time = new Date()
23-
const formattedTime = ` @ ${pad(time.getHours(), 2)}:${pad(time.getMinutes(), 2)}:${pad(time.getSeconds(), 2)}.${pad(time.getMilliseconds(), 3)}`
24-
const formattedMutation = mutationTransformer(mutation)
25-
const message = `mutation ${mutation.type}${formattedTime}`
26-
const startMessage = collapsed
27-
? logger.groupCollapsed
28-
: logger.group
29-
30-
// render
31-
try {
32-
startMessage.call(logger, message)
33-
} catch (e) {
34-
console.log(message)
19+
if (typeof logger === 'undefined') {
20+
return
21+
}
22+
23+
if (logMutations) {
24+
store.subscribe((mutation, state) => {
25+
const nextState = deepCopy(state)
26+
27+
if (filter(mutation, prevState, nextState)) {
28+
const formattedTime = getFormattedTime()
29+
const formattedMutation = mutationTransformer(mutation)
30+
const message = `mutation ${mutation.type}${formattedTime}`
31+
32+
startMessage(logger, message, collapsed)
33+
logger.log('%c prev state', 'color: #9E9E9E; font-weight: bold', transformer(prevState))
34+
logger.log('%c mutation', 'color: #03A9F4; font-weight: bold', formattedMutation)
35+
logger.log('%c next state', 'color: #4CAF50; font-weight: bold', transformer(nextState))
36+
endMessage(logger)
3537
}
3638

37-
logger.log('%c prev state', 'color: #9E9E9E; font-weight: bold', transformer(prevState))
38-
logger.log('%c mutation', 'color: #03A9F4; font-weight: bold', formattedMutation)
39-
logger.log('%c next state', 'color: #4CAF50; font-weight: bold', transformer(nextState))
39+
prevState = nextState
40+
})
41+
}
4042

41-
try {
42-
logger.groupEnd()
43-
} catch (e) {
44-
logger.log('—— log end ——')
43+
if (logActions) {
44+
store.subscribeAction((action, state) => {
45+
if (actionFilter(action, state)) {
46+
const formattedTime = getFormattedTime()
47+
const formattedAction = actionTransformer(action)
48+
const message = `action ${action.type}${formattedTime}`
49+
50+
startMessage(logger, message, collapsed)
51+
logger.log('%c action', 'color: #03A9F4; font-weight: bold', formattedAction)
52+
endMessage(logger)
4553
}
46-
}
54+
})
55+
}
56+
}
57+
}
4758

48-
prevState = nextState
49-
})
59+
function startMessage (logger, message, collapsed) {
60+
const startMessage = collapsed
61+
? logger.groupCollapsed
62+
: logger.group
63+
64+
// render
65+
try {
66+
startMessage.call(logger, message)
67+
} catch (e) {
68+
logger.log(message)
69+
}
70+
}
71+
72+
function endMessage (logger) {
73+
try {
74+
logger.groupEnd()
75+
} catch (e) {
76+
logger.log('—— log end ——')
5077
}
5178
}
5279

80+
function getFormattedTime () {
81+
const time = new Date()
82+
return ` @ ${pad(time.getHours(), 2)}:${pad(time.getMinutes(), 2)}:${pad(time.getSeconds(), 2)}.${pad(time.getMilliseconds(), 3)}`
83+
}
84+
5385
function repeat (str, times) {
5486
return (new Array(times + 1)).join(str)
5587
}

0 commit comments

Comments
 (0)