Skip to content

Commit 066de7a

Browse files
committed
feat: createLogger can optionally log actions
1 parent fa1362d commit 066de7a

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/en/plugins.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ const logger = createLogger({
107107
// `mutation` is a `{ type, payload }`
108108
return mutation.type !== "aBlacklistedMutation"
109109
},
110+
actionFilter (action, state) {
111+
// same as `filter` but for actions
112+
// `action` is a `{ type, payload }`
113+
return action.type !== "aBlacklistedAction"
114+
},
110115
transformer (state) {
111116
// transform the state before logging it.
112117
// for example return only a specific sub-tree
@@ -117,6 +122,12 @@ const logger = createLogger({
117122
// we can format it any way we want.
118123
return mutation.type
119124
},
125+
actionTransformer (action) {
126+
// Same as mutationTransformer but for actions
127+
return action.type
128+
},
129+
logActions: false, // Log Actions
130+
logMutations: true, // Log mutations
120131
logger: console, // implementation of the `console` API, default `console`
121132
})
122133
```

src/plugins/devtool.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,8 @@ export default function devtoolPlugin (store) {
1616
store.subscribe((mutation, state) => {
1717
devtoolHook.emit('vuex:mutation', mutation, state)
1818
})
19+
20+
store.subscribeAction((action, state) => {
21+
devtoolHook.emit('vuex:action', action, state)
22+
})
1923
}

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)