How can I hijack calls to the browsers console.log
and send them to my own log function while still letting them make there normal console.log
entry.
Sites like JSBin and CodePen allow you to view there version of a console log in the page DOM while still making the console.log entry as well.
So every time I call console.log('my log msg');
it should behave like normal but also pipe/send the console log data to my own JavaScript logger.log('log entry')
function.
How can this be done?
UPDATE
Desired custom logger output of an object:
Logging this object:
var data = {
date: new Date(),
prop2: 'sfgsdgsd',
arrayProp: ['key1', 'key2', 'key3'],
arrayOfObjectsProp: [
{
date: new Date(),
date2: new Date()
},{
date: new Date(),
date2: new Date()
},{
date: new Date(),
date2: new Date()
}]
};
setInterval(function() {
console.log(data);
}, 10000);
In the Logger will show as:
{
"date": "2017-01-01T03:19:46.944Z",
"prop2": "sfgsdgsd",
"arrayProp": [
"key1",
"key2",
"key3"
],
"arrayOfObjectsProp": [
{
"date": "2017-01-01T03:19:46.944Z",
"date2": "2017-01-01T03:19:46.944Z"
},
{
"date": "2017-01-01T03:19:46.944Z",
"date2": "2017-01-01T03:19:46.944Z"
},
{
"date": "2017-01-01T03:19:46.944Z",
"date2": "2017-01-01T03:19:46.944Z"
}
]
}