English | 简体中文
$ npm install vconsole
import VConsole from 'vconsole';
const vConsole = new VConsole();
// or init with options
const vConsole = new VConsole({ theme: 'dark' });
// call `console` methods as usual
console.log('Hello world');
// remove it when you finish debugging
vConsole.destroy();
Notice that
VConsole
is the prototype of vConsole.
So vConsole panel will not be inserted into your page until younew
it manually.
Otherwise, you can use CDN to import vConsole:
<script src="https://unpkg.com/vconsole@latest/dist/vconsole.min.js"></script>
<script>
// VConsole will be exported to `window.VConsole` by default.
var vConsole = new window.VConsole();
</script>
Available CDN:
- https://unpkg.com/vconsole@latest/dist/vconsole.min.js
- https://cdn.jsdelivr.net/npm/vconsole@latest/dist/vconsole.min.js
After imported, vConsole should be inited manually:
var vConsole = new VConsole(option);
option
is an optional object.
See Public Properties & Methods for definition.
Use setOption()
to update option
:
// set single key only
vConsole.setOption('log.maxLogNumber', 5000);
// overwrite 'log' object
vConsole.setOption({ log: { maxLogNumber: 5000 } });
Use the methods of console
to print logs, just like what you do at desktop browsers:
console.log('Hello World');
When vConsole is not loaded, logs will be printed to browser console. After importing vConsole, logs will be printed to both vConsole panel and browser console.
If you want to print logs to vConsole panel only, try Log plugin methods:
vConsole.log.log('Hello world');
5 types of log methods are supported, with different styles:
console.log('foo'); // black word, white bakcground
console.info('bar'); // purple word, white background
console.debug('oh'); // orange word, white background
console.warn('foo'); // orange word, yellow background
console.error('bar'); // red word, pink background
Supported console
methods:
console.clear(); // Clear all logs
console.time('foo'); // start a timer named "foo"
console.timeEnd('foo'); // stop "foo" timer and print the elapsed time
Use %c
to add style to logs:
console.log('%c blue %c red', 'color:blue', 'color:red'); // blue red
console.log('%c FOO', 'font-weight:bold', 'bar'); // FOO bar
console.log('%c Foo %c bar', 'color:red'); // Foo %c bar
Note that only the first parameter support
%c
format, and the%c
must be followed by a space. The following parameter(s) will be used as HTML style to fill%c
, and the remain%c
or parameters will be shown as normal string.
Use %s, %d, %o
to output a log with formatting. The pattern must be followed by a space.
%s
: Output as a string. Non-string objects will be converted into strings.%d
: Output as a number.%o
: Output as an object. You can clickthe object name to open more information about it.
console.log('Hi %s, Im %s', 'Foo', 'Bar'); // Hi Foo, Im Bar
console.log('I had %d cakes', 3); // I had 3 cakes
console.log('The %o is large', obj); // The [[obj]] is large
Use [system]
as the first parameter to output logs to System panel:
console.log('[system]', 'foo'); // 'foo' will be printed to System panel
console.log('[system] bar'); // this log will show in Log tab instead of System panel
All XMLHttpRequest | fetch | sendBeacon
requests will be logged in Network panel by default.
To prevent logging, add _noVConsole = true
to XHR object:
var xhr = new XMLHttpRequest();
xhr._noVConsole = true; // now this request would not be logged in panel
xhr.open('GET', 'http://example.com/');
xhr.send();
If you want to print custom request logs, try Network plugin methods:
vConsole.network.add(...);