Skip to content

Instantly share code, notes, and snippets.

@shivam-tripathi
Created March 16, 2018 10:54
Show Gist options
  • Save shivam-tripathi/a6d9f5292ebbdcce4e11dfc481cb69e8 to your computer and use it in GitHub Desktop.
Save shivam-tripathi/a6d9f5292ebbdcce4e11dfc481cb69e8 to your computer and use it in GitHub Desktop.
// ==UserScript==
'use strict';
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/*
* Logger class
* - debug
* - info
* - error
* - warn
* - setLevel
*/
var Log = function () {
function Log() {
var level = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'INFO';
_classCallCheck(this, Log);
this.logLevel = level;
}
_createClass(Log, [{
key: 'log',
value: function log(args) {
args = Array.prototype.slice.call(args);
args.unshift('[' + this.logLevel + '] ');
var console = window.console;
if (console && console.log && console.log.apply) {
try {
console.log.apply(this, args);
} catch (e) {} }
}
}, {
key: 'distinctLog',
value: function distinctLog(level, args) {
var prevLevel = this.logLevel;
this.logLevel = level;
this.log(args);
this.logLevel = prevLevel;
}
}, {
key: 'debug',
value: function debug(args) {
if (this.logLevel === 'DEBUG') {
this.log(args);
}
}
}, {
key: 'info',
value: function info(args) {
if (this.logLevel === 'INFO') {
this.log("INFO", arguments);
}
}
}, {
key: 'error',
value: function error(args) {
this.distinctLog('ERROR', args);
}
}, {
key: 'warn',
value: function warn(args) {
this.distinctLog('WARN', args);
}
}, {
key: 'setLevel',
value: function setLevel(level) {
this.logLevel = level;
}
}]);
return Log;
}();
var log = new Log();
log.warn('Warn');
log.debug('Debug');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment