【问题标题】:Where is the log file after enabling query logging?启用查询日志记录后的日志文件在哪里?
【发布时间】:2015-11-10 12:08:14
【问题描述】:

我按照说明在 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']
]);

启用查询日志后,我无法找到日志文件。我在日志文件夹下查看。我没有看到任何queries.log。在哪里可以找到日志文件?

【问题讨论】:

  • 在不接受前一个问题的情况下创建一个不同的问题有点不礼貌。无论如何。检查你的error.log,你可能有错误。您是否有权创建日志文件?
  • 很抱歉。我仍然没有找到答案。是的,我有权限,因为我看到 debug.log 等其他日志文件正在更新。
  • 如果您尝试将其记录到您的 debug.log 文件中会怎样?这行得通吗?
  • 好主意!现在会这样做。
  • 不,它不起作用。 error.log 中也没有错误。

标签: php mysql cakephp logging cakephp-3.0


【解决方案1】:

我创建了一个测试项目。创建了一个简单的模型,以便我可以解析数据。

在控制器中,我添加了这些命名空间:

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

这是控制器中的基本数据解析:

    $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);

所有这些都很棒。

【讨论】:

  • 感谢您的耐心帮助。我按照您的回答,但仍然看不到日志文件。我一定是在不知道是什么的情况下粗心做了什么。谢天谢地,我找到了一种间接查看 SQL 语句的方法。请参阅下面的答案:) stackoverflow.com/questions/33628161/…
【解决方案2】:

还应通过指定 'log' =&gt; true 在数据源配置中启用日志:

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

【讨论】:

    猜你喜欢
    • 2019-01-22
    • 1970-01-01
    • 2011-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多