Skip to content

Commit bf3fefa

Browse files
author
Eugene Rodionov
committed
Merge pull request #5 from wizardzloy/feature/add-logger-predicate
Add logger predicate
2 parents b05a7b2 + f428b1d commit bf3fefa

File tree

4 files changed

+58
-39
lines changed

4 files changed

+58
-39
lines changed

build/logger.js

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,26 @@
33
Object.defineProperty(exports, '__esModule', {
44
value: true
55
});
6-
function logger(_ref) {
7-
var getState = _ref.getState;
6+
function createLogger() {
7+
var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
8+
9+
return function (_ref) {
10+
var getState = _ref.getState;
11+
return function (next) {
12+
return function (action) {
13+
if (typeof console === 'undefined') {
14+
return next(action);
15+
}
816

9-
return function (next) {
10-
return function (action) {
11-
var prevState = getState();
12-
var returnValue = next(action);
13-
var nextState = getState();
14-
var time = new Date();
17+
// exit early if predicate function returns false
18+
if (typeof options.predicate === 'function' && !options.predicate(getState, action)) {
19+
return next(action);
20+
}
1521

16-
if (typeof console !== 'undefined') {
22+
var prevState = getState();
23+
var returnValue = next(action);
24+
var nextState = getState();
25+
var time = new Date();
1726
var message = 'action ' + action.type + ' @ ' + time.getHours() + ':' + time.getMinutes() + ':' + time.getSeconds();
1827

1928
try {
@@ -31,12 +40,12 @@ function logger(_ref) {
3140
} catch (e) {
3241
console.log('—— log end ——');
3342
}
34-
}
3543

36-
return returnValue;
44+
return returnValue;
45+
};
3746
};
3847
};
3948
}
4049

41-
exports['default'] = logger;
50+
exports['default'] = createLogger;
4251
module.exports = exports['default'];

example/app.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import React from 'react';
2-
import logger from 'redux-logger';
2+
import createLogger from 'redux-logger';
33

44
import { createStore, combineReducers, applyMiddleware } from 'redux';
55
import { Provider } from 'react-redux';
66

77
import reducers from './reducers';
8+
import { AUTH_REMOVE_TOKEN } from './constants/auth.js';
89

10+
const logger = createLogger({
11+
predicate: (getState, action) => action.type !== AUTH_REMOVE_TOKEN // log all actions except AUTH_REMOVE_TOKEN
12+
});
913
const createStoreWithMiddleware = applyMiddleware(logger)(createStore);
1014
const reducer = combineReducers(reducers);
1115
const store = createStoreWithMiddleware(reducer);

example/dist/bundle.js

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/logger.js

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,38 @@
1-
function logger({ getState }) {
2-
return (next) => (action) => {
1+
function createLogger(options = {}) {
2+
return ({ getState }) => (next) => (action) => {
3+
if (typeof console === 'undefined') {
4+
return next(action);
5+
}
6+
7+
// exit early if predicate function returns false
8+
if (typeof options.predicate === 'function' && !options.predicate(getState, action)) {
9+
return next(action);
10+
}
11+
312
const prevState = getState();
413
const returnValue = next(action);
514
const nextState = getState();
615
const time = new Date();
16+
const message = `action ${action.type} @ ${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}`;
717

8-
if (typeof console !== 'undefined') {
9-
const message = `action ${action.type} @ ${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}`;
10-
11-
try {
12-
console.group(message);
13-
} catch(e) {
14-
console.log('NOT GROUP');
15-
}
18+
try {
19+
console.group(message);
20+
} catch(e) {
21+
console.log('NOT GROUP');
22+
}
1623

17-
console.log(`%c prev state`, `color: #9E9E9E; font-weight: bold`, prevState);
18-
console.log(`%c action`, `color: #03A9F4; font-weight: bold`, action);
19-
console.log(`%c next state`, `color: #4CAF50; font-weight: bold`, nextState);
24+
console.log(`%c prev state`, `color: #9E9E9E; font-weight: bold`, prevState);
25+
console.log(`%c action`, `color: #03A9F4; font-weight: bold`, action);
26+
console.log(`%c next state`, `color: #4CAF50; font-weight: bold`, nextState);
2027

21-
try {
22-
console.groupEnd('—— log end ——');
23-
} catch(e) {
24-
console.log('—— log end ——');
25-
}
28+
try {
29+
console.groupEnd('—— log end ——');
30+
} catch(e) {
31+
console.log('—— log end ——');
2632
}
2733

2834
return returnValue;
2935
};
3036
}
3137

32-
export default logger;
38+
export default createLogger;

0 commit comments

Comments
 (0)