Console

Most JavaScript environments have some sort of console that allows running programs to print output, log messages and the like. In web browsers, this capability is provided by the console object, and in server-side environments the program may write output to the terminal. Console provides a single interface to all these facilities, using the most appropriate device wherever it is being used.

// In the browser
JS.require('JS.Console', function(Console) { ... });

// In CommonJS
var Console = require('jsclass/src/console').Console;

When writing output to a terminal, or the WebKit developer console, Console also supports many formatting commands for changing the color and other attributes of printed text.

The main two output methods are as follows:

The output devices used are as follows:

Formatting

The following formatting commands are available for formatting output. Not all environments support formatting, and on those environments these commands will be ignored. All commands are methods of Console, for example you can switch to bold text by calling Console.bold(). Text printed after the command has been issued will have the formatting applied to it. In environments that do not support formatting these methods have no effect.

Multiple formatting instructions can be combined using this method:

Console.consoleFormat('bold', 'red');

The consoleFormat() method takes a list of formatting instructions as input. It calls reset() and then applies the given formats.

On some platforms, Console also supports a set of commands for moving the cursor and clearing parts of the screen, for creating interfaces in the terminal. Those commands are:

Console as a mixin

Finally note that Console can be used as a mixin so all the above methods appear in the current class:

Logger = new Class({
    include: Console,

    info: function(message) {
        this.bgblack();
        this.white();
        this.print('INFO');
        this.reset();
        this.puts(' ' + message);
    }
});