Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions build/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,26 @@
Object.defineProperty(exports, '__esModule', {
value: true
});
function logger(_ref) {
var getState = _ref.getState;
function createLogger() {
var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];

return function (_ref) {
var getState = _ref.getState;
return function (next) {
return function (action) {
if (typeof console === 'undefined') {
return next(action);
}

return function (next) {
return function (action) {
var prevState = getState();
var returnValue = next(action);
var nextState = getState();
var time = new Date();
// exit early if predicate function returns false
if (typeof options.predicate === 'function' && !options.predicate(getState, action)) {
return next(action);
}

if (typeof console !== 'undefined') {
var prevState = getState();
var returnValue = next(action);
var nextState = getState();
var time = new Date();
var message = 'action ' + action.type + ' @ ' + time.getHours() + ':' + time.getMinutes() + ':' + time.getSeconds();

try {
Expand All @@ -31,12 +40,12 @@ function logger(_ref) {
} catch (e) {
console.log('—— log end ——');
}
}

return returnValue;
return returnValue;
};
};
};
}

exports['default'] = logger;
exports['default'] = createLogger;
module.exports = exports['default'];
6 changes: 5 additions & 1 deletion example/app.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import React from 'react';
import logger from 'redux-logger';
import createLogger from 'redux-logger';

import { createStore, combineReducers, applyMiddleware } from 'redux';
import { Provider } from 'react-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
});
const createStoreWithMiddleware = applyMiddleware(logger)(createStore);
const reducer = combineReducers(reducers);
const store = createStoreWithMiddleware(reducer);
Expand Down
14 changes: 7 additions & 7 deletions example/dist/bundle.js

Large diffs are not rendered by default.

44 changes: 25 additions & 19 deletions src/logger.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
function logger({ getState }) {
return (next) => (action) => {
function createLogger(options = {}) {
return ({ getState }) => (next) => (action) => {
if (typeof console === 'undefined') {
return next(action);
}

// exit early if predicate function returns false
if (typeof options.predicate === 'function' && !options.predicate(getState, action)) {
return next(action);
}

const prevState = getState();
const returnValue = next(action);
const nextState = getState();
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('NOT GROUP');
}
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('—— log end ——');
} catch(e) {
console.log('—— log end ——');
}

return returnValue;
};
}

export default logger;
export default createLogger;