Console

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

When writing output to a terminal, JS.Console also supports many ANSI 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:

In addition, the following formatting commands are available for formatting output in environments that support ANSI escape codes for formatting text. All commands are methods of JS.Console, for example you can switch to bold text by calling JS.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:

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

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

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

Logger = new JS.Class({
    include: JS.Console,

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