Enhanced logging system factored in a new lib: logger.js.

This commit is contained in:
Aurélien Mino 2015-01-18 14:00:58 +01:00
parent 7716463be2
commit 59e0f8b244
2 changed files with 59 additions and 16 deletions

View file

@ -1,7 +1,7 @@
// ==UserScript==
// @name Import Discogs releases to MusicBrainz
// @version 2015.01.18.0
// @version 2015.01.18.1
// @namespace http://userscripts.org/users/22504
// @icon http://www.discogs.com/images/discogs130.png
// @downloadURL https://raw.github.com/murdos/musicbrainz-userscripts/master/discogs_importer.user.js
@ -12,11 +12,11 @@
// @exclude http://www.discogs.com/release/add
// @require https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js
// @require https://raw.github.com/murdos/musicbrainz-userscripts/master/lib/import_functions.js
// @require https://raw.github.com/murdos/musicbrainz-userscripts/master/lib/logger.js
// ==/UserScript==
////////////////////////////////////////////////////////////////////////////////////////////////////////
var unsafeWindow = unsafeWindow || window;
/*
* Test cases:
* - http://www.discogs.com/release/1566223 : Artist credit of tracks contains an ending ',' join phrase
@ -54,13 +54,13 @@ $(document).ready(function(){
dataType: 'json',
crossDomain: true,
success: function (data, textStatus, jqXHR) {
//mylog(data);
LOGGER.debug("Discogs JSON Data from API:", data);
var release = parseDiscogsRelease(data);
insertLink(release);
},
error: function(jqXHR, textStatus, errorThrown) {
mylog("AJAX Status:" + textStatus);
mylog("AJAX error thrown:" + errorThrown);
LOGGER.error("AJAX Status: ", textStatus);
LOGGER.error("AJAX error thrown: ", errorThrown);
}
});
@ -438,7 +438,7 @@ function parseDiscogsRelease(data) {
});
mylog(release);
LOGGER.info("Parsed release: ", release);
return release;
}
@ -452,16 +452,6 @@ function decodeDiscogsJoinphrase(join) {
return joinphrase;
}
function mylog(obj) {
var DEBUG = true;
if (DEBUG) {
var window = unsafeWindow || window;
if (window.console) {
window.console.log(obj);
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Discogs -> MusicBrainz mapping //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

53
lib/logger.js Normal file
View file

@ -0,0 +1,53 @@
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Logger
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var LOGGER = (function() {
var LOG_LEVEL = 'info';
function fnDebug() {
if (LOG_LEVEL == 'debug') {
_log("DEBUG", arguments);
}
}
function fnInfo() {
if (LOG_LEVEL == 'debug' || LOG_LEVEL === 'info') {
_log("INFO", arguments);
}
}
function fnError() {
_log("ERROR", arguments);
}
function fnSetLevel(level) {
LOG_LEVEL = level;
}
// --------------------------------------- privates ----------------------------------------- //
function _log(level, args) {
// Prepends log level to things that will be logged
args = Array.prototype.slice.call(args);
args.unshift('['+level+']');
// Determine if there's a logger (console.log) available
var win = unsafeWindow || window;
var console = win.console;
if (console && console.log && console.log.apply) {
try {
console.log.apply(this, args);
} catch(e) {}
}
};
// ---------------------------------- expose publics here ------------------------------------ //
return {
debug: fnDebug,
info: fnInfo,
error: fnError,
setLevel: fnSetLevel
};
})();