YUI 3.x Home -

Site Search (YDN & YUIBlog):

# YUI Library Examples: Plugin.ConsoleFilters: Using the ConsoleFilters plugin

## Plugin.ConsoleFilters: Using the ConsoleFilters plugin

This example illustrates how to use and configure the ConsoleFilters plugin for Console. The debug versions of YUI module files are used, so standard YUI debugging messages are broadcast to the Console.

Use the checkboxes in the Console footer to control which messages are displayed or hidden. Click the "Log a message" button to call `Y.log` using a custom category and source.

Note how new filter checkboxes are added when a new category or source are received by the Console, for example when clicking on the "Log a message" button.

View example in new window.

### Configure the YUI instance for debug mode

Only the `module-debug.js` versions of module files include log statements, so we'll set up our YUI instance's `filter` property to "debug". We'll also reduce the noise somewhat by specifying a `logInclude` list.

    YUI({     base: '../../build/',    filter: 'debug',    logInclude: {        event: true,        attribute: true,        base: true,        widget: true,        node: true,        MyApp: true  // This must be included for the custom source message    },    timeout: 10000}).use("console-filters", function (Y) { ... });

    YUI({
        base: '../../build/',
        filter: 'debug',
        logInclude: {
            event: true,
            attribute: true,
            base: true,
            widget: true,
            node: true,
            MyApp: true  // This must be included for the custom source message
        },
        timeout: 10000
    }).use("console-filters", function (Y) { ... });

YUI({ base: '../../build/', filter: 'debug', logInclude: { event: true, attribute: true, base: true, widget: true, node: true, MyApp: true // This must be included for the custom source message }, timeout: 10000 }).use("console-filters", function (Y) { ... });

### Create the Console and plug in ConsoleFilters

All that's left to do now is plugin in the ConsoleFilters plugin. This can be done in one of a few ways.

    // create the console instancevar yconsole = new Y.Console({    boundingBox: '#console',    height: '400px',    width: '450px',    newestOnTop: false,    plugins: [ Y.Plugin.ConsoleFilters ]}).render(); // unknown categories and sources are allowed.yconsole.filter.hideCategory('error'); // hide and show methods support N arguments.yconsole.filter.hideSource('attribute','widget');

    // create the console instance
    var yconsole = new Y.Console({
        boundingBox: '#console',
        height: '400px',
        width: '450px',
        newestOnTop: false,
        plugins: [ Y.Plugin.ConsoleFilters ]
    }).render();

    // unknown categories and sources are allowed.
    yconsole.filter.hideCategory('error');

    // hide and show methods support N arguments.
    yconsole.filter.hideSource('attribute','widget');

// create the console instance var yconsole = new Y.Console({ boundingBox: '#console', height: '400px', width: '450px', newestOnTop: false, plugins: [ Y.Plugin.ConsoleFilters ] }).render(); // unknown categories and sources are allowed. yconsole.filter.hideCategory('error'); // hide and show methods support N arguments. yconsole.filter.hideSource('attribute','widget');

Alternatively, you can attach the ConsoleFilters plugin after instantiation. This version also shows how to apply initial category and source filter states.

    var yconsole = new Y.Console({    boundingBox: '#console',    height: '400px',    width: '450px',    newestOnTop: false}).plug(Y.Plugin.ConsoleFilters, {    category: {        error: false    },    source: {        attribute: false,        widget: false    }}).render();</script>

    var yconsole = new Y.Console({
        boundingBox: '#console',
        height: '400px',
        width: '450px',
        newestOnTop: false
    }).plug(Y.Plugin.ConsoleFilters, {
        category: {
            error: false
        },
        source: {
            attribute: false,
            widget: false
        }
    }).render();
    </script>

var yconsole = new Y.Console({ boundingBox: '#console', height: '400px', width: '450px', newestOnTop: false }).plug(Y.Plugin.ConsoleFilters, { category: { error: false }, source: { attribute: false, widget: false } }).render(); </script>

### Full Code Listing

#### Markup

    <div id="demo">    <div id="yconsole"></div>    <button id="log" type="button">Log a message</button>    <button id="toggle_info" type="button">Hide info messages</button></div>

    <div id="demo">
        <div id="yconsole"></div>
        <button id="log" type="button">Log a message</button>
        <button id="toggle_info" type="button">Hide info messages</button>
    </div>

<div id="demo"> <div id="yconsole"></div> <button id="log" type="button">Log a message</button> <button id="toggle_info" type="button">Hide info messages</button> </div>

#### JavaScript

    // Create a YUI instance and request the console module and its dependenciesYUI({base:"../../build/", timeout: 10000, filter:"debug", logInclude: {event:true, attribute:true, node:true, base:true, widget:true, MyApp:true}}).use("console", "console-filters", function (Y) { // create the console instancevar yconsole = new Y.Console({    boundingBox: '#yconsole',    height: '400px',    width: '450px',    newestOnTop: false,    style: 'block',    plugins: [ Y.Plugin.ConsoleFilters ]}).render(); // unknown categories and sources are allowed.yconsole.filter.hideCategory('error'); // hide and show methods support N arguments.yconsole.filter.hideSource('attribute','widget'); /* Alternatelyvar yconsole = new Y.Console({    boundingBox: '#console',    height: '400px',    width: '450px',    style: 'block',    newestOnTop: false}).plug(Y.Plugin.ConsoleFilters, {    category: {        error: false    },    source: {        attribute: false,        widget: false    }}).render();*/ // Broadcast a log message from a button that uses a custom category and sourceY.on('click', function () {    Y.log('Logging a message to the Console','my_stuff','MyApp');},'#log'); // It is also possible to set the filter's subattributes directlyY.on('click', function () {    var current = yconsole.filter.get('category.info');     yconsole.filter.set('category.info', !current);     this.set('text', (current ? 'Show' : 'Hide') + ' info messages');},'#toggle_info'); });

    // Create a YUI instance and request the console module and its dependencies
    YUI({base:"../../build/", timeout: 10000, filter:"debug", logInclude: {event:true, attribute:true, node:true, base:true, widget:true, MyApp:true}}).use("console", "console-filters", function (Y) {

    // create the console instance
    var yconsole = new Y.Console({
        boundingBox: '#yconsole',
        height: '400px',
        width: '450px',
        newestOnTop: false,
        style: 'block',
        plugins: [ Y.Plugin.ConsoleFilters ]
    }).render();

    // unknown categories and sources are allowed.
    yconsole.filter.hideCategory('error');

    // hide and show methods support N arguments.
    yconsole.filter.hideSource('attribute','widget');

    /* Alternately
    var yconsole = new Y.Console({
        boundingBox: '#console',
        height: '400px',
        width: '450px',
        style: 'block',
        newestOnTop: false
    }).plug(Y.Plugin.ConsoleFilters, {
        category: {
            error: false
        },
        source: {
            attribute: false,
            widget: false
        }
    }).render();
    */

    // Broadcast a log message from a button that uses a custom category and source
    Y.on('click', function () {
        Y.log('Logging a message to the Console','my_stuff','MyApp');
    },'#log');

    // It is also possible to set the filter's subattributes directly
    Y.on('click', function () {
        var current = yconsole.filter.get('category.info');

        yconsole.filter.set('category.info', !current);

        this.set('text', (current ? 'Show' : 'Hide') + ' info messages');
    },'#toggle_info');

    });

// Create a YUI instance and request the console module and its dependencies YUI({base:"../../build/", timeout: 10000, filter:"debug", logInclude: {event:true, attribute:true, node:true, base:true, widget:true, MyApp:true}}).use("console", "console-filters", function (Y) { // create the console instance var yconsole = new Y.Console({ boundingBox: '#yconsole', height: '400px', width: '450px', newestOnTop: false, style: 'block', plugins: [ Y.Plugin.ConsoleFilters ] }).render(); // unknown categories and sources are allowed. yconsole.filter.hideCategory('error'); // hide and show methods support N arguments. yconsole.filter.hideSource('attribute','widget'); /* Alternately var yconsole = new Y.Console({ boundingBox: '#console', height: '400px', width: '450px', style: 'block', newestOnTop: false }).plug(Y.Plugin.ConsoleFilters, { category: { error: false }, source: { attribute: false, widget: false } }).render(); */ // Broadcast a log message from a button that uses a custom category and source Y.on('click', function () { Y.log('Logging a message to the Console','my_stuff','MyApp'); },'#log'); // It is also possible to set the filter's subattributes directly Y.on('click', function () { var current = yconsole.filter.get('category.info'); yconsole.filter.set('category.info', !current); this.set('text', (current ? 'Show' : 'Hide') + ' info messages'); },'#toggle_info'); });

#### CSS

    #console {    margin: 0 auto 1em;} // To override some styles applied by the YDN chrome#demo .yui-console .yui-console-title {    border: 0 none;    color: #000;    font-size: 13px;    font-weight: bold;    margin: 0;    text-transform: none;}#demo .yui-console .yui-console-entry-meta {    margin: 0;}

    #console {
        margin: 0 auto 1em;
    }

    // To override some styles applied by the YDN chrome
    #demo .yui-console .yui-console-title {
        border: 0 none;
        color: #000;
        font-size: 13px;
        font-weight: bold;
        margin: 0;
        text-transform: none;
    }
    #demo .yui-console .yui-console-entry-meta {
        margin: 0;
    }

#console { margin: 0 auto 1em; } // To override some styles applied by the YDN chrome #demo .yui-console .yui-console-title { border: 0 none; color: #000; font-size: 13px; font-weight: bold; margin: 0; text-transform: none; } #demo .yui-console .yui-console-entry-meta { margin: 0; }

Copyright © 2009 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings