redux-logger
Advanced tools
Comparing version 0.0.3 to 1.0.0
@@ -0,1 +1,5 @@ | ||
/** | ||
* Deprecated, will be removed 1.1.0 | ||
*/ | ||
'use strict'; | ||
@@ -11,2 +15,7 @@ | ||
return function (action) { | ||
// exit if console undefined | ||
if (typeof console === 'undefined') { | ||
return next(action); | ||
} | ||
var prevState = getState(); | ||
@@ -16,21 +25,18 @@ var returnValue = next(action); | ||
var time = new Date(); | ||
var message = 'action ' + action.type + ' @ ' + time.getHours() + ':' + time.getMinutes() + ':' + time.getSeconds(); | ||
if (typeof console !== 'undefined') { | ||
var message = 'action ' + action.type + ' @ ' + time.getHours() + ':' + time.getMinutes() + ':' + time.getSeconds(); | ||
try { | ||
console.group(message); | ||
} catch (e) { | ||
console.log(message); | ||
} | ||
try { | ||
console.group(message); | ||
} catch (e) { | ||
console.log('NOT GROUP'); | ||
} | ||
console.log('%c prev state', 'color: #9E9E9E; font-weight: bold', prevState); | ||
console.log('%c action', 'color: #03A9F4; font-weight: bold', action); | ||
console.log('%c next state', 'color: #4CAF50; font-weight: bold', nextState); | ||
console.log('%c prev state', 'color: #9E9E9E; font-weight: bold', prevState); | ||
console.log('%c action', 'color: #03A9F4; font-weight: bold', action); | ||
console.log('%c next state', 'color: #4CAF50; font-weight: bold', nextState); | ||
try { | ||
console.groupEnd('—— log end ——'); | ||
} catch (e) { | ||
console.log('—— log end ——'); | ||
} | ||
try { | ||
console.groupEnd(); | ||
} catch (e) { | ||
console.log('—— log end ——'); | ||
} | ||
@@ -37,0 +43,0 @@ |
import React from 'react'; | ||
import logger from 'redux-logger'; | ||
import createLogger from 'redux-logger'; | ||
@@ -8,3 +8,9 @@ import { createStore, combineReducers, applyMiddleware } from 'redux'; | ||
import reducers from './reducers'; | ||
import { AUTH_REMOVE_TOKEN } from './constants/auth.js'; | ||
const logger = createLogger({ | ||
predicate: (getState, action) => action.type !== AUTH_REMOVE_TOKEN, // log all actions except AUTH_REMOVE_TOKEN | ||
collapsed: true, | ||
level: 'warn', | ||
}); | ||
const createStoreWithMiddleware = applyMiddleware(logger)(createStore); | ||
@@ -11,0 +17,0 @@ const reducer = combineReducers(reducers); |
{ | ||
"name": "redux-logger", | ||
"version": "0.0.3", | ||
"version": "1.0.0", | ||
"description": "Logger for redux", | ||
"main": "build/logger.js", | ||
"main": "build/index.js", | ||
"scripts": { | ||
@@ -7,0 +7,0 @@ "test": "npm run lint", |
@@ -5,9 +5,10 @@ # Logger for redux | ||
### Install | ||
## Install | ||
`npm i --save redux-logger` | ||
### Usage | ||
``` | ||
import logger from 'redux-logger'; | ||
## Usage | ||
```javascript | ||
import createLogger from 'redux-logger'; | ||
const logger = createLogger(); | ||
const createStoreWithMiddleware = applyMiddleware(logger)(createStore); | ||
@@ -17,3 +18,38 @@ const store = createStoreWithMiddleware(reducer); | ||
## API | ||
`redux-logger` exposes single constructor function for creating logger middleware. | ||
__createLogger(options?: Object)__ | ||
### Options | ||
#### __level (String)__ | ||
Level of `console`. `warn`, `error`, `info` or [else](https://developer.mozilla.org/en/docs/Web/API/console). | ||
#### __collapsed (Boolean)__ | ||
Is group collapsed? | ||
#### __predicate (getState: Function, action: Object): boolean__ | ||
If specified this function will be called before each action is processed with this middleware. | ||
Receives `getState` function for accessing current store state and `action` object as parameters. Returns `true` if action should be logged, `false` otherwise. | ||
##### Examples: | ||
###### log only in dev mode | ||
```javascript | ||
const __DEV__ = true; | ||
createLogger({ | ||
predicate: (getState, action) => __DEV__ | ||
}); | ||
``` | ||
###### log everything except actions with type `AUTH_REMOVE_TOKEN` | ||
```javascript | ||
createLogger({ | ||
predicate: (getState, action) => action.type !== AUTH_REMOVE_TOKEN | ||
}); | ||
``` | ||
### License | ||
MIT |
@@ -0,3 +1,12 @@ | ||
/** | ||
* Deprecated, will be removed 1.1.0 | ||
*/ | ||
function logger({ getState }) { | ||
return (next) => (action) => { | ||
// exit if console undefined | ||
if (typeof console === 'undefined') { | ||
return next(action); | ||
} | ||
const prevState = getState(); | ||
@@ -7,21 +16,18 @@ const returnValue = next(action); | ||
const time = new Date(); | ||
const message = `action ${action.type} @ ${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}`; | ||
if (typeof console !== 'undefined') { | ||
const message = `action ${action.type} @ ${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}`; | ||
try { | ||
console.group(message); | ||
} catch(e) { | ||
console.log(message); | ||
} | ||
try { | ||
console.group(message); | ||
} catch(e) { | ||
console.log('NOT GROUP'); | ||
} | ||
console.log(`%c prev state`, `color: #9E9E9E; font-weight: bold`, prevState); | ||
console.log(`%c action`, `color: #03A9F4; font-weight: bold`, action); | ||
console.log(`%c next state`, `color: #4CAF50; font-weight: bold`, nextState); | ||
console.log(`%c prev state`, `color: #9E9E9E; font-weight: bold`, prevState); | ||
console.log(`%c action`, `color: #03A9F4; font-weight: bold`, action); | ||
console.log(`%c next state`, `color: #4CAF50; font-weight: bold`, nextState); | ||
try { | ||
console.groupEnd('—— log end ——'); | ||
} catch(e) { | ||
console.log('—— log end ——'); | ||
} | ||
try { | ||
console.groupEnd(); | ||
} catch(e) { | ||
console.log('—— log end ——'); | ||
} | ||
@@ -28,0 +34,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
217132
28
1155
0
54