redux-logger 使用

安装

npm install redux-logger --save-dev 

redux-logger 仅且在开发模式下使用,所以只需增加参数--save-dev.

  • --save-dev 开发模式下使用的插件
  • --save 生成环境下使用的插件
  • -g 将插件安装成全局插件,该电脑的所有项目都可以使用。无-g参数时,仅且该项目可以使用改插件。

使用

  • API定义:createLogger(options?:Object)=>LoggerMiddleware;
  • JS新语法说明:
  1. options?:Object 是类型定义,表明options参数的类型是Object类型。
  2. “?”号表示非必须参数。
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写博客。写的很粗糙,期望大神帮忙指出问题。

转载于:https://my.oschina.net/u/2550428/blog/716707

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值