16

This question already has an answer here:

Want to get all Sql queries during the page load.

can any one help me on How to log all Magento 2 SQL queries ?

marked as duplicate by Manoj Deswal, Piyush, Siarhey Uchukhlebau, Prince Patel, liyakat Nov 15 '17 at 11:18

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

31

In your app/etc/di.xml, replace the line:

<preference for="Magento\Framework\DB\LoggerInterface" type="Magento\Framework\DB\Logger\Quiet"/>

with

<preference for="Magento\Framework\DB\LoggerInterface" type="Magento\Framework\DB\Logger\File"/>
<type name="Magento\Framework\DB\Logger\File">
  <arguments>
      <argument name="logAllQueries" xsi:type="boolean">true</argument>
      <argument name="debugFile" xsi:type="string">log/sql.log</argument>
  </arguments>
</type>

Flush Magento cache (in backend, or by executing bin/magento cache:flush) after this.

Log file is located here: var/log/sql.log

  • 6
    I am not sure, but this might not work anymore in 2.2 ; at least it doesnt in my case. – Felix Nov 14 '17 at 16:45
  • 1
    In latest version of magento (i'm checking on 2.3.1) You can use this: php bin/magento dev:query-log:enable --include-all-queries=1 --query-time-threshold=1 --include-call-stack=false (as mentioned by @Felix below) – Siju Joseph Jun 12 at 7:44
12
bin/magento dev:query-log:enable

. (copied my answer from Magento 2 log database queries)

6

Alternative (to the answer of Siju Joseph),

if you don't want to change your magento config, you can also configure mysql itself: activate slow.log and set it to 0 seconds.

Therefore edit /etc/mysql/my.cnf and set slow_query_log to 'on' and long_query_time to '0'.

This will result in all mysql queries getting logged.

  • How is that done? – Felix Nov 8 '17 at 9:24
  • in the my.cnf. Just set slow_query_log to 'on' and long_query_time to '0' – Jonas Hünig Nov 9 '17 at 13:26

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