mowispace/react-native-logs

Disable not working

devilankur18 opened this issue · 2 comments

Logs are showing up after disabling an extension.

   const name = "Axios"
   const modulelog = LOG.extend(name);
    LOG.warn(LOG.getExtensions());
    LOG.disable(name);
    LOG.warn(LOG.getExtensions());
    // test
    modulelog.debug('bla bla');
 LOG  1:45:53 PM | WARN : 
[
  "Wisdom",
  "Navigation",
  "Axios",
  "Notification"
]

 LOG  1:45:53 PM | WARN : Disabling module Axios 
 LOG  1:45:53 PM | WARN : 
[
  "Wisdom",
  "Navigation",
  "Axios",
  "Notification"
]

 LOG  1:45:53 PM |  Axios  | DEBUG : bla bla 

It's a bug.

Let's say you have create three extensions, One, Two, and Three:

const logger = createLogger();

const oneLogger = logger.extend('One');
const twoLogger = logger.extend('Two');
const threeLogger = logger.extend('Three');

oneLogger.debug('1'); // outputs 'One | DEBUG: 1'
twoLogger.debug('2'): // outputs 'Two | DEBUG: 2'
threeLogger.debug('3'): // outputs 'Three | DEBUG: 3'

There's an _enabledExtensions property, which is initially null. When null, all extensions are considered enabled.
Disabling an extension does not modify this property, if it is null:

logger.disable('One');
oneLogger.debug('1'); // outputs 'One | DEBUG: 1'
twoLogger.debug('2'): // outputs 'Two | DEBUG: 2'
threeLogger.debug('3'): // outputs 'Three | DEBUG: 3'

However, as soon as you explicitly enable one extension, _enabledExtensions becomes an array (eg. ['One']). The problem is, this disables all other extensions, which then have to be enabled explicitly.

logger.enable('One');

oneLogger.debug('1'); // outputs 'One | DEBUG: 1'
twoLogger.debug('2'): // no output
threeLogger.debug('3'): // no output

logger.enable('Three');
logger.disable('One');

oneLogger.debug('1'); // no output
twoLogger.debug('2'): // no output
threeLogger.debug('3'): // outputs 'Three | DEBUG: 3'

One way around this is to call enable() on at least one extension, but that might be unpredictable.

Another way is to pass enabledExtensions: [] to the config. This will initially disable all extensions and you'll have to enable them individually.

const config = {
  // ... other config values,
  enabledExtensions: [],
};
const logger = createLogger(config);

const oneLogger = logger.extend('One');
const twoLogger = logger.extend('Two');
const threeLogger = logger.extend('Three');

oneLogger.enable();
twoLogger.enable();

oneLogger.debug('1'); // outputs 'One | DEBUG: 1'
twoLogger.debug('2'): // outputs 'Two | DEBUG: 2'
threeLogger.debug('3'): // no output