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:

<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:

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