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
1012ascending 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
1216highest. 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
1826After 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.
5865You can customize the logger by passing a config object to the ` createLogger ` method (see example
5966below). 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
6485import { logger , consoleTransport } from " react-native-logs" ;
6586
6687const 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
85110var 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
119133var log = logger .createLogger (config);
134+
135+ log .silly (" Silly message" );
120136```
121137
122138### Custom transport
@@ -146,6 +162,8 @@ const config = {
146162};
147163
148164var log = logger .createLogger (config);
165+
166+ log .debug (" Debug message" );
149167```
150168
151169### Transport Options
@@ -167,6 +185,8 @@ const config = {
167185};
168186
169187var 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);
186206react-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
192259import { logger , consoleTransport } from " react-native-logs" ;
193260
194261const 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
201282var 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
304419const 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
330442import { logger } from " react-native-logs" ;
331443
332444var 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
379488the ` __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" ;
383496import RNFS from " react-native-fs" ;
384497
385498const 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" ;
408525import RNFS from " react-native-fs" ;
409526
410527const 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```
0 commit comments