Skip to content

Instantly share code, notes, and snippets.

@czottmann
Created April 14, 2010 11:33
Show Gist options
  • Save czottmann/365705 to your computer and use it in GitHub Desktop.
Save czottmann/365705 to your computer and use it in GitHub Desktop.
/*jslint browser: true, devel: true, onevar: true, undef: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, newcap: true, immed: true */
/*global jQuery: false */
"use strict";
/**
* @module jQuery
*/
/**
* A quick and dirty jQuery logger; working with either a div container with
* the ID "jquery-log-console" or the browser's <tt>console</tt> object.
* The div takes precedence; if neither of both exists, the log calls will be
* silently ignored.
*
* @class log
* @extends jQuery
* @static
* @author Carlo Zottmann, carlo@municode.de, http://municode.de/
*/
jQuery.log = (function() {
function print( level, msg, o ) {
var div = jQuery("#jquery-log-console"),
outputToDiv = ( div.size() > 0 ),
hasConsole = ( typeof console !== "undefined" ),
date = new Date(),
time = date.toLocaleTimeString() + "." + date.getMilliseconds();
if ( !hasConsole && !outputToDiv ) {
return;
}
if ( typeof o !== "undefined" ) {
if (outputToDiv) {
div.append( [
"<div class='jquery-logger-console-", level, "'>",
"[", time, "] ",
msg, " ", o,
"</div>"
].join("") );
}
else {
console[ level ]( "[%s] %s %o", time, msg, o );
}
}
else {
if (outputToDiv) {
div.append( [
"<div class='jquery-logger-console-", level, "'>",
"[", time, "] ",
msg,
"</div>"
].join("") );
}
else {
console[ level ]( "[%s] %s", time, msg );
}
}
}
return {
/**
* Logs an error message.
* @param msg {String} A string to log.
* @param o {Object} An object to log. Optional.
*/
error: function( msg, o ) {
print( "error", msg, o );
},
/**
* Logs a warning message.
* @param msg {String} A string to log.
* @param o {Object} An object to log. Optional.
*/
warning: function( msg, o ) {
print( "warn", msg, o );
},
/**
* Logs a debug message.
* @param msg {String} A string to log.
* @param o {Object} An object to log. Optional.
*/
debug: function( msg, o ) {
print( "debug", msg, o );
},
/**
* Logs an info message.
* @param msg {String} A string to log.
* @param o {Object} An object to log. Optional.
*/
info: function( msg, o ) {
print( "info", msg, o );
}
};
}());
@SueFolkerts
Copy link

How do I see the output from this? I have a service that I wish to create a log for.

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