Last active
December 30, 2015 14:08
-
-
Save tanaka9230/218079ed12e0ed5a9a70 to your computer and use it in GitHub Desktop.
"logger.js"; to show the structure of the JavaScript module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | |
<meta http-equiv="Pragma" content="no-cache"> | |
<meta http-equiv="Cache-Control" content="no-cache"> | |
<meta http-equiv="Expires" content="0"> | |
<title>Logger Module Experiment</title> | |
</head> | |
<body> | |
<h1>Logger Module Experiment</h1> | |
<p> | |
See the output on the web console. | |
</p> | |
<!-- SCRIPTS --> | |
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script> | |
<script src="http://underscorejs.org/underscore-min.js"></script> | |
<script src="js/logger.js"></script> | |
<script> | |
Logger.log_level = Logger.LOG_LEVEL.DEBUG; | |
Logger.alert = true; | |
Logger.debug_dump("a string", "hoge"); | |
Logger.debug_dump("an integer", 123); | |
Logger.debug_dump("a boolean", true); | |
Logger.debug_dump("an object", {x:1,y:"foo",z:{a:2,b:false}}); | |
Logger.debug_dump("an object", new Date()); | |
Logger.debug_trace("run through here"); | |
Logger.debug_begins("my procedure"); | |
Logger.debug_completed("my procedure"); | |
Logger.debug_succeeded("my procedure"); | |
Logger.debug_failed("my procedure"); | |
try { | |
xxxxx; | |
} catch (exp) { | |
Logger.error_abend(exp); | |
} | |
</script> | |
<!-- /SCRIPTS --> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* LOGGER | |
*/ | |
; (function(module_def) { | |
var MODULE_NAME = "Logger"; | |
try { | |
console.log(">> loading '" + MODULE_NAME + "' begins..."); | |
var export_target = this; | |
export_target[MODULE_NAME] = module_def(); | |
// TODO more precise exporting procedure | |
console.log("<< loading '" + MODULE_NAME + "' succeeded. (^_^)"); | |
} catch (exp) { | |
console.error("<< loading '" + MODULE_NAME + "' FAILED! (T_T) : " + exp); | |
console.dir(exp); | |
} | |
})(function() { | |
"use strict"; | |
/////////////////////////////////////////////////////////////////////////////// | |
//// DECLARATIONS | |
var Logger = { | |
LOG_LEVEL : { | |
DEBUG : 4, | |
INFO : 3, | |
ERROR : 1, | |
NEVER : 0 | |
}, | |
log_level : 0, | |
alert : false, | |
debug_dump : function(arg_name, arg) { | |
if (this.log_level >= this.LOG_LEVEL.DEBUG) { | |
debug_dump(arg_name, arg); | |
} | |
}, | |
debug_trace : function(msg) { | |
if (this.log_level >= this.LOG_LEVEL.DEBUG) { | |
debug_trace(msg); | |
} | |
}, | |
debug_begins : function(arg) { | |
this.debug_trace(">> `" + arg + "` begins..."); | |
}, | |
debug_completed : function(arg) { | |
this.debug_trace("<< `" + arg + "` completed. ('_')"); | |
}, | |
debug_succeeded : function(arg) { | |
this.debug_trace("<< `" + arg + "` succeeded. (^_^)"); | |
}, | |
debug_failed : function(arg) { | |
this.debug_trace("<< `" + arg + "` FAILED! (;_;)"); | |
}, | |
error_abend : function(exp) { | |
if (this.log_level >= this.LOG_LEVEL.ERROR) { | |
error(exp); | |
} | |
throw "OOPS! APPLICATION ABENDED"; | |
} | |
}; | |
return Logger; | |
/////////////////////////////////////////////////////////////////////////////// | |
//// PRIVATE | |
function debug_dump(arg_name, arg) { | |
if (_.isObject(arg)) { | |
console.log(format_log("DEBUG", arg_name + " = (shown in below)")); | |
console.dir(arg); | |
} else if (_.isString(arg)) { | |
console.log(format_log("DEBUG", arg_name + " = \"" + arg + "\"")); | |
} else { | |
console.log(format_log("DEBUG", arg_name + " = " + arg)); | |
} | |
} | |
function debug_trace(msg) { | |
console.log(format_log("DEBUG", msg || "")); | |
} | |
function error(exp) { | |
console.error(format_log("ERROR", exp)); | |
if (Logger.alert) { | |
alert("[ERROR] " + exp); | |
} | |
} | |
function format_log(log_level, msg) { | |
var tms = new Date().toString(); | |
return "[" + tms + "][" + log_level + "] : " + msg; | |
} | |
/////////////////////////////////////////////////////////////////////////////// | |
//// (FIN) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
logger.js
"logger.js" is made to show the template of the JavaScript module's structure.
"logger.js" works good as a module providing trivial logging feature. tested on only Firefox and Chrome.