Javascript Logging

This is a noob question.

What if I want to add logging to the java script application, which is running in a browser (IE, FF, etc.) ? As I understand I can not save log files in the client host. So, I have only two options: display my logging information in a new browser window (like "blackbird") or send the logging to the server.

Is it correct? What kind of logging do they usually use?

Answers


You can't "store" log files on client host. You can open a window and visualize it, but you (assuming you are running the Web Application) will never see it.

If you absolutely must get client side logs, you need to send them back to the server using AJAX. Here's a blog post I really liked about it.


Another possibility is the jsnlog library http://js.jsnlog.com/ It will let you send client side logs to the server.


Take a look at https://log4sure.com (disclosure: I created it) - but it is really useful, check it out and decide for yourself. It allows you to log errors/event and also lets you create your custom log table. It stores everything on its own server so you don't have to. It also allows you to monitor your logs real-time. And the best part, its free.

You can also use bower to install it, use bower install log4sure

The set up code is really easy too:

// setup
var _logServer;

(function() {
  var ls = document.createElement('script');
  ls.type = 'text/javascript';
  ls.async = true;
  ls.src = 'https://log4sure.com/ScriptsExt/log4sure.min.js';
  var s = document.getElementsByTagName('script')[0];
  s.parentNode.insertBefore(ls, s);
  ls.onload = function() {
    // use your token here.
    _logServer = new LogServer("use-your-token-here");
  };
})();

//  example for logging text
_logServer.logText("your log message goes here.")

//example for logging error 
divide = function(numerator, divisor) {
    try {
      if (parseFloat(value) && parseFloat(divisor)) {
        throw new TypeError("Invalid input", "myfile.js", 12, {
          value: value,
          divisor: divisor
        });
      } else {
        if (divisor == 0) {
          throw new RangeError("Divide by 0", "myfile.js", 15, {
            value: value,
            divisor: divisor
          });
        }
      }
    } catch (e) {
      _logServer.logError(e.name, e.message, e.stack);

    }
  }
  // another use of logError in window.onerror
  // must be careful with window.onerror as you might be overwriting some one else's window.onerror functionality
  // also someone else can overwrite window.onerror.
window.onerror = function(msg, url, line, column, err) {
  // may want to check if url belongs to your javascript file
  var data = {
    url: url,
    line: line,
    column: column,

  }
  _logServer.logError(err.name, err.message, err.stack, data);

};

// example for custom logs
var foo = "some variable value";
var bar = "another variable value";
var flag = "false";
var temp = "yet another variable value";

_logServer.log(foo, bar, flag, temp);

Need Your Help

java - why does my code fill up all values

java arrays

This is the method in my program which deals an 'initial hand' to players in the game, the players array is an array which contains Player objects. The Player objects themselves are arrays or the C...