Skip to content

Commit dbfd4f9

Browse files
committed
feat: export logger with default settings
1 parent d5adea2 commit dbfd4f9

File tree

3 files changed

+62
-33
lines changed

3 files changed

+62
-33
lines changed

README.md

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
## Table of contents
99
* [Install](#install)
1010
* [Usage](#usage)
11-
* [API](#api)
11+
* [Options](#options)
1212
* [Recipes](#recipes)
1313
* [Log only in development](#log-only-in-development)
1414
* [Log everything except actions with certain type](#log-everything-except-actions-with-certain-type)
@@ -25,49 +25,59 @@
2525
## Usage
2626
```javascript
2727
import { applyMiddleware, createStore } from 'redux';
28-
import thunk from 'redux-thunk';
29-
import promise from 'redux-promise';
30-
import createLogger from 'redux-logger';
3128

32-
const logger = createLogger();
29+
// Logger with default options
30+
import { logger } from 'redux-logger'
3331
const store = createStore(
3432
reducer,
35-
applyMiddleware(thunk, promise, logger)
36-
);
33+
applyMiddleware(logger)
34+
)
3735

3836
// Note passing middleware as the third argument requires redux@>=3.1.0
3937
```
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)).
4138

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'
4343

44-
`redux-logger` exposes single constructor function for creating logger middleware.
44+
const logger = createLogger({
45+
// ...options
46+
});
4547

48+
const store = createStore(
49+
reducer,
50+
applyMiddleware(logger)
51+
);
4652
```
47-
createLogger(options?: Object) => LoggerMiddleware
48-
```
4953

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
5157
```javascript
5258
{
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+
5364
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
6466
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`
6777
}
6878
```
6979

70-
### Options
80+
### Options description
7181

7282
#### __level (String | Function | Object)__
7383
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';
166176
const middlewares = [thunk];
167177

168178
if (process.env.NODE_ENV === `development`) {
169-
const createLogger = require(`redux-logger`);
170-
const logger = createLogger();
179+
const { logger } = require(`redux-logger`);
171180
middlewares.push(logger);
172181
}
173182

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "redux-logger",
3-
"version": "2.8.2",
3+
"version": "2.9.0",
44
"description": "Logger for Redux",
55
"main": "lib/index.js",
66
"scripts": {

src/index.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,28 @@ function createLogger(options = {}) {
4949
// Detect if 'createLogger' was passed directly to 'applyMiddleware'.
5050
if (options.getState && options.dispatch) {
5151
// eslint-disable-next-line no-console
52-
console.error(`redux-logger not installed. Make sure to pass logger instance as middleware:
52+
console.error(`[redux-logger] redux-logger not installed. Make sure to pass logger instance as middleware:
53+
54+
// Logger with default options
55+
import { logger } from 'redux-logger'
56+
const store = createStore(
57+
reducer,
58+
applyMiddleware(logger)
59+
)
5360
54-
import createLogger from 'redux-logger';
5561
56-
const logger = createLogger();
62+
// Or you can create your own logger with custom options http://bit.ly/redux-logger-options
63+
import createLogger from 'redux-logger'
64+
65+
const logger = createLogger({
66+
// ...options
67+
});
68+
5769
const store = createStore(
5870
reducer,
5971
applyMiddleware(logger)
60-
);`);
72+
)
73+
`);
6174

6275
return () => next => action => next(action);
6376
}
@@ -102,4 +115,11 @@ const store = createStore(
102115
};
103116
}
104117

118+
const defaultLogger = createLogger();
119+
120+
export {
121+
defaults,
122+
defaultLogger as logger,
123+
};
124+
105125
export default createLogger;

0 commit comments

Comments
 (0)