Skip to content

Commit

Permalink
feat: add logger plugin logger config support (#771)
Browse files Browse the repository at this point in the history
* feat: add logger plugin logger config

* fix: make eslint happy
  • Loading branch information
lonelyclick authored and yyx990803 committed Oct 11, 2017
1 parent 92ef5ea commit 804c3bb
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
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({
// les mutations sont logguées au format `{ type, payload }`
// nous pouvons les formater comme nous le souhaitons.
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

0 comments on commit 804c3bb

Please sign in to comment.