Skip to content

Commit

Permalink
Merge pull request #16 from farin/master
Browse files Browse the repository at this point in the history
added state transformer (allows better immutable logging)
  • Loading branch information
Eugene Rodionov committed Aug 22, 2015
2 parents e6ca40b + af57529 commit 3d2526d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ Receives `getState` function for accessing current store state and `action` obj

*Default: `null` (always log)*

#### __transform (Function)__
Transform state before print. Eg. convert Immutable object to plain JSON.

*Default: identity function*


##### Examples:
###### log only in dev mode
```javascript
Expand Down
8 changes: 6 additions & 2 deletions build/createLogger.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ function createLogger() {
var collapsed = options.collapsed;
var predicate = options.predicate;
var logger = options.logger;
var _options$transformer = options.transformer;
var transformer = _options$transformer === undefined ? function (state) {
return state;
} : _options$transformer;

var console = logger || window.console;

Expand All @@ -37,9 +41,9 @@ function createLogger() {
return next(action);
}

var prevState = getState();
var prevState = transformer(getState());
var returnValue = next(action);
var nextState = getState();
var nextState = transformer(getState());
var time = new Date();
var actionType = String(action.type);
var message = 'action ' + actionType + ' @ ' + time.getHours() + ':' + time.getMinutes() + ':' + time.getSeconds();
Expand Down
6 changes: 3 additions & 3 deletions src/createLogger.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

function createLogger(options = {}) {
return ({ getState }) => (next) => (action) => {
const { level, collapsed, predicate, logger } = options;
const { level, collapsed, predicate, logger, transformer = state => state} = options;
const console = logger || window.console;

// exit if console undefined
Expand All @@ -23,9 +23,9 @@ function createLogger(options = {}) {
return next(action);
}

const prevState = getState();
const prevState = transformer(getState());
const returnValue = next(action);
const nextState = getState();
const nextState = transformer(getState());
const time = new Date();
const actionType = String(action.type);
const message = `action ${actionType} @ ${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}`;
Expand Down

0 comments on commit 3d2526d

Please sign in to comment.