How to log SQL query of the collection?

Try out $Collection->printLogQuery(true); this will print collection query.


If you want the SQL query from a collection you need to cast it as string. Just add (string) to your previously written logging code.

Mage::log((string)$collection->getSelect(),null,'test.log',true);

As Flyingmana explained to me, thanks to him.

$collection->load($printQuery = false, $logQuery = false)

It is important to log or print the query after the loading, because in the beforeLoad method can lot of thing be done. So the first answer:

Mage::log((string)$collection->getSelect(),null,'test.log',true);

should be used AFTER load is called.