Example—logging database activity

The following example shows how to log database activity using the Magento\Framework\DB\LoggerInterface, which has two implementations:

You can also use the Magento CLI to enable and disable database logging.

To change the default configuration of \Magento\Framework\DB\Logger\LoggerProxy, edit your app/etc/di.xml.

First, change the default values of loggerAlias and logCallStack arguments to:

1
2
3
4
5
6
7
8
<type name="Magento\Framework\DB\Logger\LoggerProxy">
    <arguments>
        <argument name="loggerAlias" xsi:type="const">Magento\Framework\DB\Logger\LoggerProxy::LOGGER_ALIAS_FILE</argument>
        <argument name="logAllQueries" xsi:type="init_parameter">Magento\Framework\Config\ConfigOptionsListConstants::CONFIG_PATH_DB_LOGGER_LOG_EVERYTHING</argument>
        <argument name="logQueryTime" xsi:type="init_parameter">Magento\Framework\Config\ConfigOptionsListConstants::CONFIG_PATH_DB_LOGGER_QUERY_TIME_THRESHOLD</argument>
        <argument name="logCallStack" xsi:type="boolean">false</argument>
    </arguments>
</type>

After that, provide the file path for Magento\Framework\DB\Logger\File:

1
2
3
4
5
<type name="Magento\Framework\DB\Logger\File">
    <arguments>
        <argument name="debugFile" xsi:type="string">log/db.log</argument>
    </arguments>
</type>

Finally, compile the code with:

1
bin/magento setup:di:compile

and clean the cache with:

1
bin/magento cache:clean