Palzin Track
Get 15% off with code PTRACKSIGNUP15 

Laravel Diary Logo

How to hook Google Console Practical Example

tooling
Table of Contents

Yesterday, I was working on one of the project whereas we needed to start logging the data from Console as the 3rd Party applications are publishing the log in the console.

As we all know that when the system becomes complex it is very hard to keep the track of which functionality is developed for what purpose. It was similar situation so I started reviewing my private snippet and found these that can help you if you have similar problems.

    var _log = window.console ? 
               window.console.log : function(){};

    _log.history = [];

    console.log = function( ){
        
      var args = Array.prototype.slice.call( arguments );
      _log.history.push.apply( _log.history, args );
      _log.apply( window.console, args );
        
      //OMG custom logic, note forEach is newish dont use in old browsers
      args.forEach(function( n, i ){
          // You can write here or submit the request from JS to push this output to any browser. 
          document.write( 
              '<pre class="code">' + 
              JSON.stringify( n, undefined, 2 ) +
              '</pre>' );
      });
    }
        
    //Example usage
    console.log({ 
      complex: 'data', 
      object: { 
        neato: [1,2,3,4]
      }
    });

    //What's in the history now use native console.log
    _log.call( console, _log.history );


Below is the snippet that can be linked with my previous article.

    var defaultLog = console.warn.bind(console);
		console.warns=[];
    


    console.warn = function( ){
        
      defaultLog.apply(console, arguments);
      console.warns.push(Array.from(arguments));
      //OMG custom logic, note forEach is newish dont use in old browsers
      console.warns.forEach(function( n, i ){
          // You can write here or submit the request from JS to push this output to any browser. 
          document.write( 
              'Warning::<pre class="code">' + 
              JSON.stringify( n, undefined, 2 ) +
              '</pre>' );
      });
    }
    

        var defaultLog1 = console.log.bind(console);
			console.logs=[];
    

console.log = function( ){
        
      defaultLog1.apply(console, arguments);
      console.logs.push(Array.from(arguments));
      //OMG custom logic, note forEach is newish dont use in old browsers
      console.logs.forEach(function( n, i ){
          // You can write here or submit the request from JS to push this output to any browser. 
          document.write( 
              'LOG::<pre class="code">' + 
              JSON.stringify( n, undefined, 2 ) +
              '</pre>' );
      });
    }
    
        
    //Example usage
    console.warn({ 
      complex: 'data', 
      object: { 
        neato: [1,2,3,4]
      }
    });

::Share it on::

Comments (0)

What are your thoughts on "How to hook Google Console Practical Example"?

You need to create an account to comment on this post.

Related articles