Skip to content

Instantly share code, notes, and snippets.

@tanaka9230
Last active December 30, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanaka9230/218079ed12e0ed5a9a70 to your computer and use it in GitHub Desktop.
Save tanaka9230/218079ed12e0ed5a9a70 to your computer and use it in GitHub Desktop.
"logger.js"; to show the structure of the JavaScript module
<!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>
/*
* 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)
});
@tanaka9230
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment