|
8 | 8 | ## Table of contents
|
9 | 9 | * [Install](#install)
|
10 | 10 | * [Usage](#usage)
|
11 |
| -* [API](#api) |
| 11 | +* [Options](#options) |
12 | 12 | * [Recipes](#recipes)
|
13 | 13 | * [Log only in development](#log-only-in-development)
|
14 | 14 | * [Log everything except actions with certain type](#log-everything-except-actions-with-certain-type)
|
|
25 | 25 | ## Usage
|
26 | 26 | ```javascript
|
27 | 27 | import { applyMiddleware, createStore } from 'redux';
|
28 |
| -import thunk from 'redux-thunk'; |
29 |
| -import promise from 'redux-promise'; |
30 |
| -import createLogger from 'redux-logger'; |
31 | 28 |
|
32 |
| -const logger = createLogger(); |
| 29 | +// Logger with default options |
| 30 | +import { logger } from 'redux-logger' |
33 | 31 | const store = createStore(
|
34 | 32 | reducer,
|
35 |
| - applyMiddleware(thunk, promise, logger) |
36 |
| -); |
| 33 | + applyMiddleware(logger) |
| 34 | +) |
37 | 35 |
|
38 | 36 | // Note passing middleware as the third argument requires redux@>=3.1.0
|
39 | 37 | ```
|
40 |
| -Logger **must be** the last middleware in chain, otherwise it will log thunk and promise, not actual actions ([#20](https://github.com/evgenyrodionov/redux-logger/issues/20)). |
41 | 38 |
|
42 |
| -## API |
| 39 | +Or you can create your own logger with custom [options](https://github.com/evgenyrodionov/redux-logger#options): |
| 40 | +```javascript |
| 41 | +import { applyMiddleware, createStore } from 'redux'; |
| 42 | +import createLogger from 'redux-logger' |
43 | 43 |
|
44 |
| -`redux-logger` exposes single constructor function for creating logger middleware. |
| 44 | +const logger = createLogger({ |
| 45 | + // ...options |
| 46 | +}); |
45 | 47 |
|
| 48 | +const store = createStore( |
| 49 | + reducer, |
| 50 | + applyMiddleware(logger) |
| 51 | +); |
46 | 52 | ```
|
47 |
| -createLogger(options?: Object) => LoggerMiddleware |
48 |
| -``` |
49 | 53 |
|
50 |
| -### Options |
| 54 | +Note: logger **must be** the last middleware in chain, otherwise it will log thunk and promise, not actual actions ([#20](https://github.com/evgenyrodionov/redux-logger/issues/20)). |
| 55 | + |
| 56 | +## Options |
51 | 57 | ```javascript
|
52 | 58 | {
|
| 59 | + predicate, // if specified this function will be called before each action is processed with this middleware. |
| 60 | + collapsed, // 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. |
| 61 | + duration = false: Boolean, // print the duration of each action? |
| 62 | + timestamp = true: Boolean, // print the timestamp with each action? |
| 63 | + |
53 | 64 | level = 'log': 'log' | 'console' | 'warn' | 'error' | 'info', // console's level
|
54 |
| - duration = false: Boolean, // Print the duration of each action? |
55 |
| - timestamp = true: Boolean, // Print the timestamp with each action? |
56 |
| - colors: ColorsObject, // Object with color getters. See the ColorsObject interface. |
57 |
| - logger = console: LoggerObject, // Implementation of the `console` API. |
58 |
| - logErrors = true: Boolean, // Should the logger catch, log, and re-throw errors? |
59 |
| - collapsed, // 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. |
60 |
| - predicate, // If specified this function will be called before each action is processed with this middleware. |
61 |
| - stateTransformer, // Transform state before print. Eg. convert Immutable object to plain JSON. |
62 |
| - actionTransformer, // Transform state before print. Eg. convert Immutable object to plain JSON. |
63 |
| - errorTransformer, // Transform state before print. Eg. convert Immutable object to plain JSON. |
| 65 | + colors: ColorsObject, // colors for title, prev state, action and next state: https://github.com/evgenyrodionov/redux-logger/blob/master/src/defaults.js#L12-L18 |
64 | 66 | titleFormatter, // Format the title used when logging actions.
|
65 |
| - diff = false: Boolean, // Show diff between states. |
66 |
| - diffPredicate // Filter function for showing states diff.' |
| 67 | + |
| 68 | + stateTransformer, // Transform state before print. Eg. convert Immutable object to plain JSON. |
| 69 | + actionTransformer, // Transform action before print. Eg. convert Immutable object to plain JSON. |
| 70 | + errorTransformer, // Transform error before print. Eg. convert Immutable object to plain JSON. |
| 71 | + |
| 72 | + logger = console: LoggerObject, // implementation of the `console` API. |
| 73 | + logErrors = true: Boolean, // should the logger catch, log, and re-throw errors? |
| 74 | + |
| 75 | + diff = false: Boolean, // (alpha) show diff between states? |
| 76 | + diffPredicate // (alpha) filter function for showing states diff, similar to `predicate` |
67 | 77 | }
|
68 | 78 | ```
|
69 | 79 |
|
70 |
| -### Options |
| 80 | +### Options description |
71 | 81 |
|
72 | 82 | #### __level (String | Function | Object)__
|
73 | 83 | Level of `console`. `warn`, `error`, `info` or [else](https://developer.mozilla.org/en/docs/Web/API/console).
|
@@ -166,8 +176,7 @@ import thunk from 'redux-thunk';
|
166 | 176 | const middlewares = [thunk];
|
167 | 177 |
|
168 | 178 | if (process.env.NODE_ENV === `development`) {
|
169 |
| - const createLogger = require(`redux-logger`); |
170 |
| - const logger = createLogger(); |
| 179 | + const { logger } = require(`redux-logger`); |
171 | 180 | middlewares.push(logger);
|
172 | 181 | }
|
173 | 182 |
|
|
0 commit comments