Skip to content

Commit 3881bbd

Browse files
committed
feat: createLogger can optionally log actions
1 parent 7da1f13 commit 3881bbd

File tree

4 files changed

+83
-30
lines changed

4 files changed

+83
-30
lines changed

dist/logger.d.ts

Lines changed: 4 additions & 0 deletions
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

Lines changed: 11 additions & 0 deletions
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: false, // Log Actions
132+
logMutations: true, // Log mutations
122133
logger: console, // implementation of the `console` API, default `console`
123134
})
124135
```

src/plugins/devtool.js

Lines changed: 4 additions & 0 deletions
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

Lines changed: 64 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,83 @@ 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 = false,
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+
}
42+
43+
if (logActions) {
44+
store.subscribeAction((action, state) => {
45+
const currentState = deepCopy(state)
4046

41-
try {
42-
logger.groupEnd()
43-
} catch (e) {
44-
logger.log('—— log end ——')
47+
if (actionFilter(action, currentState)) {
48+
const formattedTime = getFormattedTime()
49+
const formattedAction = actionTransformer(action)
50+
const message = `action ${action.type}${formattedTime}`
51+
52+
startMessage(logger, message, collapsed)
53+
logger.log('%c action', 'color: #03A9F4; font-weight: bold', formattedAction)
54+
endMessage(logger)
4555
}
46-
}
56+
})
57+
}
58+
}
59+
}
4760

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

82+
function getFormattedTime () {
83+
const time = new Date()
84+
return ` @ ${pad(time.getHours(), 2)}:${pad(time.getMinutes(), 2)}:${pad(time.getSeconds(), 2)}.${pad(time.getMilliseconds(), 3)}`
85+
}
86+
5387
function repeat (str, times) {
5488
return (new Array(times + 1)).join(str)
5589
}

0 commit comments

Comments
 (0)