简而言之:您所需要做的就是修改配置/应用程序.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 查询记录到文件中:
配置/应用程序.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),
],
// Add the following...
'queries' => [
'className' => 'File',
'path' => LOGS,
'file' => 'queries.log',
'scopes' => ['queriesLog']
]
],
您的 SQL 查询现在将被写入日志文件,您可以在其中找到该文件/logs/queries.log