skip to Main Content

I have a load of console.logs in my code, but I only need to see them/ turn them on if I need to see them on a site, so I want a way to be able to enable/ disbale them from in browser console by setting up a couple of snippets, such as: adysis.debug(‘enable’) , adysis.debug(‘disable’) but how can i get these to them trigger the console.logs in the code, I got no clue at all at the moment on how to do it?

2

Answers


  1. It sounds like you want to control the visibility of console.log statements in your code using commands like adysis.debug('enable') and adysis.debug('disable') that you can run from the browser console. To achieve this, you can create a custom debugging utility that wraps around the console.log function and toggles its behavior based on your commands.

    // Custom debugging utility
    const adysis = {
      debugEnabled: false,
    
      debug: function(action) {
        if (action === 'enable') {
          this.debugEnabled = true;
          console.log('Debugging enabled.');
        } else if (action === 'disable') {
          this.debugEnabled = false;
          console.log('Debugging disabled.');
        }
      },
    
      log: function(...args) {
        if (this.debugEnabled) {
          console.log(...args);
        }
      }
    };
    
    // Usage in your code
    function someFunction() {
      adysis.log('This will only be logged if debugging is enabled.');
      // ... rest of your code ...
    }
    
    // Usage in the browser console
    adysis.debug('enable');  // Enable debugging
    someFunction();          // This log will appear in the console
    adysis.debug('disable'); // Disable debugging
    someFunction();          // This log will not appear in the console
    
    Login or Signup to reply.
  2. To disable ALL the console’s methods use Proxy.

    Use console.disabled = true/false to disable/enable console.

    window.console = new Proxy(window.console, {
      get(console, prop){
        if(console.disabled && typeof console[prop] === 'function' && Object.hasOwn(console, prop)){
          return function(){}
        }
        return Reflect.get(...arguments);
      }
    });
    
    console.log('test');
    console.dir(document.body);
    
    console.disabled = true;
    
    console.log('disabled');
    console.dir(document.body);
    
    $pre.textContent = 'console.log is enumerable: ' + console.propertyIsEnumerable('log');
    
    console.disabled = false;
    
    console.log('enabled');
    console.dir(document.body);
    <pre id="$pre"></pre>

    If you want it disabled initially:

    window.console = new Proxy(window.console, {
      get(console, prop){
        if(!console.enabled && typeof console[prop] === 'function' && Object.hasOwn(console, prop)){
          return function(){}
        }
        return Reflect.get(...arguments);
      }
    });
    
    
    console.log('disabled');
    console.dir(document.body);
    
    $pre.textContent = 'console.log is enumerable: ' + console.propertyIsEnumerable('log');
    
    console.enabled = true;
    
    console.log('enabled');
    console.dir(document.body);
    <pre id="$pre"></pre>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search