Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I override console.log() and prepend a word at the beginning of the output?

Tags:

I have the function Log which prints data along with passed arguments, how can I print the content & at the same time always print the word "Report: " at the beginning of the log.

function Log(){     if (app.debug_logs){         if(console && console.log){             console.log.apply(console, "Report: " + arguments);         }     } }  Log(" error occurred ", " on this log... "); 

I want to have: "Report: error occurred on this log..."

Thanks.

like image 718
Uuid Avatar asked Apr 28 '13 04:04

Uuid


Video Answer


1 Answers

You can override the console.log easily

(function(){     if(window.console && console.log){         var old = console.log;         console.log = function(){             Array.prototype.unshift.call(arguments, 'Report: ');             old.apply(this, arguments)         }     }   })();  console.log('test') 

Demo: Fiddle

like image 102
Arun P Johny Avatar answered Nov 01 '22 18:11

Arun P Johny