<commit_before><?php
namespace Crud\Listener;

use Cake\Core\Configure;
use Cake\Datasource\ConnectionManager;
use Cake\Event\Event;

/**
 * When loaded Crud API will include query logs in the response
 *
 * Very much like the DebugKit version, the SQL log will only be appended
 * if the following conditions is true:
 *  1) The request must be 'api' (.json/.xml)
 *  2) The debug level must be 2 or above
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 */
class ApiQueryLogListener extends BaseListener
{

    /**
     * Returns a list of all events that will fire in the controller during its lifecycle.
     * You can override this function to add you own listener callbacks
     *
     * We attach at priority 10 so normal bound events can run before us
     *
     * @return array
     */
    public function implementedEvents()
    {
        if (!$this->_checkRequestType('api')) {
            return [];
        }

        return [
            'Crud.beforeFilter' => ['callable' => [$this, 'setupLogging'], 'priority' => 1],
            'Crud.beforeRender' => ['callable' => [$this, 'beforeRender'], 'priority' => 75]
        ];
    }

    /**
     * Setup logging for all connections
     *
     * @param \Cake\Event\Event $event Event
     * @return void
     */
    public function setupLogging(Event $event)
    {
        foreach ($this->_getSources() as $connectionName) {
            $this->_getSource($connectionName)->logQueries(true);
            $this->_getSource($connectionName)->logger(new \Crud\Log\QueryLogger());
        }
    }

    /**
     * Appends the query log to the JSON or XML output
     *
     * @param \Cake\Event\Event $event Event
     * @return void
     */
    public function beforeRender(Event $event)
    {
        if (!Configure::read('debug')) {
            return;
        }

        $this->_action()->config('serialize.queryLog', 'queryLog');
        $this->_controller()->set('queryLog', $this->_getQueryLogs());
    }

    /**
     * Get the query logs for all sources
     *
     * @return array
     */
    protected function _getQueryLogs()
    {
        $sources = $this->_getSources();

        $queryLog = [];
        foreach ($sources as $source) {
            $queryLog[$source] = $this->_getSource($source)->logger()->getLogs();
        }

        return $queryLog;
    }

    /**
     * Get a list of sources defined in database.php
     *
     * @return array
     * @codeCoverageIgnore
     */
    protected function _getSources()
    {
        return ConnectionManager::configured();
    }

    /**
     * Get a specific data source
     *
     * @param string $source Datasource name
     * @return \Cake\Database\Connection
     * @codeCoverageIgnore
     */
    protected function _getSource($source)
    {
        try {
            return ConnectionManager::get($source);
        } catch (\Cake\Datasource\Exception\MissingDatasourceConfigException $e) {
            //Safe to ignore this
        }
    }
}<commit_msg>Revert "Catch MissingDatasourceConfigException in ApiQueryLogListenern because it should be ignored"<commit_after><?php
namespace Crud\Listener;

use Cake\Core\Configure;
use Cake\Datasource\ConnectionManager;
use Cake\Event\Event;

/**
 * When loaded Crud API will include query logs in the response
 *
 * Very much like the DebugKit version, the SQL log will only be appended
 * if the following conditions is true:
 *  1) The request must be 'api' (.json/.xml)
 *  2) The debug level must be 2 or above
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 */
class ApiQueryLogListener extends BaseListener
{

    /**
     * Returns a list of all events that will fire in the controller during its lifecycle.
     * You can override this function to add you own listener callbacks
     *
     * We attach at priority 10 so normal bound events can run before us
     *
     * @return array
     */
    public function implementedEvents()
    {
        if (!$this->_checkRequestType('api')) {
            return [];
        }

        return [
            'Crud.beforeFilter' => ['callable' => [$this, 'setupLogging'], 'priority' => 1],
            'Crud.beforeRender' => ['callable' => [$this, 'beforeRender'], 'priority' => 75]
        ];
    }

    /**
     * Setup logging for all connections
     *
     * @param \Cake\Event\Event $event Event
     * @return void
     */
    public function setupLogging(Event $event)
    {
        foreach ($this->_getSources() as $connectionName) {
            $this->_getSource($connectionName)->logQueries(true);
            $this->_getSource($connectionName)->logger(new \Crud\Log\QueryLogger());
        }
    }

    /**
     * Appends the query log to the JSON or XML output
     *
     * @param \Cake\Event\Event $event Event
     * @return void
     */
    public function beforeRender(Event $event)
    {
        if (!Configure::read('debug')) {
            return;
        }

        $this->_action()->config('serialize.queryLog', 'queryLog');
        $this->_controller()->set('queryLog', $this->_getQueryLogs());
    }

    /**
     * Get the query logs for all sources
     *
     * @return array
     */
    protected function _getQueryLogs()
    {
        $sources = $this->_getSources();

        $queryLog = [];
        foreach ($sources as $source) {
            $queryLog[$source] = $this->_getSource($source)->logger()->getLogs();
        }

        return $queryLog;
    }

    /**
     * Get a list of sources defined in database.php
     *
     * @return array
     * @codeCoverageIgnore
     */
    protected function _getSources()
    {
        return ConnectionManager::configured();
    }

    /**
     * Get a specific data source
     *
     * @param string $source Datasource name
     * @return \Cake\Database\Connection
     * @codeCoverageIgnore
     */
    protected function _getSource($source)
    {
        return ConnectionManager::get($source);
    }
}
{'fraction_non_alphanumeric': 0.1187479297780722, 'fraction_numerical': 0.002649884067572044, 'mean_word_length': 2.303610503282276, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '804816', 'n_tokens_mistral': 1699, 'n_tokens_neox': 1653, 'n_words': 497}