Console: Creating a Console for debugging
This example walks through the basics of setting up and logging messages to a YUI Console instance. Three Console instances are created with slightly different configurations. All calls to Y.log(..)
will be broadcast to every Console.
Basic Console
New messages added to bottom
Custom strings
Rendered in default location (top right)
Log some messages
Markup not required
The primary purpose of the Console is to aid in debugging your application. As such, it doesn't make much sense to require your page to include markup for something that is not bound for production.
However, Console is built on the Widget framework, so for this example, we'll illustrate three ways to place a Console instance on the page:
- Providing a
boundingBox
node reference. - Providing both
boundingBox
andcontentBox
references. - Using the default rendering behavior.
For the first two cases, we need to set up some markup. We'll throw in some placeholder content for the third.
<h4>Basic Console</h4> <div id="basic"></div> <h4>New messages added to bottom</h4> <div id="add_to_bottom"><div></div></div> <h4>Custom strings</h4> <p><em>Rendered in default location (top right)</em></p>
<h4>Basic Console</h4> <div id="basic"></div> <h4>New messages added to bottom</h4> <div id="add_to_bottom"><div></div></div> <h4>Custom strings</h4> <p><em>Rendered in default location (top right)</em></p>
Then instantiate the Console instances.
// Create a YUI instance and request the console module and its dependencies YUI({base:"../../build/", timeout: 10000}).use("console", "console-filters", "dd-plugin", function (Y) { // Create and render the three Console instances var basic, newOnBottom, customStrings; basic = new Y.Console({ boundingBox: '#basic', style: 'block' // keeps the Console in the page flow as a block element }).render(); // note the inline render() newOnBottom = new Y.Console({ boundingBox: '#add_to_bottom', contentBox: '#add_to_bottom > div', style: 'inline', // keeps the Console in the page flow as an inline element newestOnTop: false, visible: false // hidden at instantiation }).render(); customStrings = new Y.Console({ strings: { title : 'Console with custom strings!', pause : 'Wait', clear : 'Flush', collapse : 'Shrink', expand : 'Grow' }, plugins: [ Y.Plugin.Drag, Y.Plugin.ConsoleFilters ], // add some plugins visible: false // hidden at instantiation }).render(); });
// Create a YUI instance and request the console module and its dependencies YUI({base:"../../build/", timeout: 10000}).use("console", "console-filters", "dd-plugin", function (Y) { // Create and render the three Console instances var basic, newOnBottom, customStrings; basic = new Y.Console({ boundingBox: '#basic', style: 'block' // keeps the Console in the page flow as a block element }).render(); // note the inline render() newOnBottom = new Y.Console({ boundingBox: '#add_to_bottom', contentBox: '#add_to_bottom > div', style: 'inline', // keeps the Console in the page flow as an inline element newestOnTop: false, visible: false // hidden at instantiation }).render(); customStrings = new Y.Console({ strings: { title : 'Console with custom strings!', pause : 'Wait', clear : 'Flush', collapse : 'Shrink', expand : 'Grow' }, plugins: [ Y.Plugin.Drag, Y.Plugin.ConsoleFilters ], // add some plugins visible: false // hidden at instantiation }).render(); });
Log some messages
In your code, inserting calls to Y.log(..)
will cause the content of those log messages to be broadcast to every Console instance present in the YUI instance. We'll illustrate this by creating some buttons to log various types of messages.
// Create a YUI instance and request the console module and its dependencies YUI({base:"../../build/", timeout: 10000}).use("console", "console-filters", "dd-plugin", function (Y) { // Create and render the three Console instances var basic, newOnBottom, customStrings; ... function report(e,type) { Y.log(this.get('value'),type); } // Set the context of the event handler to the input text node // for convenience and pass the message type as a second arg Y.on('click', report, '#info', Y.one('#info_text'), 'info'); Y.on('click', report, '#warn', Y.one('#warn_text'), 'warn'); Y.on('click', report, '#error', Y.one('#error_text'), 'error'); });
// Create a YUI instance and request the console module and its dependencies YUI({base:"../../build/", timeout: 10000}).use("console", "console-filters", "dd-plugin", function (Y) { // Create and render the three Console instances var basic, newOnBottom, customStrings; ... function report(e,type) { Y.log(this.get('value'),type); } // Set the context of the event handler to the input text node // for convenience and pass the message type as a second arg Y.on('click', report, '#info', Y.one('#info_text'), 'info'); Y.on('click', report, '#warn', Y.one('#warn_text'), 'warn'); Y.on('click', report, '#error', Y.one('#error_text'), 'error'); });
Full Code Listing
Markup
<div id="demo"> <h4>Basic Console</h4> <div id="basic"></div> <p> <button type="button" id="toggle_basic">hide console</button> </p> <h4>New messages added to bottom</h4> <div id="add_to_bottom"><div></div></div> <p> <button type="button" id="toggle_atb">show console</button> </p> <h4>Custom strings</h4> <p><em>Rendered in default location (top right)</em></p> <p> <button type="button" id="toggle_cstrings">show console</button> </p> <h4>Log some messages</h4> <p> <input type="text" id="info_text" value="I'm an info message!"> <button type="button" id="info">log info message</button> </p> <p> <input type="text" id="warn_text" value="I'm a warning!"> <button type="button" id="warn">log warning</button> </p> <p> <input type="text" id="error_text" value="I'm an error!"> <button type="button" id="error">log error</button> </p> </div>
<div id="demo"> <h4>Basic Console</h4> <div id="basic"></div> <p> <button type="button" id="toggle_basic">hide console</button> </p> <h4>New messages added to bottom</h4> <div id="add_to_bottom"><div></div></div> <p> <button type="button" id="toggle_atb">show console</button> </p> <h4>Custom strings</h4> <p><em>Rendered in default location (top right)</em></p> <p> <button type="button" id="toggle_cstrings">show console</button> </p> <h4>Log some messages</h4> <p> <input type="text" id="info_text" value="I'm an info message!"> <button type="button" id="info">log info message</button> </p> <p> <input type="text" id="warn_text" value="I'm a warning!"> <button type="button" id="warn">log warning</button> </p> <p> <input type="text" id="error_text" value="I'm an error!"> <button type="button" id="error">log error</button> </p> </div>
JavaScript
// Create a YUI instance and request the console module and its dependencies YUI({base:"../../build/", timeout: 10000}).use("console", "console-filters", "dd-plugin", function (Y) { // Create and render the three Console instances var basic, newOnBottom, customStrings; basic = new Y.Console({ boundingBox: '#basic', style: 'block' // keeps the Console in the page flow as a block element }).render(); newOnBottom = new Y.Console({ boundingBox: '#add_to_bottom', contentBox: '#add_to_bottom > div', style: 'inline', // keeps the Console in the page flow as an inline element newestOnTop: false, visible: false }).render(); customStrings = new Y.Console({ strings: { title : 'Draggable Console with filters!', pause : 'Wait', clear : 'Flush', collapse : 'Shrink', expand : 'Grow' }, plugins: [ Y.Plugin.Drag, Y.Plugin.ConsoleFilters ], // add some plugins visible: false }).render(); // Set up the button listeners function toggle(e,cnsl) { if (cnsl.get('visible')) { cnsl.hide(); this.set('innerHTML','show console'); } else { cnsl.show(); this.set('innerHTML','hide console'); } } function report(e,type) { Y.log(this.get('value'),type); } // Display a message in the Console for reference Y.log("Click the buttons below to log messages"); // Pass the corresponding Console instance to the handler as a second arg Y.on('click', toggle, '#toggle_basic', null, basic); Y.on('click', toggle, '#toggle_atb', null, newOnBottom); Y.on('click', toggle, '#toggle_cstrings', null, customStrings); // Set the context of the event handler to the input text node // for convenience and pass the message type as a second arg Y.on('click', report, '#info', Y.one('#info_text'), 'info'); Y.on('click', report, '#warn', Y.one('#warn_text'), 'warn'); Y.on('click', report, '#error', Y.one('#error_text'), 'error'); });
// Create a YUI instance and request the console module and its dependencies YUI({base:"../../build/", timeout: 10000}).use("console", "console-filters", "dd-plugin", function (Y) { // Create and render the three Console instances var basic, newOnBottom, customStrings; basic = new Y.Console({ boundingBox: '#basic', style: 'block' // keeps the Console in the page flow as a block element }).render(); newOnBottom = new Y.Console({ boundingBox: '#add_to_bottom', contentBox: '#add_to_bottom > div', style: 'inline', // keeps the Console in the page flow as an inline element newestOnTop: false, visible: false }).render(); customStrings = new Y.Console({ strings: { title : 'Draggable Console with filters!', pause : 'Wait', clear : 'Flush', collapse : 'Shrink', expand : 'Grow' }, plugins: [ Y.Plugin.Drag, Y.Plugin.ConsoleFilters ], // add some plugins visible: false }).render(); // Set up the button listeners function toggle(e,cnsl) { if (cnsl.get('visible')) { cnsl.hide(); this.set('innerHTML','show console'); } else { cnsl.show(); this.set('innerHTML','hide console'); } } function report(e,type) { Y.log(this.get('value'),type); } // Display a message in the Console for reference Y.log("Click the buttons below to log messages"); // Pass the corresponding Console instance to the handler as a second arg Y.on('click', toggle, '#toggle_basic', null, basic); Y.on('click', toggle, '#toggle_atb', null, newOnBottom); Y.on('click', toggle, '#toggle_cstrings', null, customStrings); // Set the context of the event handler to the input text node // for convenience and pass the message type as a second arg Y.on('click', report, '#info', Y.one('#info_text'), 'info'); Y.on('click', report, '#warn', Y.one('#warn_text'), 'warn'); Y.on('click', report, '#error', Y.one('#error_text'), 'error'); });
CSS
/* Override default positioning for two of the example Consoles */ #basic, #add_to_bottom { margin-bottom: 1em; } /* Reapply some style settings that were overridden by the page 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; }
/* Override default positioning for two of the example Consoles */ #basic, #add_to_bottom { margin-bottom: 1em; } /* Reapply some style settings that were overridden by the page 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; }