1

I have a very large app that logs a lot of stuff happening in it.

When there's a client error, I want to send a history of X logs to my db.

To do that I first thought of something like that:

var yo = {
  log: function() {
    //some logic to save arguments to a queue
    console.log(arguments);
  },
  error: function() {
    //some logic to save arguments to a queue
    //some logic to save queue to db
    console.error(arguments);
  }
}

and then use

yo.log()
instead of
console.log()

The problem with this method is that all errors and logs (in chrome's console) debug link directs to the yo function definition instead of the actual error in the code.

Does anyone know has a better solution to this problem?

Here is a simple example: http://codepen.io/guysopher/pen/xZKMgG

var yo = {
  log: function() {
    console.log.apply(console, arguments);
  }
}

console.log('This shows the right line number!');

yo.log('This shows the wrong line number!');
Guy Sopher
  • 4,402
  • 4
  • 22
  • 36
  • 1
    Try this to get the trace http://stackoverflow.com/a/635852/942223 – tiriana Dec 03 '15 at 10:04
  • This is a good way to get a stack trace after the error has occurred. But I'm looking for a way to wrap the log function without ruining the line numbers. – Guy Sopher Dec 03 '15 at 10:33

1 Answers1

0

This could probably help you. Capturing javascript console.log?

However you need to keep in mind that you overwritten the console.log function.

Community
  • 1
  • 1
  • Yes, I know this solution but it displays the wrong line number. I need something that will keep the console functionality as is. – Guy Sopher Dec 03 '15 at 10:20
  • Would combining it with the comment from tiriana work for you. – Hendrik Mittrop Dec 03 '15 at 10:27
  • The thing is I'm not looking for a stack trace. I want to see the logs I wrote before the error (i.e. what the users did during the session that led to this error) – Guy Sopher Dec 03 '15 at 10:32
  • You could extract the File from the Stacktrace. ` (function() { var oldLog = console.log; console.log = function (message) { var error = new Error(); var stackArr = error.stack.split('\n'); var fileRef = stackArr[stackArr.length - 2]; oldLog.apply(console, arguments); }; })();` – Hendrik Mittrop Dec 03 '15 at 11:34