English | 简体中文

Tutorial

Installation

1. Download

Download the latest release of vConsole.

Or, install via npm :

  1. npm install vconsole

Then save dist/vconsole.min.js to your project.

2. Import

(1) Under non-AMD/CMD rule, insert vConsole into <head>. To support further features, insert vConsole into <head> rather than </body> is a better choice.

  1. <head>
  2. <script src="path/to/vconsole.min.js"></script>
  3. <script>
  4. var vConsole = new VConsole();
  5. </script>
  6. </head>

(2) Under AMD/CMD rule, use require() to import vConsole.

  1. var VConsole = require('path/to/vconsole.min.js');
  2. var vConsole = new VConsole();

Notice that VConsole is the prototype of vConsole. So vConsole panel will not be inserted into your page until you new it manually.

Usage

Initialization & Configuaration

After imported, vConsole should be inited manually:

  1. var vConsole = new VConsole(option);

option is an optional object.

See Public Properties & Methods for definition.

Use setOption() to update option:

  1. vConsole.setOption('maxLogNumber', 5000);
  2. // or:
  3. vConsole.setOption({maxLogNumber: 5000});

Print logs

Use the methods of console to print logs, just like what you do at desktop browsers:

  1. console.log('Hello World');

When vConsole is not loaded, logs will be printed to native console. After importing vConsole, logs will be printed to both front-end console and native console.

Styles

5 types of log method are supported, with different styles:

  1. console.log('foo'); // black word, white bakcground
  2. console.info('bar'); // purple word, white background
  3. console.debug('oh'); // orange word, white background
  4. console.warn('foo'); // orange word, yellow background
  5. console.error('bar'); // red word, pink background

Other methods

Supported console methods:

  1. console.time('foo'); // start a timer named "foo"
  2. console.timeEnd('foo'); // stop "foo" timer and print the elapsed time

Formatted object / array

Object or Array variable will be printed as formatted JSON:

  1. var obj = {};
  2. obj.foo = 'bar';
  3. console.log(obj);
  4. /*
  5. Object
  6. {
  7. foo: "bar"
  8. }
  9. */

Polymorphic

Multiple arguments are supported, each variable will be divided by a space:

  1. var uid = 233;
  2. console.log('UserID:', uid); // UserID: 233

Special format

Use [system] as the first parameter to print logs to System tab:

  1. console.log('[system]', 'foo'); // 'foo' will be printed to System tab
  2. console.log('[system] bar'); // this log will show in Log tab instead of System tab

Built-in Plugins

Network

All XMLHttpRequest requests will be displayed in Network tab by default.

To prevent the display, add _noVConsole = true to XHR object:

  1. var xhr = new XMLHttpRequest();
  2. xhr._noVConsole = true; // now this request would not be displayed in tab
  3. xhr.open("GET", 'http://example.com/');
  4. xhr.send();

Goto: Documentation Index