Skip to content

Commit b3261c0

Browse files
author
Eugene Rodionov
committed
Merge pull request #25 from brandonlilly/master
Collapsed can take function
2 parents 5122121 + ee51803 commit b3261c0

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

README.md

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ Logger **must be** last middleware in chain, otherwise it will log thunk and pro
2525
__createLogger(options?: Object)__
2626

2727
### Options
28-
#### __collapsed (Boolean)__
29-
Is group collapsed?
30-
31-
*Default: `false`*
3228

3329
#### __level (String)__
3430
Level of `console`. `warn`, `error`, `info` or [else](https://developer.mozilla.org/en/docs/Web/API/console).
@@ -61,6 +57,11 @@ Receives `getState` function for accessing current store state and `action` obj
6157

6258
*Default: `null` (always log)*
6359

60+
#### __collapsed (getState: Function, action: Object): boolean__
61+
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.
62+
63+
*Default: `false`*
64+
6465
##### Examples:
6566
###### log only in dev mode
6667
```javascript
@@ -72,13 +73,26 @@ createLogger({
7273
```
7374

7475
###### log everything except actions with type `AUTH_REMOVE_TOKEN`
75-
7676
```javascript
7777
createLogger({
7878
predicate: (getState, action) => action.type !== AUTH_REMOVE_TOKEN
7979
});
8080
```
8181

82+
###### collapse all actions
83+
```javascript
84+
createLogger({
85+
collapsed: true
86+
});
87+
```
88+
89+
###### collapse actions with type `FORM_CHANGE`
90+
```javascript
91+
createLogger({
92+
collapsed: (getState, action) => action.type === FORM_CHANGE
93+
});
94+
```
95+
8296
###### transform Immutable objects into JSON
8397
```javascript
8498
createLogger({

src/createLogger.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const timer = typeof performance !== `undefined` ? performance : Date;
1010
* @property {object} options - options for logger
1111
* @property {string} level - console[level]
1212
* @property {boolean} collapsed - is group collapsed?
13-
* @property {bool} predicate - condition which resolves logger behavior
13+
* @property {boolean} predicate - condition which resolves logger behavior
1414
*/
1515

1616
function createLogger(options = {}) {
@@ -45,7 +45,11 @@ function createLogger(options = {}) {
4545
const actionType = String(action.type);
4646
const message = `action ${actionType}${formattedTime}${formattedDuration}`;
4747

48-
if (collapsed) {
48+
const isCollapsed = (typeof collapsed === 'function') ?
49+
collapsed(getState, action) :
50+
collapsed;
51+
52+
if (isCollapsed) {
4953
try {
5054
console.groupCollapsed(message);
5155
} catch (e) {

0 commit comments

Comments
 (0)