API Query Log

Warning

This feature requires the API listener to work.

This listener appends query log information to the API responses

Note

The listener will only append the queryLog key if debug is set to true.

Setup

Attach it on the fly in your controller beforeFilter, this is recommended if you want to attach it only to specific controllers and actions

<?php
class SamplesController extends AppController
{
    public function beforeFilter(\Cake\Event\EventInterface $event)
    {
        $this->Crud->addListener('Crud.Api'); // Required
        $this->Crud->addListener('Crud.ApiQueryLog');
    }
}

Attach it in AppController::initialize(), this is recommended if you want to attach it to all controllers, application wide

<?php
class AppController extends \Cake\Controller\Controller
{
    public function initialize(): void
    {
        $this->loadComponent('RequestHandler');
        $this->loadComponent('Crud.Crud', [
            'listeners' => [
                'Crud.Api', // Required
                'Crud.ApiQueryLog'
            ]
        ]);
    }
}

Output

Paginated results will include a

{
    "success": true,
    "data": [

    ],
    "queryLog": {
        "default": {
            "log": [
                {
                    "query": "SELECT SOMETHING FROM SOMEWHERE",
                    "took": 2,
                    "params": [

                    ],
                    "affected": 25,
                    "numRows": 25
                },
                {
                    "query": "SELECT SOMETHING FROM SOMEWHERE'",
                    "params": [

                    ],
                    "affected": 1,
                    "numRows": 1,
                    "took": 0
                }
            ]
        }
    }
}

Configuration

By default this listener will log all defined connections.

If you need to select specific connections to log, you can use the connections configuration:

$this->loadComponent('Crud.Crud', [
    'listeners' => [
        'Crud.Api',
        'ApiQueryLog' => [
            'className' => 'Crud.ApiQueryLog',
            'connections' => ['default', 'elastic']
        ]
    ]
]);