5
<title>Example: YUI Configuration to Filter Log Messages</title>
6
<link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
7
<link rel="stylesheet" href="../assets/css/main.css">
8
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
9
<script src="../../build/yui/yui-min.js"></script>
14
<h1>Example: YUI Configuration to Filter Log Messages</h1>
19
<div id="main" class="yui3-u">
20
<div class="content"><style scoped>
24
#demo .yui3-console .yui3-console-title {
33
#demo .yui3-console .yui3-console-entry-meta {
37
.filter-controls p label {
42
vertical-align: middle;
66
border: 1px solid #999;
76
<p>This example illustrates how to configure your YUI instance to ignore certain log messages to aid in reducing the signal-to-noise ratio when debugging.</p>
78
<p>Log messages filtered out from the YUI config are permanently ignored. If you want to be able to temporarily hide and reshow messages, use the <a href="../console-filters/">ConsoleFilters plugin</a>. It is not uncommon to set up <code>logInclude</code> or <code>logExclude</code> in the YUI configuration and use the ConsoleFilters plugin.</p>
80
<p>Log messages can be ignored based on the source (e.g. <code>event</code> or <code>attribute</code>) or based on their log level (info, warn, error).</p>
84
<div class="example yui3-skin-sam">
85
<div id="demo" class="yui3-skin-sam">
86
<div id="console"></div>
88
<div class="filter-controls">
89
<h4>Source filter</h4>
92
<option value="logExclude" selected="selected">Exclude</option>
93
<option value="logInclude">Include</option>
95
<label for="filter_a"><input type="checkbox" name="src_filter" value="sourceA" id="filter_a"> <code>sourceA</code></label>
96
<label for="filter_b"><input type="checkbox" name="src_filter" value="sourceB" id="filter_b"> <code>sourceB</code></label>
97
<label for="filter_c"><input type="checkbox" name="src_filter" value="sourceC" id="filter_c" checked="checked"> <code>sourceC</code></label>
101
<div class="filter-controls">
104
<label for="lvl_info">
105
<input type="radio" name="log_level" id="lvl_info" value="info" checked="checked">
108
<label for="lvl_warn">
109
<input type="radio" name="log_level" id="lvl_warn" value="warn">
112
<label for="lvl_error">
113
<input type="radio" name="log_level" id="lvl_error" value="error">
120
<h4>Log a message</h4>
122
<input type="text" id="msg" value="This is a log message!">
123
<button type="button" id="log">log message</button>
127
<label for="msg_src_a">
128
<input type="radio" name="msg_src" id="msg_src_a" value="sourceA" checked="checked">
131
<label for="msg_src_b">
132
<input type="radio" name="msg_src" id="msg_src_b" value="sourceB">
135
<label for="msg_src_c">
136
<input type="radio" name="msg_src" id="msg_src_c" value="sourceC">
140
<span>Category:</span>
141
<label for="msg_info">
142
<input type="radio" name="msg_cat" id="msg_info" value="info" checked="checked">
145
<label for="msg_warn">
146
<input type="radio" name="msg_cat" id="msg_warn" value="warn">
149
<label for="msg_error">
150
<input type="radio" name="msg_cat" id="msg_error" value="error">
156
<h4>Code preview</h4>
157
<pre id="preview">// YUI instance configuration
166
Y.log("This is a log message!", "info", "sourceA");</pre>
170
<script type="text/javascript">
171
YUI().use("console", "selector-css3", "json-stringify", function (Y) {
173
// Add the default filtering of sourceC messages
174
Y.config.logExclude = {
178
// Create and render the Console
179
var yconsole = new Y.Console({
180
boundingBox: "#console",
185
// Set up event listeners
186
// Source include or exclude select
187
Y.on("change", function () {
188
if (this.get("value") === "logInclude") {
189
Y.config.logInclude = Y.config.logExclude;
190
delete Y.config.logExclude;
192
Y.config.logExclude = Y.config.logInclude;
193
delete Y.config.logInclude;
198
// delegate all checkbox and radio group clicks via a single event subscriber
199
// routing to the appropriate function based on the input name
200
var clickHandlers = {
201
src_filter : updateSourceFilters,
202
log_level : updateLogLevel,
203
msg_src : updatePreview,
204
msg_cat : updatePreview
207
Y.delegate("click", function (e) {
208
var input = e.currentTarget,
209
handler = clickHandlers[ input.get("name") ];
212
handler(input.get("value"), input.get("checked"));
215
}, "#demo", "input[name]");
217
// Log message input and radio groups
218
Y.on("keyup", updatePreview, "#msg");
220
// Log message button
221
Y.on("click", function (e) {
222
var msg = Y.one("#msg").get("value"),
223
cat = Y.one("#demo input[name=msg_cat]:checked").get("value"),
224
src = Y.one("#demo input[name=msg_src]:checked").get("value");
230
function updateSourceFilters(source, checked) {
231
var disposition = Y.one("#incexc").get("value"),
232
cfg = Y.config[disposition]; // Y.config.logInclude or logExclude
236
cfg = Y.config[disposition] = {};
241
if (!Y.Object.size(cfg)) {
242
delete Y.config[disposition];
249
function updateLogLevel(level, checked) {
251
Y.config.logLevel = level;
252
yconsole.set("logLevel", level);
257
function updatePreview() {
258
var filters = Y.all("#demo input[name=src_filter]:checked"),
260
logLevel: Y.one("#demo input[name=log_level]:checked").get("value")
263
if (filters.size()) {
264
cfg[Y.one("#incexc").get("value")] = Y.Array.hash(filters.get("value"));
267
Y.one("#preview").set("text",Y.substitute(
268
"// YUI instance configuration\n" +
269
"var Y = YUI({cfg});\n\n" +
270
"// Log statement\n" +
271
'Y.log("{msg}", "{lvl}", "{src}");',
273
cfg: Y.JSON.stringify(cfg, null, 4),
274
msg: Y.one("#msg").get("value"),
275
lvl: Y.one("#demo input[name=msg_cat]:checked").get("value"),
276
src: Y.one("#demo input[name=msg_src]:checked").get("value")
285
<h3>Setting up filters in the YUI configuration</h3>
286
<p>The configuration object passed to the YUI constructor supports a few settings that can help manage Console output while debugging. These configuration options are <code>logExclude</code>, <code>logInclude</code>, <code>logLevel</code>, <code>filter</code>, and <code>filters</code>.</p>
288
<p>This example will show the use of the <code>logInclude</code>, <code>logExclude</code>, and <code>logLevel</code> configurations.</p>
290
<p>An example configuration might look like this:</p>
292
<pre class="code prettyprint">YUI({
293
filter : 'debug', // request -debug versions of modules for log statements
295
event : true, // Don't broadcast log messages from the event module
296
attribute : true, // or the attribute module
297
widget : true // or the widget module
299
logLevel : 'error', // Show only errors in the Console
300
useBrowserConsole : false // Don't use the browser's native console
301
}).use('overlay','anim','console', function (Y) {
303
/* Console instances will default to logLevel = "info" */
308
<p><code>logExclude</code> and <code>logInclude</code> prevent the logging subsystem from broadcasting filtered log messages. <code>logLevel</code>, on the other hand is used by Console instances to filter messages received from the subsystem.</p>
310
<p>Updating <code>Y.config.logExclude</code> or <code>Y.config.logInclude</code> at runtime will immediately change the subsystem filtering, but will not recover messages previously sent from that source.</p>
312
<pre class="code prettyprint">YUI({
316
}).use('console', function (Y) {
318
/* In here, Y.config refers to the config object passed to the constructor */
320
// Stop broadcasting log messages from the attribute module
321
Y.config.logExclude.attribute = true;
323
// Start broadcasting log messages from the event module again
324
delete Y.config.logExclude.event;
329
<p>When a Console is instantiated, barring explicit <code>logLevel</code> attribute configuration, the <code>logLevel</code> will be adopted from the YUI instance's configured <code>logLevel</code>, or <code>Y.Console.LOG_LEVEL_INFO</code> ("info") as a fallback. Unlike <code>logExclude</code>, changing the value in the YUI configuration will only affect instantiated Consoles from that point on. Additionally, you can manually override the <code>logLevel</code> a Console instance will display by updating its <code>logLevel</code> attribute.</p>
331
<pre class="code prettyprint">YUI({ logLevel : "warn" }).use('console', function (Y) {
333
var yconsole_1 = new Y.Console(); // logLevel == "warn"
335
var yconsole_2 = new Y.Console({
336
logLevel : "info" // override at construction
339
// This will not affect yconsole_1 or yconsole_2
340
Y.config.logLevel = "error";
342
var yconsole_3 = new Y.Console(); // logLevel == "error"
344
yconsole_1.set("logLevel", "info"); // update this instance
349
<p>The interactive portion of this example illustrates the effect of various filter settings against logged messages. In a real application, it is most likely that the logging configuration won't be changed at runtime but set once in the YUI configuration at construction.</p>
351
<p>The most relevant portion of the <a href="#full_code_listing">code for the demo above</a> is the updating of the YUI config and Console attribute.</p>
353
<pre class="code prettyprint">// Create and render the Console
354
var yconsole = new Y.Console({
355
boundingBox: '#console', // anchored to the page for the demo
356
style: "block"
361
// Source include or exclude select
362
Y.on("change", function () {
363
if (this.get("value") === "logInclude") {
364
Y.config.logInclude = Y.config.logExclude;
365
delete Y.config.logExclude;
367
Y.config.logExclude = Y.config.logInclude;
368
delete Y.config.logInclude;
371
}, "#incexc");
373
// These functions are called from a delegated event handler.
374
// See the Full Code Listing for how they are called.
375
function updateSourceFilters(source, checked) {
376
var disposition = Y.one("#incexc").get("value"),
377
cfg = Y.config[disposition]; // Y.config.logInclude or logExclude
381
cfg = Y.config[disposition] = {};
383
cfg[source] = true; // e.g. Y.config.logInclude.sourceA = true;
387
if (!Y.Object.size(cfg)) {
388
delete Y.config[disposition];
395
function updateLogLevel(level, checked) {
397
Y.config.logLevel = level;
398
yconsole.set("logLevel", level);
404
<h3 id="full_code_listing">Full Code Listing</h3>
408
<pre class="code prettyprint"><div id="demo" class="yui3-skin-sam">
409
<div id="console"></div>
411
<div class="filter-controls">
412
<h4>Source filter</h4>
414
<select id="incexc">
415
<option value="logExclude" selected="selected">Exclude</option>
416
<option value="logInclude">Include</option>
418
<label for="filter_a"><input type="checkbox" name="src_filter" value="sourceA" id="filter_a"> <code>sourceA</code></label>
419
<label for="filter_b"><input type="checkbox" name="src_filter" value="sourceB" id="filter_b"> <code>sourceB</code></label>
420
<label for="filter_c"><input type="checkbox" name="src_filter" value="sourceC" id="filter_c" checked="checked"> <code>sourceC</code></label>
424
<div class="filter-controls">
425
<h4>Log level</h4>
427
<label for="lvl_info">
428
<input type="radio" name="log_level" id="lvl_info" value="info" checked="checked">
431
<label for="lvl_warn">
432
<input type="radio" name="log_level" id="lvl_warn" value="warn">
435
<label for="lvl_error">
436
<input type="radio" name="log_level" id="lvl_error" value="error">
442
<div class="form">
443
<h4>Log a message</h4>
445
<input type="text" id="msg" value="This is a log message!">
446
<button type="button" id="log">log message</button>
450
<label for="msg_src_a">
451
<input type="radio" name="msg_src" id="msg_src_a" value="sourceA" checked="checked">
454
<label for="msg_src_b">
455
<input type="radio" name="msg_src" id="msg_src_b" value="sourceB">
458
<label for="msg_src_c">
459
<input type="radio" name="msg_src" id="msg_src_c" value="sourceC">
463
<span>Category:</span>
464
<label for="msg_info">
465
<input type="radio" name="msg_cat" id="msg_info" value="info" checked="checked">
468
<label for="msg_warn">
469
<input type="radio" name="msg_cat" id="msg_warn" value="warn">
472
<label for="msg_error">
473
<input type="radio" name="msg_cat" id="msg_error" value="error">
479
<h4>Code preview</h4>
480
<pre id="preview">// YUI instance configuration
482
"logLevel": "info",
483
"logExclude": {
484
"sourceC": true
488
// Log statement
489
Y.log(&quot;This is a log message!&quot;, &quot;info&quot;, &quot;sourceA&quot;);</pre>
491
</div></pre>
496
<pre class="code prettyprint"><script type="text/javascript">
497
YUI().use("console", "selector-css3", "json-stringify", function (Y) {
499
// Add the default filtering of sourceC messages
500
Y.config.logExclude = {
504
// Create and render the Console
505
var yconsole = new Y.Console({
506
boundingBox: "#console",
507
style: "block"
511
// Set up event listeners
512
// Source include or exclude select
513
Y.on("change", function () {
514
if (this.get("value") === "logInclude") {
515
Y.config.logInclude = Y.config.logExclude;
516
delete Y.config.logExclude;
518
Y.config.logExclude = Y.config.logInclude;
519
delete Y.config.logInclude;
522
}, "#incexc");
524
// delegate all checkbox and radio group clicks via a single event subscriber
525
// routing to the appropriate function based on the input name
526
var clickHandlers = {
527
src_filter : updateSourceFilters,
528
log_level : updateLogLevel,
529
msg_src : updatePreview,
530
msg_cat : updatePreview
533
Y.delegate("click", function (e) {
534
var input = e.currentTarget,
535
handler = clickHandlers[ input.get("name") ];
538
handler(input.get("value"), input.get("checked"));
541
}, "#demo", "input[name]");
543
// Log message input and radio groups
544
Y.on("keyup", updatePreview, "#msg");
546
// Log message button
547
Y.on("click", function (e) {
548
var msg = Y.one("#msg").get("value"),
549
cat = Y.one("#demo input[name=msg_cat]:checked").get("value"),
550
src = Y.one("#demo input[name=msg_src]:checked").get("value");
553
}, "#log");
555
// Support functions
556
function updateSourceFilters(source, checked) {
557
var disposition = Y.one("#incexc").get("value"),
558
cfg = Y.config[disposition]; // Y.config.logInclude or logExclude
562
cfg = Y.config[disposition] = {};
567
if (!Y.Object.size(cfg)) {
568
delete Y.config[disposition];
575
function updateLogLevel(level, checked) {
577
Y.config.logLevel = level;
578
yconsole.set("logLevel", level);
583
function updatePreview() {
584
var filters = Y.all("#demo input[name=src_filter]:checked"),
586
logLevel: Y.one("#demo input[name=log_level]:checked").get("value")
589
if (filters.size()) {
590
cfg[Y.one("#incexc").get("value")] = Y.Array.hash(filters.get("value"));
593
Y.one("#preview").set("text",Y.substitute(
594
"// YUI instance configuration\n" +
595
"var Y = YUI({cfg});\n\n" +
596
"// Log statement\n" +
597
'Y.log("{msg}", "{lvl}", "{src}");',
599
cfg: Y.JSON.stringify(cfg, null, 4),
600
msg: Y.one("#msg").get("value"),
601
lvl: Y.one("#demo input[name=msg_cat]:checked").get("value"),
602
src: Y.one("#demo input[name=msg_src]:checked").get("value")
607
</script></pre>
612
<pre class="code prettyprint"><style scoped>
616
#demo .yui3-console .yui3-console-title {
622
text-transform: none;
625
#demo .yui3-console .yui3-console-entry-meta {
629
.filter-controls p label {
634
vertical-align: middle;
658
border: 1px solid #999;
664
</style></pre>
671
<div id="sidebar" class="yui3-u">
675
<div class="sidebox">
677
<h2 class="no-toc">Examples</h2>
681
<ul class="examples">
684
<li data-description="The basics of setting up a Console">
685
<a href="console-basic.html">Creating a Console for Debugging</a>
690
<li data-description="Using your YUI instance configuration to filter which messages are reported in the Console">
691
<a href="console-yui-config.html">YUI Configuration to Filter Log Messages</a>
696
<li data-description="Using the Console's logSource attribute to consolidate log messages from multiple YUI instances into one Console">
697
<a href="console-global.html">Creating a Universal Console</a>
709
<div class="sidebox">
711
<h2 class="no-toc">Examples That Use This Component</h2>
715
<ul class="examples">
724
<li data-description="Adding the ConsoleFilters plugin to a Console instance for more granular run time log message filtering">
725
<a href="../console-filters/console-filters-intro.html">Using the ConsoleFilters Plugin</a>
737
<script src="../assets/vendor/prettify/prettify-min.js"></script>
738
<script>prettyPrint();</script>