Skip to content

Commit 83c2c9f

Browse files
committed
Add filtering of the mutations logging
Fixes #710
1 parent a7bf0ec commit 83c2c9f

File tree

8 files changed

+57
-22
lines changed

8 files changed

+57
-22
lines changed

dist/logger.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Payload, Plugin } from "../types/index";
77

88
export interface LoggerOption<S> {
99
collapsed?: boolean;
10+
filter?: <P extends Payload>(mutation: P, stateBefore: S, stateAfter: S) => boolean;
1011
transformer?: (state: S) => any;
1112
mutationTransformer?: <P extends Payload>(mutation: P) => any;
1213
}

docs/en/plugins.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ The `createLogger` function takes a few options:
102102
``` js
103103
const logger = createLogger({
104104
collapsed: false, // auto-expand logged mutations
105+
filter(mutation, stateBefore, stateAfter) {
106+
// returns true if a mutation should be logged
107+
// `mutation` is a { type, payload }
108+
return mutation.type !== "aBlacklistedMutation"
109+
},
105110
transformer (state) {
106111
// transform the state before logging it.
107112
// for example return only a specific sub-tree

docs/fr/plugins.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ La fonction `createLogger` prend quelques options :
102102
``` js
103103
const logger = createLogger({
104104
collapsed: false, // auto-expand logged mutations
105+
filter(mutation, stateBefore, stateAfter) {
106+
// returns true if a mutation should be logged
107+
// `mutation` is a { type, payload }
108+
return mutation.type !== "aBlacklistedMutation"
109+
},
105110
transformer (state) {
106111
// transform the state before logging it.
107112
// for example return only a specific sub-tree

docs/ja/plugins.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ const store = new Vuex.Store({
102102
``` js
103103
const logger = createLogger({
104104
collapsed: false, // ログ出力されたミューテーションを自動で展開します
105+
filter(mutation, stateBefore, stateAfter) {
106+
// returns true if a mutation should be logged
107+
// `mutation` is a { type, payload }
108+
return mutation.type !== "aBlacklistedMutation"
109+
},
105110
transformer (state) {
106111
// ロギングの前に、状態を変換します
107112
// 例えば、特定のサブツリーのみを返します

docs/kr/plugins.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ const store = new Vuex.Store({
103103
``` js
104104
const logger = createLogger({
105105
collapsed: false, // 로그를 가지는 변이 자동 확장
106+
filter(mutation, stateBefore, stateAfter) {
107+
// returns true if a mutation should be logged
108+
// `mutation` is a { type, payload }
109+
return mutation.type !== "aBlacklistedMutation"
110+
},
106111
transformer (state) {
107112
// 로깅하기전 상태를 변이 하십시오.
108113
// 예를 들어 특정 하위 트리만 반환합니다.

docs/ru/plugins.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ const store = new Vuex.Store({
102102
``` js
103103
const logger = createLogger({
104104
collapsed: false, // автоматически раскрывать залогированные мутации
105+
filter(mutation, stateBefore, stateAfter) {
106+
// returns true if a mutation should be logged
107+
// `mutation` is a { type, payload }
108+
return mutation.type !== "aBlacklistedMutation"
109+
},
105110
transformer (state) {
106111
// обработать состояние перед логированием
107112
// например, позволяет рассматривать только конкретное поддерево

docs/zh-cn/plugins.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ const store = new Vuex.Store({
102102
``` js
103103
const logger = createLogger({
104104
collapsed: false, // 自动展开记录的 mutation
105+
filter(mutation, stateBefore, stateAfter) {
106+
// returns true if a mutation should be logged
107+
// `mutation` is a { type, payload }
108+
return mutation.type !== "aBlacklistedMutation"
109+
},
105110
transformer (state) {
106111
// 在开始记录之前转换状态
107112
// 例如,只返回指定的子树

src/plugins/logger.js

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { deepCopy } from '../util'
44

55
export default function createLogger ({
66
collapsed = true,
7+
filter = (mutation, stateBefore, stateAfter) => true,
78
transformer = state => state,
89
mutationTransformer = mut => mut
910
} = {}) {
@@ -15,29 +16,32 @@ export default function createLogger ({
1516
return
1617
}
1718
const nextState = deepCopy(state)
18-
const time = new Date()
19-
const formattedTime = ` @ ${pad(time.getHours(), 2)}:${pad(time.getMinutes(), 2)}:${pad(time.getSeconds(), 2)}.${pad(time.getMilliseconds(), 3)}`
20-
const formattedMutation = mutationTransformer(mutation)
21-
const message = `mutation ${mutation.type}${formattedTime}`
22-
const startMessage = collapsed
23-
? console.groupCollapsed
24-
: console.group
25-
26-
// render
27-
try {
28-
startMessage.call(console, message)
29-
} catch (e) {
30-
console.log(message)
31-
}
32-
33-
console.log('%c prev state', 'color: #9E9E9E; font-weight: bold', transformer(prevState))
34-
console.log('%c mutation', 'color: #03A9F4; font-weight: bold', formattedMutation)
35-
console.log('%c next state', 'color: #4CAF50; font-weight: bold', transformer(nextState))
3619

37-
try {
38-
console.groupEnd()
39-
} catch (e) {
40-
console.log('—— log end ——')
20+
if (filter(mutation, prevState, nextState)) {
21+
const time = new Date()
22+
const formattedTime = ` @ ${pad(time.getHours(), 2)}:${pad(time.getMinutes(), 2)}:${pad(time.getSeconds(), 2)}.${pad(time.getMilliseconds(), 3)}`
23+
const formattedMutation = mutationTransformer(mutation)
24+
const message = `mutation ${mutation.type}${formattedTime}`
25+
const startMessage = collapsed
26+
? console.groupCollapsed
27+
: console.group
28+
29+
// render
30+
try {
31+
startMessage.call(console, message)
32+
} catch (e) {
33+
console.log(message)
34+
}
35+
36+
console.log('%c prev state', 'color: #9E9E9E; font-weight: bold', transformer(prevState))
37+
console.log('%c mutation', 'color: #03A9F4; font-weight: bold', formattedMutation)
38+
console.log('%c next state', 'color: #4CAF50; font-weight: bold', transformer(nextState))
39+
40+
try {
41+
console.groupEnd()
42+
} catch (e) {
43+
console.log('—— log end ——')
44+
}
4145
}
4246

4347
prevState = nextState

0 commit comments

Comments
 (0)