JavaScript Debug: A simple wrapper for console.log

Version: 0.4, Last Updated: 6/22/2010

Tested with Internet Explorer 6-8, Firefox 3-3.6, Safari 3-4, Chrome 3-5, Opera 9.6-10.5

Homehttp://benalman.com/projects/javascript-debug-console-log/
GitHubhttp://github.com/cowboy/javascript-debug/
Sourcehttp://github.com/cowboy/javascript-debug/raw/master/ba-debug.js
(Minified)http://github.com/cowboy/javascript-debug/raw/master/ba-debug.min.js (1.1kb)
Summary
JavaScript Debug: A simple wrapper for console.logVersion: 0.4, Last Updated: 6/22/2010
LicenseCopyright © 2010 “Cowboy” Ben Alman, Dual licensed under the MIT and GPL licenses.
Support and TestingInformation about what browsers this code has been tested in.
ExamplesThese working examples, complete with fully commented code, illustrate a few ways in which this plugin can be used.
Revision History
Pass-through console methodsassert, clear, count, dir, dirxml, exception, group, groupCollapsed, groupEnd, profile, profileEnd, table, time, timeEnd, trace
Functions
debug.logCall the console.log method if available.
debug.debugCall the console.debug method if available, otherwise call console.log.
debug.infoCall the console.info method if available, otherwise call console.log.
debug.warnCall the console.warn method if available, otherwise call console.log.
debug.errorCall the console.error method if available, otherwise call console.log.
debug.setLevelSet a minimum or maximum logging level for the console.
debug.setCallbackSet a callback to be used if logging isn’t possible due to console.log not existing.

License

Copyright © 2010 “Cowboy” Ben Alman, Dual licensed under the MIT and GPL licenses.  http://benalman.com/about/license/

Support and Testing

Information about what browsers this code has been tested in.

Browsers TestedInternet Explorer 6-8, Firefox 3-3.6, Safari 3-4, Chrome 3-5, Opera 9.6-10.5

Examples

These working examples, complete with fully commented code, illustrate a few ways in which this plugin can be used.

Exampleshttp://benalman.com/code/projects/javascript-debug/examples/debug/

Revision History

0.4(6/22/2010) Added missing passthrough methods: exception, groupCollapsed, table
0.3(6/8/2009) Initial release

Pass-through console methods

assert, clear, count, dir, dirxml, exception, group, groupCollapsed, groupEnd, profile, profileEnd, table, time, timeEnd, trace

These console methods are passed through (but only if both the console and the method exists), so use them without fear of reprisal.  Note that these methods will not be passed through if the logging level is set to 0 via debug.setLevel.

Functions

debug.log

Call the console.log method if available.  Adds an entry into the logs array for a callback specified via debug.setCallback.

Usage

debug.log( object [, object, ...]  );-

Arguments

object(Object) Any valid JavaScript object.

debug.debug

Call the console.debug method if available, otherwise call console.log.  Adds an entry into the logs array for a callback specified via debug.setCallback.

Usage

debug.debug( object [, object, ...]  );-

Arguments

object(Object) Any valid JavaScript object.

debug.info

Call the console.info method if available, otherwise call console.log.  Adds an entry into the logs array for a callback specified via debug.setCallback.

Usage

debug.info( object [, object, ...]  );-

Arguments

object(Object) Any valid JavaScript object.

debug.warn

Call the console.warn method if available, otherwise call console.log.  Adds an entry into the logs array for a callback specified via debug.setCallback.

Usage

debug.warn( object [, object, ...]  );-

Arguments

object(Object) Any valid JavaScript object.

debug.error

Call the console.error method if available, otherwise call console.log.  Adds an entry into the logs array for a callback specified via debug.setCallback.

Usage

debug.error( object [, object, ...]  );-

Arguments

object(Object) Any valid JavaScript object.

debug.setLevel

Set a minimum or maximum logging level for the console.  Doesn’t affect the debug.setCallback callback function, but if set to 0 to disable logging, Pass-through console methods will be disabled as well.

Usage

debug.setLevel( [ level ] )-

Arguments

level(Number) If 0, disables logging.  If negative, shows N lowest priority levels of log messages.  If positive, shows N highest priority levels of log messages.

Priority levels

log (1) < debug (2) < info (3) < warn (4) < error (5)

debug.setCallback

Set a callback to be used if logging isn’t possible due to console.log not existing.  If unlogged logs exist when callback is set, they will all be logged immediately unless a limit is specified.

Usage

debug.setCallback( callback [, force ] [, limit ] )

Arguments

callback(Function) The aforementioned callback function.  The first argument is the logging level, and all subsequent arguments are those passed to the initial debug logging method.
force(Boolean) If false, log to console.log if available, otherwise callback.  If true, log to both console.log and callback.
limit(Number) If specified, number of lines to limit initial scrollback to.
Set a minimum or maximum logging level for the console.
Set a callback to be used if logging isn’t possible due to console.log not existing.
assert, clear, count, dir, dirxml, exception, group, groupCollapsed, groupEnd, profile, profileEnd, table, time, timeEnd, trace
Close