Later versions of Magento have added javascript code to /js/varien/js.js to “stub out” the console if it’s not available. This is no doubt to prevent a javascript crash if errant console.log statements get left in throughout the project. The problem is, it prevents console.log from working in Chrome – which I increasingly use just because it’s so much faster.
But there’s a fix (thanks to AlexB in this post http://stackoverflow.com/questions/8080610/javascript-console-log-in-magento for this fix):
Find this block of code in /js/varien/js.js:
if (!("console" in window) || !("firebug" in console))
{
var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
"group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
window.console = {};
for (var i = 0; i < names.length; ++i)
window.console[names[i]] = function() {}
}
And change it to:
var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;</HL>
if (!("console" in window) || !("firebug" in console) && !is_chrome)
{
var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
"group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
window.console = {};
for (var i = 0; i < names.length; ++i)
window.console[names[i]] = function() {}
}
Magento uses caching extensively. If you also want to be able to save something to cache rather than reading from the database or some more expense resource like that, it’s simple:
$value = Mage::app()->loadCache(‘my_cache_key’);
If the key isn’t found, $value will be null.
To store a value in the cache is almost as easy:
Mage::app()->saveCache($data_to_store, ‘my_cache_key’, array(), $duration_in_seconds);
(The empty array in the call above is standing in for a parameter called “tags” which, to my knowledge, is not used. Someone please correct me on that if I’ve got it wrong.)
Sometimes I’m banging my head against the wall trying to figure out why my block isn’t loading right or some other layout element isn’t rendering. And I’ve always wished I could just see the layout as Magento is seeing it. So I finally dug in to see and it’s actually incredibly simple to output the layout for a given page in xml format.
echo Mage::app()->getLayout()->getNode()->asNiceXml('', 0);
The layout is just a Varien_Simplexml_Config. getNode buys you access to the private $_xml variable that holds the root node of the xml document. Then asNiceXml formats it nicely for the screen. If you’d rather output to a file (they can be large chunks of xml) supply a file name for the first parameter of asNiceXml and the xml will be written there, too.
Now back to why my block isn’t loading…
Sometimes Magento does something and it’s just not obvious why. We’ve all heard “why aren’t my products showing up?” more times than we can count. Magento’s object model for building queries is great, but sometimes you just need to see the sql that’s being run on the database in order to know what’s really going on.
In the past I’ve used the general log in mysql. That can be a great source of information, but, depending on the version of mysql, it can be a pain to set up. Other times I’ve inserted logging code into various db classes in order to print out queries that are being run, but I always have to find the right place again. But today I stumbled on this blog entry - http://blog.nexcess.net/2011/04/15/logging-database-queries-in-magento/ that spells out exactly how you can turn on some (as far as I know) undocumented logging functionality that is built right in to the Magento data access layer. Kudos to the author and thanks for sharing!