MCPcopy
hub / github.com/vuejs/vuex / createLogger

Function createLogger

src/plugins/logger.js:5–57  ·  view source on GitHub ↗
({
  collapsed = true,
  filter = (mutation, stateBefore, stateAfter) => true,
  transformer = state => state,
  mutationTransformer = mut => mut,
  actionFilter = (action, state) => true,
  actionTransformer = act => act,
  logMutations = true,
  logActions = true,
  logger = console
} = {})

Source from the content-addressed store, hash-verified

3import { deepCopy } from '../util'
4
5export function createLogger ({
6 collapsed = true,
7 filter = (mutation, stateBefore, stateAfter) => true,
8 transformer = state => state,
9 mutationTransformer = mut => mut,
10 actionFilter = (action, state) => true,
11 actionTransformer = act => act,
12 logMutations = true,
13 logActions = true,
14 logger = console
15} = {}) {
16 return store => {
17 let prevState = deepCopy(store.state)
18
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)
37 }
38
39 prevState = nextState
40 })
41 }
42
43 if (logActions) {
44 store.subscribeAction((action, state) => {
45 if (actionFilter(action, state)) {
46 const formattedTime = getFormattedTime()
47 const formattedAction = actionTransformer(action)
48 const message = `action ${action.type}${formattedTime}`
49
50 startMessage(logger, message, collapsed)
51 logger.log('%c action', 'color: #03A9F4; font-weight: bold', formattedAction)
52 endMessage(logger)
53 }
54 })
55 }
56 }
57}
58
59function startMessage (logger, message, collapsed) {
60 const startMessage = collapsed

Callers 6

plugins.jsFile · 0.85
index.jsFile · 0.85
index.jsFile · 0.85
plugins.jsFile · 0.85
index.jsFile · 0.85
index.jsFile · 0.85

Calls 7

deepCopyFunction · 0.90
getFormattedTimeFunction · 0.85
startMessageFunction · 0.85
endMessageFunction · 0.85
subscribeMethod · 0.80
subscribeActionMethod · 0.80
logMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…