Is there a way in JavaScript to listen console events?

前端 未结 6 2367
悲&欢浪女
悲&欢浪女 2020-12-05 04:53

I\'m trying to write handler for uncaught exceptions and browser warnings in Javascript. All errors and warnings should be sent to server for later review.

Handled e

相关标签:
6条回答
  • 2020-12-05 05:32

    I needed to debug console output on mobile devices so I built this drop-in library to capture console output and category and dump it to the page. Check the source code, it's quite straightforward.

    https://github.com/samsonradu/Consolify

    0 讨论(0)
  • 2020-12-05 05:33

    More Succint Way:

    // this method will proxy your custom method with the original one
    function proxy(context, method, message) { 
      return function() {
        method.apply(context, [message].concat(Array.prototype.slice.apply(arguments)))
      }
    }
    
    // let's do the actual proxying over originals
    console.log = proxy(console, console.log, 'Log:')
    console.error = proxy(console, console.error, 'Error:')
    console.warn = proxy(console, console.warn, 'Warning:')
    
    // let's test
    console.log('im from console.log', 1, 2, 3);
    console.error('im from console.error', 1, 2, 3);
    console.warn('im from console.warn', 1, 2, 3);

    0 讨论(0)
  • 2020-12-05 05:36

    You're going about this backwards. Instead of intercepting when an error is logged, trigger an event as part of the error handling mechanism and log it as one of the event listeners:

    try
    {
      //might throw an exception
      foo();
    }
    catch (e)
    {
      $(document).trigger('customerror', e);
    }
    
    function customErrorHandler(event, ex)
    {
      console.error(ex)
    }
    function customErrorHandler2(event, ex)
    {
      $.post(url, ex);
    }
    

    this code uses jQuery and is oversimplified strictly for use as an example.

    0 讨论(0)
  • 2020-12-05 05:43

    In the same function that you are using to do console.log(), simply post the same message to a web service that you are recording the logs on.

    0 讨论(0)
  • 2020-12-05 05:46

    You could just wrap the console methods yourself. For example, to record each call in an array:

    var logOfConsole = [];
    
    var _log = console.log,
        _warn = console.warn,
        _error = console.error;
    
    console.log = function() {
        logOfConsole.push({method: 'log', arguments: arguments});
        return _log.apply(console, arguments);
    };
    
    console.warn = function() {
        logOfConsole.push({method: 'warn', arguments: arguments});
        return _warn.apply(console, arguments);
    };
    
    console.error = function() {
        logOfConsole.push({method: 'error', arguments: arguments});
        return _error.apply(console, arguments);
    };
    
    0 讨论(0)
  • 2020-12-05 05:47

    I know it's an old post but it can be useful anyway as others solution are not compatible with older browsers.

    You can redefine the behavior of each function of the console (and for all browsers) like this:

    // define a new console
    var console = (function(oldCons){
        return {
            log: function(text){
                oldCons.log(text);
                // Your code
            },
            info: function (text) {
                oldCons.info(text);
                // Your code
            },
            warn: function (text) {
                oldCons.warn(text);
                // Your code
            },
            error: function (text) {
                oldCons.error(text);
                // Your code
            }
        };
    }(window.console));
    
    //Then redefine the old console
    window.console = console;
    
    0 讨论(0)
提交回复
热议问题