Skip to content

Commit

Permalink
Fixed enable and disable methods for extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandro-bottamedi committed Jan 15, 2022
1 parent fb78cbc commit db0c81a
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 24 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [4.0.1] - 15-01-2022

- enable() and disable() methods can now enable or disable extensions

## [4.0.0] - 03-01-2022

In this new major update many of the features requested in the previous issues have been fixed, introduced or improved:
Expand All @@ -14,7 +18,6 @@ In this new major update many of the features requested in the previous issues h
- from this version if no extensions are specified in the configuration then all are printed, otherwise only the specified ones
- 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
- removed css web color support (latest chrome versions support ansi codes)
- enable() and disable() methods can now only enable or disable the whole logger

## [3.0.4] - 04-06-2021

Expand Down
46 changes: 31 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
[![Build Status](https://travis-ci.org/onubo/react-native-logs.svg?branch=master)](https://travis-ci.org/onubo/react-native-logs)
![npm](https://img.shields.io/npm/v/react-native-logs.svg)
![GitHub](https://img.shields.io/github/license/onubo/react-native-logs.svg)
[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](https://github.com/onubo/react-native-logs/issues)
Expand Down Expand Up @@ -146,7 +145,7 @@ whatever you want. The following parameters are received by the function:
- `extension?: string | null`: its namespace if it is an extended log
- `options?: any`: the transportOptions object

You can define your custom transport as follow (example in typescript):
You can define your custom transport as follow (example in typescript)§:

```javascript
import { logger, transportFunctionType } from "react-native-logs";
Expand Down Expand Up @@ -418,18 +417,18 @@ import { logger, consoleTransport } from "react-native-logs";

const config = {
transport: consoleTransport,
enabledExtensions: ['ROOT','HOME']
enabledExtensions: ["ROOT", "HOME"],
};

var log = logger.createLogger(config);
var rootLog = log.extend('ROOT');
var homeLog = log.extend('HOME');
var profileLog = log.extend('PROFILE');

log.debug('print this'): // this will print "<time> | DEBUG | print this"
rootLog.debug('print this'): // this will print "<time> | ROOT | DEBUG | print this"
homeLog.debug('print this'): // this will print "<time> | HOME | DEBUG | print this"
profileLog.debug('not print this'): // this extension is not enabled
var rootLog = log.extend("ROOT");
var homeLog = log.extend("HOME");
var profileLog = log.extend("PROFILE");

log.debug("print this"); // this will print "<time> | DEBUG | print this"
rootLog.debug("print this"); // this will print "<time> | ROOT | DEBUG | print this"
homeLog.debug("print this"); // this will print "<time> | HOME | DEBUG | print this"
profileLog.debug("not print this"); // this extension is not enabled
```

## Methods
Expand All @@ -439,13 +438,30 @@ profileLog.debug('not print this'): // this extension is not enabled
Dynamically enable/disable loggers and extensions, if it is called without parameters then it will disable/enable the whole logger:

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

var log = logger.createLogger();
const config = {
transport: consoleTransport,
enabledExtensions: ["ROOT", "HOME"],
};

var log = logger.createLogger(config);
var rootLog = log.extend("ROOT");
var homeLog = log.extend("HOME");

log.info("print this"); // this will print "<time> | ROOT | INFO | print this"
homeLog.info("print this"); // extension is enabled

log.disable("HOME");

homeLog.info("not print this"); // extension is not enabled
rootLog.info("print this"); // extension is enabled

log.info('print this'): // this will print "<time> | ROOT | INFO | print this"
log.disable();
log.info('not print this'): // logger is not enabled

homeLog.info("not print this"); // logger is not enabled
rootLog.info("not print this"); // logger is not enabled
log.info("not print this"); // logger is not enabled
```

#### getExtensions
Expand Down
50 changes: 42 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,16 +394,50 @@ class logs {
return extendedLog;
};

/** Enable logger */
enable = (): boolean => {
this._enabled = true;
return true;
/** Enable logger or extension */
enable = (extension?: string): boolean => {
if (!extension) {
this._enabled = true;
return true;
}

if (this._extensions.includes(extension)) {
if (this._enabledExtensions) {
if (!this._enabledExtensions.includes(extension)) {
this._enabledExtensions.push(extension);
return true;
} else {
return true;
}
} else {
this._enabledExtensions = [];
this._enabledExtensions.push(extension);
return true;
}
} else {
throw Error(`react-native-logs: Extension [${extension}] not exist`);
}
};

/** Disable logger */
disable = (): boolean => {
this._enabled = false;
return true;
/** Disable logger or extension */
disable = (extension?: string): boolean => {
if (!extension) {
this._enabled = false;
return true;
}
if (this._extensions.includes(extension)) {
if (this._enabledExtensions) {
let extIndex = this._enabledExtensions.indexOf(extension);
if (extIndex > -1) {
this._enabledExtensions.splice(extIndex, 1);
}
return true;
} else {
return true;
}
} else {
throw Error(`react-native-logs: Extension [${extension}] not exist`);
}
};

/** Return all created extensions */
Expand Down

0 comments on commit db0c81a

Please sign in to comment.