2

I followed the instructions to enable query logging in cakephp v3.

http://book.cakephp.org/3.0/en/orm/database-basics.html#query-logging

// Turn query logging on.
$conn->logQueries(true);

// Turn query logging off
$conn->logQueries(false);

use Cake\Log\Log;

// Console logging
Log::config('queries', [
    'className' => 'Console',
    'stream' => 'php://stderr',
    'scopes' => ['queriesLog']
]);

// File logging
Log::config('queries', [
    'className' => 'File',
    'path' => LOGS,
    'file' => 'queries.log',
    'scopes' => ['queriesLog']
]);

After enabling query logging, I am not able to find the log file. I looked under the logs folder. I don't see any queries.log. Where can the log file be found?

9
  • It is kind of rude to just create a different question without accepting the previous one. Anyways. Check your error.log, you might have errors. Do you have permissions to create the log file? Commented Nov 10, 2015 at 12:13
  • Sorry about that. I still have not found the answer. Yes, I have the permissions because I see other log files like debug.log being updated. Commented Nov 10, 2015 at 12:15
  • What if you try logging it to your debug.log file? Does that work? Commented Nov 10, 2015 at 12:16
  • Great idea! Will do that now. Commented Nov 10, 2015 at 12:16
  • No, it does not work. No error in the error.log as well. Commented Nov 10, 2015 at 12:17

2 Answers 2

3

I've created a test project. Created a simple model so I can parse the data.

In the controller, I added these namespaces:

use App\Model\Table\User; // <---My model
use Cake\ORM\TableRegistry;
use Cake\Log\Log;
use Cake\Datasource\ConnectionManager;

Here's the basic data parse in a controller:

    $conn = ConnectionManager::get('default');
    Log::config('queries', [
        'className' => 'File',
        'path' => LOGS,
        'file' => 'queries.log',
        'scopes' => ['queriesLog']
    ]);

    $users = TableRegistry::get('User'); 

    $conn->logQueries(true);
    $q = $users->find('all');
    $results = $q->all();
    $conn->logQueries(false);

All of this works just great.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your patient help. I followed your answer but still could not see the log file. I must have done something careless without knowing what it is. Thankfully, I figured out an indirect way to see the SQL statement. See answer below :) stackoverflow.com/questions/33628161/…
0

Log also should be enabled in datasources config by specify 'log' => true:

'Datasources' => [
    'default' => [
        'className' => Connection::class,
        'driver' => Mysql::class,
        'persistent' => false,
        'host' => 'localhost',
        ...
        'log' => true,
        ...
        'url' => env('DATABASE_URL', null),
    ],

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.