Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added log class that I sometimes use.

  • Loading branch information...
commit 70de8ffb76e851426da4ab673240a2445e779707 1 parent 66e3bb8
@jnunemaker authored
Showing with 41 additions and 0 deletions.
  1. +41 −0 log.js
View
41 log.js
@@ -0,0 +1,41 @@
+/**
+ * Logging class which ignores logging unless we set a level to output
+ */
+var Log = {
+ levels: $A(['off', 'info', 'debug', 'error']),
+ _level: 'off',
+ _levelNotFoundValue: -1,
+
+ // writes to the log if the level is appropriate and we have our Firebug console
+ write: function(level, msg) {
+ if (Log.getLevel() == 'off') return;
+ var cur_level = Log.getLevelIndex(Log.getLevel());
+ var req_level = Log.getLevelIndex(level);
+ // only log if level exists and it is greater than or equal the the current level
+ if (req_level != Log._levelNotFoundValue && cur_level <= req_level) {
+ try {
+ console.log(level + ': ' + msg);
+ } catch(e) {
+ // fail silently
+ }
+ }
+ },
+
+ // creates functions to write to the log at the various levels
+ info: function(msg) { Log.write('info', msg); },
+ debug: function(msg) { Log.write('debug', msg); },
+ error: function(msg) { Log.write('error', msg); },
+
+ // gets the levels array index of a level
+ getLevelIndex: function(level) { return (Log.levels.include(level) ? Log.levels.indexOf(level) : Log._levelNotFoundValue); },
+
+ // returns the current level string value
+ getLevel: function() { return Log._level; },
+
+ // sets the level of logging to output
+ level: function(level) {
+ Log._level = Log.levels.include(level) ? level : null;
+ if (Log._level == null) { throw('Log level does not exist'); }
+ }
+}
+Log.level('info');
Please sign in to comment.
Something went wrong with that request. Please try again.