Skip to content

Add filtering of the mutations logging #711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dist/logger.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Payload, Plugin } from "../types/index";

export interface LoggerOption<S> {
collapsed?: boolean;
filter?: <P extends Payload>(mutation: P, stateBefore: S, stateAfter: S) => boolean;
transformer?: (state: S) => any;
mutationTransformer?: <P extends Payload>(mutation: P) => any;
}
Expand Down
5 changes: 5 additions & 0 deletions docs/en/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ The `createLogger` function takes a few options:
``` js
const logger = createLogger({
collapsed: false, // auto-expand logged mutations
filter (mutation, stateBefore, stateAfter) {
// returns true if a mutation should be logged
// `mutation` is a { type, payload }
return mutation.type !== "aBlacklistedMutation"
},
transformer (state) {
// transform the state before logging it.
// for example return only a specific sub-tree
Expand Down
5 changes: 5 additions & 0 deletions docs/fr/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ La fonction `createLogger` prend quelques options :
``` js
const logger = createLogger({
collapsed: false, // auto-expand logged mutations
filter (mutation, stateBefore, stateAfter) {
// returns true if a mutation should be logged
// `mutation` is a { type, payload }
return mutation.type !== "aBlacklistedMutation"
},
transformer (state) {
// transform the state before logging it.
// for example return only a specific sub-tree
Expand Down
5 changes: 5 additions & 0 deletions docs/ja/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ const store = new Vuex.Store({
``` js
const logger = createLogger({
collapsed: false, // ログ出力されたミューテーションを自動で展開します
filter (mutation, stateBefore, stateAfter) {
// returns true if a mutation should be logged
// `mutation` is a { type, payload }
return mutation.type !== "aBlacklistedMutation"
},
transformer (state) {
// ロギングの前に、状態を変換します
// 例えば、特定のサブツリーのみを返します
Expand Down
5 changes: 5 additions & 0 deletions docs/kr/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ const store = new Vuex.Store({
``` js
const logger = createLogger({
collapsed: false, // 로그를 가지는 변이 자동 확장
filter (mutation, stateBefore, stateAfter) {
// returns true if a mutation should be logged
// `mutation` is a { type, payload }
return mutation.type !== "aBlacklistedMutation"
},
transformer (state) {
// 로깅하기전 상태를 변이 하십시오.
// 예를 들어 특정 하위 트리만 반환합니다.
Expand Down
5 changes: 5 additions & 0 deletions docs/ru/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ const store = new Vuex.Store({
``` js
const logger = createLogger({
collapsed: false, // автоматически раскрывать залогированные мутации
filter (mutation, stateBefore, stateAfter) {
// returns true if a mutation should be logged
// `mutation` is a { type, payload }
return mutation.type !== "aBlacklistedMutation"
},
transformer (state) {
// обработать состояние перед логированием
// например, позволяет рассматривать только конкретное поддерево
Expand Down
5 changes: 5 additions & 0 deletions docs/zh-cn/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ const store = new Vuex.Store({
``` js
const logger = createLogger({
collapsed: false, // 自动展开记录的 mutation
filter (mutation, stateBefore, stateAfter) {
// returns true if a mutation should be logged
// `mutation` is a { type, payload }
return mutation.type !== "aBlacklistedMutation"
},
transformer (state) {
// 在开始记录之前转换状态
// 例如,只返回指定的子树
Expand Down
48 changes: 26 additions & 22 deletions src/plugins/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { deepCopy } from '../util'

export default function createLogger ({
collapsed = true,
filter = (mutation, stateBefore, stateAfter) => true,
transformer = state => state,
mutationTransformer = mut => mut
} = {}) {
Expand All @@ -15,29 +16,32 @@ export default function createLogger ({
return
}
const nextState = deepCopy(state)
const time = new Date()
const formattedTime = ` @ ${pad(time.getHours(), 2)}:${pad(time.getMinutes(), 2)}:${pad(time.getSeconds(), 2)}.${pad(time.getMilliseconds(), 3)}`
const formattedMutation = mutationTransformer(mutation)
const message = `mutation ${mutation.type}${formattedTime}`
const startMessage = collapsed
? console.groupCollapsed
: console.group

// render
try {
startMessage.call(console, message)
} catch (e) {
console.log(message)
}

console.log('%c prev state', 'color: #9E9E9E; font-weight: bold', transformer(prevState))
console.log('%c mutation', 'color: #03A9F4; font-weight: bold', formattedMutation)
console.log('%c next state', 'color: #4CAF50; font-weight: bold', transformer(nextState))

try {
console.groupEnd()
} catch (e) {
console.log('—— log end ——')
if (filter(mutation, prevState, nextState)) {
const time = new Date()
const formattedTime = ` @ ${pad(time.getHours(), 2)}:${pad(time.getMinutes(), 2)}:${pad(time.getSeconds(), 2)}.${pad(time.getMilliseconds(), 3)}`
const formattedMutation = mutationTransformer(mutation)
const message = `mutation ${mutation.type}${formattedTime}`
const startMessage = collapsed
? console.groupCollapsed
: console.group

// render
try {
startMessage.call(console, message)
} catch (e) {
console.log(message)
}

console.log('%c prev state', 'color: #9E9E9E; font-weight: bold', transformer(prevState))
console.log('%c mutation', 'color: #03A9F4; font-weight: bold', formattedMutation)
console.log('%c next state', 'color: #4CAF50; font-weight: bold', transformer(nextState))

try {
console.groupEnd()
} catch (e) {
console.log('—— log end ——')
}
}

prevState = nextState
Expand Down