简单来说,你只需要修改
config/app.php 文件即可。
找到
Datasources
配置并将
'log' => true
设置为真。
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
...
'log' => true, // Set this
]
]
如果您的应用程序处于调试模式,则在页面显示SQL错误时,您现在将看到SQL查询。如果您没有打开调试模式,则可以通过添加以下内容将SQL查询记录到文件中:
config/app.php
找到
Log
配置并添加一个新的日志类型:
'Log' => [
'debug' => [
'className' => 'Cake\Log\Engine\FileLog',
'path' => LOGS,
'file' => 'debug',
'levels' => ['notice', 'info', 'debug'],
'url' => env('LOG_DEBUG_URL', null),
],
'error' => [
'className' => 'Cake\Log\Engine\FileLog',
'path' => LOGS,
'file' => 'error',
'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'],
'url' => env('LOG_ERROR_URL', null),
],
'queries' => [
'className' => 'File',
'path' => LOGS,
'file' => 'queries.log',
'scopes' => ['queriesLog']
]
],
现在你的SQL查询将被记录到一个日志文件中,你可以在/logs/queries.log
中找到它。