Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A super easy logger wrapper for making dumb js frameworks less dumb. By all means steal this. Please.
root = exports ? this
root.Logger =
class Logger
constructor: (options = {}) ->
@mapping = options.mapping ? {
error: (message) ->
console.error.call console, message
warn: (message) ->
console.warn.call console, message
info: (message) ->
console.info.call console, message
debug: (message) ->
console.debug.call console, message
}
@prefixText = options.prefixText ? null
@includePrefix = options.includePrefix ? false
@includeLevel = options.includeLevel ? true
@level = options.level ? 4
error: (message, params) ->
prefix = level = ""
prefix = "[#{@prefixText}]" if @includePrefix
level = "[ERROR]" if @includeLevel
if params
for param in params
message = "#{message}\n\t#{JSON.stringify param }"
@mapping.error "#{prefix} #{level} #{message}" if @level > 0
warn: (message, params) ->
prefix = level = ""
prefix = "[#{@prefixText}]" if @includePrefix
level = "[ERROR]" if @includeLevel
if params
for param in params
message = "#{message}\n\t#{JSON.stringify param }"
@mapping.warn "#{prefix} #{level} #{message}" if @level > 1
info: (message, params) ->
prefix = level = ""
prefix = "[#{@prefixText}]" if @includePrefix
level = "[ERROR]" if @includeLevel
if params
for param in params
message = "#{message}\n\t#{JSON.stringify param }"
@mapping.info "#{prefix} #{level} #{message}" if @level > 2
debug: (message, params) ->
prefix = level = ""
prefix = "[#{@prefixText}]" if @includePrefix
level = "[ERROR]" if @includeLevel
if params
for param in params
message = "#{message}\n\t#{JSON.stringify param }"
@mapping.debug "#{prefix} #{level} #{message}" if @level > 3
setLevel: (level) ->
@level = level
getLevel: () ->
@level
// Generated by CoffeeScript 1.6.2
(function() {
var Logger, root;
root = typeof exports !== "undefined" && exports !== null ? exports : this;
root.Logger = Logger = (function() {
function Logger(options) {
var _ref, _ref1, _ref2, _ref3, _ref4;
if (options == null) {
options = {};
}
this.mapping = (_ref = options.mapping) != null ? _ref : {
error: function(message) {
return console.error.call(console, message);
},
warn: function(message) {
return console.warn.call(console, message);
},
info: function(message) {
return console.info.call(console, message);
},
debug: function(message) {
return console.debug.call(console, message);
}
};
this.prefixText = (_ref1 = options.prefixText) != null ? _ref1 : null;
this.includePrefix = (_ref2 = options.includePrefix) != null ? _ref2 : false;
this.includeLevel = (_ref3 = options.includeLevel) != null ? _ref3 : true;
this.level = (_ref4 = options.level) != null ? _ref4 : 4;
}
Logger.prototype.error = function(message, params) {
var level, param, prefix, _i, _len;
prefix = level = "";
if (this.includePrefix) {
prefix = "[" + this.prefixText + "]";
}
if (this.includeLevel) {
level = "[ERROR]";
}
if (params) {
for (_i = 0, _len = params.length; _i < _len; _i++) {
param = params[_i];
message = "" + message + "\n\t" + (JSON.stringify(param));
}
}
if (this.level > 0) {
return this.mapping.error("" + prefix + " " + level + " " + message);
}
};
Logger.prototype.warn = function(message, params) {
var level, param, prefix, _i, _len;
prefix = level = "";
if (this.includePrefix) {
prefix = "[" + this.prefixText + "]";
}
if (this.includeLevel) {
level = "[ERROR]";
}
if (params) {
for (_i = 0, _len = params.length; _i < _len; _i++) {
param = params[_i];
message = "" + message + "\n\t" + (JSON.stringify(param));
}
}
if (this.level > 1) {
return this.mapping.warn("" + prefix + " " + level + " " + message);
}
};
Logger.prototype.info = function(message, params) {
var level, param, prefix, _i, _len;
prefix = level = "";
if (this.includePrefix) {
prefix = "[" + this.prefixText + "]";
}
if (this.includeLevel) {
level = "[ERROR]";
}
if (params) {
for (_i = 0, _len = params.length; _i < _len; _i++) {
param = params[_i];
message = "" + message + "\n\t" + (JSON.stringify(param));
}
}
if (this.level > 2) {
return this.mapping.info("" + prefix + " " + level + " " + message);
}
};
Logger.prototype.debug = function(message, params) {
var level, param, prefix, _i, _len;
prefix = level = "";
if (this.includePrefix) {
prefix = "[" + this.prefixText + "]";
}
if (this.includeLevel) {
level = "[ERROR]";
}
if (params) {
for (_i = 0, _len = params.length; _i < _len; _i++) {
param = params[_i];
message = "" + message + "\n\t" + (JSON.stringify(param));
}
}
if (this.level > 3) {
return this.mapping.debug("" + prefix + " " + level + " " + message);
}
};
Logger.prototype.setLevel = function(level) {
return this.level = level;
};
Logger.prototype.getLevel = function() {
return this.level;
};
return Logger;
})();
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment