Skip to content
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

feat: add logger plugin logger config support #771

Merged
merged 2 commits into from
Oct 11, 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
3 changes: 2 additions & 1 deletion docs/en/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ const logger = createLogger({
// mutations are logged in the format of { type, payload }
// we can format it any way we want.
return mutation.type
}
},
logger: console, // implementation of the `console` API, default `console`
})
```

Expand Down
3 changes: 2 additions & 1 deletion docs/fr/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ const logger = createLogger({
// mutations are logged in the format of { type, payload }
// we can format it any way we want.
return mutation.type
}
},
logger: console, // implementation of the `console` API, default `console`
})
```

Expand Down
3 changes: 2 additions & 1 deletion docs/zh-cn/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ const logger = createLogger({
// mutation 按照 { type, payload } 格式记录
// 我们可以按任意方式格式化
return mutation.type
}
},
logger: console, // 自定义 console 实现,默认为 `console`
})
```

Expand Down
21 changes: 11 additions & 10 deletions src/plugins/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ export default function createLogger ({
collapsed = true,
filter = (mutation, stateBefore, stateAfter) => true,
transformer = state => state,
mutationTransformer = mut => mut
mutationTransformer = mut => mut,
logger = console
} = {}) {
return store => {
let prevState = deepCopy(store.state)

store.subscribe((mutation, state) => {
if (typeof console === 'undefined') {
if (typeof logger === 'undefined') {
return
}
const nextState = deepCopy(state)
Expand All @@ -23,24 +24,24 @@ export default function createLogger ({
const formattedMutation = mutationTransformer(mutation)
const message = `mutation ${mutation.type}${formattedTime}`
const startMessage = collapsed
? console.groupCollapsed
: console.group
? logger.groupCollapsed
: logger.group

// render
try {
startMessage.call(console, message)
startMessage.call(logger, 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))
logger.log('%c prev state', 'color: #9E9E9E; font-weight: bold', transformer(prevState))
logger.log('%c mutation', 'color: #03A9F4; font-weight: bold', formattedMutation)
logger.log('%c next state', 'color: #4CAF50; font-weight: bold', transformer(nextState))

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

Expand Down