安装
npm install redux-logger --save-dev
redux-logger 仅且在开发模式下使用,所以只需增加参数--save-dev.
- --save-dev 开发模式下使用的插件
- --save 生成环境下使用的插件
- -g 将插件安装成全局插件,该电脑的所有项目都可以使用。无-g参数时,仅且该项目可以使用改插件。
使用
- API定义:createLogger(options?:Object)=>LoggerMiddleware;
- JS新语法说明:
- options?:Object 是类型定义,表明options参数的类型是Object类型。
- “?”号表示非必须参数。
options包含的属性:
{
//日志等级,可以是以下字符串,也可以是返回以下字符串的function.
level = 'log': 'log' | 'console' | 'warn' | 'error' | 'info', // console's level
//执行持续时间
duration = false: Boolean, // Print the duration of each action?
//执行时间
timestamp = true: Boolean, // Print the timestamp with each action?
//日志颜色
colors: ColorsObject, // Object with color getters. See the ColorsObject interface.
//自定义一个日志输出函数,相当于再次封装console类
logger = console: LoggerObject, // Implementation of the `console` API.
logErrors = true: Boolean, // Should the logger catch, log, and re-throw errors?
collapsed, // Takes a boolean or optionally a function that receives `getState` function for accessing current store state and `action` object as parameters. Returns `true` if the log group should be collapsed, `false` otherwise.
predicate, // If specified this function will be called before each action is processed with this middleware.
stateTransformer, // Transform state before print. Eg. convert Immutable object to plain JSON.
actionTransformer, // Transform state before print. Eg. convert Immutable object to plain JSON.
errorTransformer // Transform state before print. Eg. convert Immutable object to plain JSON.
}
注意事项
logger 中间件必须放在所有中间件的最后,不然会出现部分action无法打印日志的情况。 使用如下:
var logger = createLogger({ predicate: (getState, action) => isDebuggingInChrome, collapsed: true, duration: true, });
var createF8Store = applyMiddleware(thunk, promise, array, analytics, logger)(createStore);
brett:首次用markdown写博客。写的很粗糙,期望大神帮忙指出问题。