Skip to content

Commit fb78cbc

Browse files
new ver 4.0.0
1 parent 99dfe1c commit fb78cbc

14 files changed

+1573
-2437
lines changed

CHANGELOG.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
## [4.0.0] - 03-01-2022
2+
3+
In this new major update many of the features requested in the previous issues have been fixed, introduced or improved:
4+
5+
- reversed the extension mechanism, now if they are not specified, they will all be displayed
6+
- added the ability to choose the colors of the levels for the consoleTransport
7+
- added the ability to choose the colors of extensions in consoleTransport
8+
- added a transport that prints logs with the native console methods (log, info, error, etc ...)
9+
- fixed type exports
10+
- minor bugfix
11+
12+
#### BREAKING CHANGES
13+
14+
- from this version if no extensions are specified in the configuration then all are printed, otherwise only the specified ones
15+
- the colors option for the consoleTransport must now be set with the desired colors for each level (see the readme), if not set the logs will not be colored
16+
- removed css web color support (latest chrome versions support ansi codes)
17+
- enable() and disable() methods can now only enable or disable the whole logger
18+
119
## [3.0.4] - 04-06-2021
220

321
- queue management to avoid race conditions problems with ExpoFS

README.md

Lines changed: 174 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,26 @@
55

66
# react-native-logs
77

8-
Performance-aware simple logger for React-Native and Expo (managed and bare) with custom levels and transports (colored console,
9-
file writing, etc.). Each level has its severity: a number that represents its importance in
8+
Performance-aware simple logger for React-Native Expo (managed and bare) and react-native-web with custom levels and transports (colored console,
9+
file writing, etc.).
10+
11+
Each level has its severity: a number that represents its importance in
1012
ascending order from the least important to the most important. Eg. _debug:0, info:1, warn:2,
11-
error:3_. By config the logger with a minium severity level, you will see only the logs that have it
13+
error:3_.
14+
15+
By config the logger with a minium severity level, you will see only the logs that have it
1216
highest. Then logs will be managed by transport: the function that will display/save/send log
13-
messages. It is also possible to create namespaced logs that allow logging to be enabled only for
14-
certain parts of the app.
17+
messages.
18+
19+
It is also possible to extend the logger to create namespaced logs. In this way you will be able to see the log messages only for one or some parts of the code of your choice
20+
21+
**Demo console transport with custom colored levels and namespaces:**
22+
![console log demo](https://raw.githubusercontent.com/onubo/react-native-logs/master/demo/demo-react-native-logs.png)
1523

1624
## Why another logging library?
1725

1826
After trying the most known logging libraries, like winston and bunyan, we found that for
19-
react-native we needed something simpler, but still flexible, and without dependencies on nodejs (we
20-
don't like the rn-nodeify solution). Comments and suggestions are welcome.
27+
react-native we needed something simpler, but still flexible, and without dependencies on nodejs. Comments and suggestions are welcome.
2128

2229
## Installation
2330

@@ -58,23 +65,41 @@ and error levels.
5865
You can customize the logger by passing a config object to the `createLogger` method (see example
5966
below). All params are optional and will take default values if no corresponding argument is passed.
6067

61-
**Example with common configuration:**
68+
| Parameter | Type | Description | Default |
69+
| ----------------- | -------- | ----------------------------------------------------------- | ----------------------------------------- |
70+
| severity | string | Init logs severity (least important level you want to see) | `debug` (or the first custom level) |
71+
| transport | Function | The transport function for logs (see below for presets) | The preset transport `consoleTransport` |
72+
| transportOptions | Object | Set custom options for transports | `null` |
73+
| levels | Object | Set custom log levels: {name:power} | `false` |
74+
| async | boolean | Set to true for async logs (to improve app performance) | `true` |
75+
| asyncFunc | Function | Set a cutom async function `(cb: Function)=>{return cb()}` | `InteractionManager.runAfterInteractions` |
76+
| dateFormat | string | Choose between only `time` or a date: `local`, `utc`, `iso` | `time` |
77+
| printLevel | boolean | Choose whether to print the log level | `true` |
78+
| printDate | boolean | Choose whether to print the log date/time | `true` |
79+
| enabled | boolean | Enable or disable logging | `true` |
80+
| enabledExtensions | string[] | Enable only certain namespaces | `null` |
81+
82+
#### Example with common configuration
6283

6384
```javascript
6485
import { logger, consoleTransport } from "react-native-logs";
6586

6687
const defaultConfig = {
67-
severity: "debug",
68-
transport: consoleTransport,
69-
transportOptions: {
70-
color: "ansi", // custom option that color consoleTransport logs
71-
},
7288
levels: {
7389
debug: 0,
7490
info: 1,
7591
warn: 2,
7692
error: 3,
7793
},
94+
severity: "debug",
95+
transport: consoleTransport,
96+
transportOptions: {
97+
colors: {
98+
info: "blueBright",
99+
warn: "yellowBright",
100+
error: "redBright",
101+
},
102+
},
78103
async: true,
79104
dateFormat: "time",
80105
printLevel: true,
@@ -83,21 +108,10 @@ const defaultConfig = {
83108
};
84109

85110
var log = logger.createLogger(defaultConfig);
86-
```
87111

88-
| Parameter | Type | Description | Default |
89-
| ----------------- | -------- | ----------------------------------------------------------- | ----------------------------------------- |
90-
| severity | string | Init logs severity (least important level you want to see) | `debug` (or the first custom level) |
91-
| transport | Function | The transport function for logs (see below for presets) | The preset transport `consoleTransport` |
92-
| transportOptions | Object | Set custom options for transports | `null` |
93-
| levels | Object | Set custom log levels: {name:power} | `false` |
94-
| async | boolean | Set to true for async logs (to improve app performance) | `true` |
95-
| asyncFunc | Function | Set a cutom async function `(cb: Function)=>{return cb()}` | `InteractionManager.runAfterInteractions` |
96-
| dateFormat | string | Choose between only `time` or a date: `local`, `utc`, `iso` | `time` |
97-
| printLevel | boolean | Choose whether to print the log level | `true` |
98-
| printDate | boolean | Choose whether to print the log date/time | `true` |
99-
| enabled | boolean | Enable or disable logging | `true` |
100-
| enabledExtensions | string[] | List of enabled namepaces | `[]` |
112+
log.debug("Debug message");
113+
log.info({ message: "hi!" });
114+
```
101115

102116
### Custom levels
103117

@@ -117,6 +131,8 @@ const config = {
117131
};
118132

119133
var log = logger.createLogger(config);
134+
135+
log.silly("Silly message");
120136
```
121137

122138
### Custom transport
@@ -146,6 +162,8 @@ const config = {
146162
};
147163

148164
var log = logger.createLogger(config);
165+
166+
log.debug("Debug message");
149167
```
150168

151169
### Transport Options
@@ -167,6 +185,8 @@ const config = {
167185
};
168186

169187
var log = logger.createLogger(config);
188+
189+
log.debug("Debug message");
170190
```
171191

172192
### Multiple Arguments
@@ -186,30 +206,125 @@ log.error("New error occured", errorObject);
186206
react-native-logs includes some preset transports. You can import the one of your choice:
187207
`import { logger, <transportName> } from 'react-native-logs';`
188208

189-
#### Example:
209+
#### Example
210+
211+
```javascript
212+
import { logger, mapConsoleTransport } from "react-native-logs";
213+
214+
const config = {
215+
transport: mapConsoleTransport,
216+
};
217+
218+
var log = logger.createLogger(config);
219+
220+
log.debug("Debug message");
221+
```
222+
223+
## List of included preset transports
224+
225+
### **consoleTransport**
226+
227+
Print the logs with a formatted `console.log` output.
228+
229+
| name | type | description | default |
230+
| --------------- | ------ | ------------------------------------------------------------------------ | ------- |
231+
| colors | object | If setted you can choose the log colors, defined by level: {level:color} | `null` |
232+
| extensionColors | object | If setted you can choose the extension label colors: {extension:color} | `null` |
233+
234+
#### Available colors
235+
236+
| name | ansi code | note |
237+
| ------------- | --------- | --------------------- |
238+
| default | null | default console color |
239+
| black | 30 |
240+
| red | 31 |
241+
| green | 32 |
242+
| yellow | 33 |
243+
| blue | 34 |
244+
| magenta | 35 |
245+
| cyan | 36 |
246+
| white | 37 |
247+
| grey | 90 |
248+
| redBright | 91 |
249+
| greenBright | 92 |
250+
| yellowBright | 93 |
251+
| blueBright | 94 |
252+
| magentaBright | 95 |
253+
| cyanBright | 96 |
254+
| whiteBright | 97 |
255+
256+
#### Example
190257

191258
```javascript
192259
import { logger, consoleTransport } from "react-native-logs";
193260

194261
const config = {
262+
levels: {
263+
debug: 0,
264+
info: 1,
265+
warn: 2,
266+
error: 3,
267+
},
195268
transport: consoleTransport,
196269
transportOptions: {
197-
colors: `ansi`,
270+
colors: {
271+
info: "blueBright",
272+
warn: "yellowBright",
273+
error: "redBright",
274+
},
275+
extensionColors: {
276+
root: "magenta",
277+
home: "green",
278+
},
198279
},
199280
};
200281

201282
var log = logger.createLogger(config);
283+
var rootLog = log.extend("root");
284+
var homeLog = log.extend("home");
285+
286+
rootLog.info("Magenta Extension and bright blue message");
287+
homeLog.error("Green Extension and bright red message");
202288
```
203289

204-
## List of included preset transports
290+
### **mapConsoleTransport**
205291

206-
### **consoleTransport**
292+
Print the logs with a selected `console` method (`console.log`, `console.warn`, `console.error`, etc.).
207293

208-
Print the logs with a formatted `console.log` output.
294+
| name | type | description | default |
295+
| --------- | ------ | -------------------------------------------------- | ------- |
296+
| mapLevels | object | Select the console method by level: {level:method} | `null` |
209297

210-
| name | type | description | default |
211-
| ------ | ------ | ------------------------------------------------------------------------------------------------------------------ | ------- |
212-
| colors | string | Choose between `null` (no colors), `web` (colors for chrome console), `ansi` (colors for system or vscode console) | `null` |
298+
If mapLevels is not setted, the transport will try to map the console methods with the level name.
299+
300+
#### Example
301+
302+
```javascript
303+
import { logger, mapConsoleTransport } from "react-native-logs";
304+
305+
const config = {
306+
levels: {
307+
debug: 0,
308+
info: 1,
309+
warn: 2,
310+
err: 3,
311+
},
312+
transport: mapConsoleTransport,
313+
transportOptions: {
314+
mapLevels: {
315+
debug: "log",
316+
info: "info",
317+
warn: "warn",
318+
err: "error",
319+
},
320+
},
321+
};
322+
323+
var log = logger.createLogger(config);
324+
325+
log.debug("Print this with console.log");
326+
log.err("Print this with console.error");
327+
```
213328

214329
### **fileAsyncTransport**
215330

@@ -303,9 +418,6 @@ import { logger, consoleTransport } from "react-native-logs";
303418

304419
const config = {
305420
transport: consoleTransport,
306-
transportOptions: {
307-
colors: `ansi`,
308-
},
309421
enabledExtensions: ['ROOT','HOME']
310422
};
311423

@@ -330,13 +442,10 @@ Dynamically enable/disable loggers and extensions, if it is called without param
330442
import { logger } from "react-native-logs";
331443

332444
var log = logger.createLogger();
333-
var rootLog = log.extend('ROOT');
334445

335-
rootLog.info('not print this'): // this extension is not enabled
336-
log.enable('ROOT');
337-
rootLog.info('print this'): // this will print "<time> | ROOT | INFO | print this"
338-
log.disable('ROOT');
339-
rootLog.info('not print this'): // this extension is not enabled
446+
log.info('print this'): // this will print "<time> | ROOT | INFO | print this"
447+
log.disable();
448+
log.info('not print this'): // logger is not enabled
340449
```
341450

342451
#### getExtensions
@@ -379,14 +488,18 @@ In reacly-native, after you have create your logger, you can set to log only in
379488
the `__DEV__` as follows:
380489

381490
```javascript
382-
import { logger, consoleTransport, fileAsyncTransport } from "react-native-logs";
491+
import {
492+
logger,
493+
consoleTransport,
494+
fileAsyncTransport,
495+
} from "react-native-logs";
383496
import RNFS from "react-native-fs";
384497

385498
const config = {
386499
transport: __DEV__ ? consoleTransport : fileAsyncTransport,
387500
severity: __DEV__ ? "debug" : "error",
388501
transportOptions: {
389-
colors: `ansi`,
502+
colors
390503
FS: RNFS,
391504
},
392505
};
@@ -404,14 +517,22 @@ initialize the logger so it can be imported wherever it is needed. Example:
404517

405518
```javascript
406519
//config.js
407-
import { logger, consoleTransport, fileAsyncTransport } from "react-native-logs";
520+
import {
521+
logger,
522+
consoleTransport,
523+
fileAsyncTransport,
524+
} from "react-native-logs";
408525
import RNFS from "react-native-fs";
409526

410527
const config = {
411528
transport: __DEV__ ? consoleTransport : fileAsyncTransport,
412529
severity: __DEV__ ? "debug" : "error",
413530
transportOptions: {
414-
colors: `ansi`,
531+
colors: {
532+
info: "blueBright",
533+
warn: "yellowBright",
534+
error: "redBright",
535+
},
415536
FS: RNFS,
416537
},
417538
};
@@ -485,7 +606,11 @@ const log = logger.createLogger({
485606
transportOptions: {
486607
FS: RNFS,
487608
SENTRY: Sentry,
488-
colors: "ansi",
609+
colors: {
610+
info: "blueBright",
611+
warn: "yellowBright",
612+
error: "redBright",
613+
},
489614
},
490615
});
491616
```

demo/demo-react-native-logs.png

60.1 KB
Loading

0 commit comments

Comments
 (0)