9

I want to log the SQL query for a custom collection. I tried the following code. But did not work. Any suggestions will be appreciated.

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

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);
12

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

  • Thank you very much. Your code prints the query to the interface. – Sukeshini Sep 26 '13 at 9:49
2

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.

  • How to get current event and print into the logfile in Magento 1.9 – Gem Aug 14 at 9:10
  • I don't get the question. What event? – Fabian Blechschmidt Aug 14 at 18:34
2

Step 1:

$result_colletion = print_r($collection->getSelect());
Mage::log($$result_colletion, null, custom_collection.log,true);

Step 2: After that Login into Magento admin section and enable to log setting . Please see below .

System > Configuration > Developer > Log Settings

Step 3: After that see the log file “custom_collection.log” in var/log/ folder .

1
Mage::log($collection->getSelect()->__toString(), Zend_Log::DEBUG, 'test.log', true);

Edit:

Instead of using null (fallback to DEBUG) as second parameter better use one of these from Zend_Log:

const EMERG   = 0;  // Emergency: system is unusable
const ALERT   = 1;  // Alert: action must be taken immediately
const CRIT    = 2;  // Critical: critical conditions
const ERR     = 3;  // Error: error conditions
const WARN    = 4;  // Warning: warning conditions
const NOTICE  = 5;  // Notice: normal but significant condition
const INFO    = 6;  // Informational: informational messages
const DEBUG   = 7;  // Debug: debug messages

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.