Console: YUI configuration to filter log messages
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.
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 ConsoleFilters plugin. It is not uncommon to set up logInclude or logExclude in the YUI configuration and use the ConsoleFilters plugin.
Log messages can be ignored based on the source (e.g. event or attribute) or based on their log level (info, warn, error).
Source filter
Log level
Log a message
Source: Category:
Code preview
// YUI instance configuration
var Y = YUI({
"logLevel": "info",
"logExclude": {
"sourceC": true
}
});
// Log statement
Y.log("This is a log message!", "info", "sourceA");
Setting up filters in the YUI configuration
The configuration object passed to the YUI constructor supports a few settings that can help manage Console output while debugging. These configuration options are logExclude, logInclude, logLevel, filter, and filters.
This example will show the use of the logInclude, logExclude, and logLevel configurations.
An example configuration might look like this:
logExclude and logInclude prevent the logging subsystem from broadcasting filtered log messages. logLevel, on the other hand is used by Console instances to filter messages received from the subsystem.
Updating Y.config.logExclude or Y.config.logInclude at runtime will immediately change the subsystem filtering, but will not recover messages previously sent from that source.
When a Console is instantiated, barring explicit logLevel attribute configuration, the logLevel will be adopted from the YUI instance's configured logLevel, or Y.Console.LOG_LEVEL_INFO as a fallback. Unlike logExclude, changing the value in the YUI configuration will only affect instantiated Consoles from that point on. Additionally, you can manually override the logLevel a Console instance will display by updating its logLevel attribute.
The interactive portion of this example illustrates the effect of various filter settings against logged messages. In 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.
The most relevant portion of the code for the demo above is the updating of the YUI config and Console attribute.
