首页 > 解决方案 > 无法在 Codeigniter4 中触发 ChromeLogger 处理程序

问题描述

我是 php 及其框架的新手。我遵循了有关如何在 Logger.php 中添加 ChromeLogger的官方指南。我还为Chrome Logger添加了 chrome 扩展。

如何查看控制器上的任何日志记录信息?

记录器.php

<?php

namespace Config;

use CodeIgniter\Config\BaseConfig;

class Logger extends BaseConfig
{
    ...
    public $handlers = [

        //--------------------------------------------------------------------
        // File Handler
        //--------------------------------------------------------------------

        'CodeIgniter\Log\Handlers\FileHandler' => [

            /*
             * The log levels that this handler will handle.
             */
            'handles'         => [
                'critical',
                'alert',
                'emergency',
                'debug',
                'error',
                'info',
                'notice',
                'warning',
            ],

            /*
             * Leave this BLANK unless you would like to set something other than the default
             * writeable/logs/ directory. Use a full getServer path with trailing slash.
             */
            'path'            => WRITEPATH . 'logs/',

            /*
             * The default filename extension for log files. The default 'php' allows for
             * protecting the log files via basic scripting, when they are to be stored
             * under a publicly accessible directory.
             *
             * Note: Leaving it blank will default to 'php'.
             */
            'fileExtension'   => 'php',

            /*
             * The file system permissions to be applied on newly created log files.
             *
             * IMPORTANT: This MUST be an integer (no quotes) and you MUST use octal
             * integer notation (i.e. 0700, 0644, etc.)
             */
            'filePermissions' => 0644,
        ],

        /**
         * The ChromeLoggerHandler requires the use of the Chrome web browser
         * and the ChromeLogger extension. Uncomment this block to use it.
         */
        'CodeIgniter\Log\Handlers\ChromeLoggerHandler' => [
            /*
                  * The log levels that this handler will handle.
                  */
            'handles' => [
                'critical', 'alert', 'emergency', 'debug',
                'error', 'info', 'notice', 'warning'
            ],
        ]
    ];
}

新闻.php

<?php

namespace App\Controllers;

use App\Models\NewsModel;
use CodeIgniter\Controller;


class News extends Controller
{
    public function index()
    {
        log_message('info', 'News Index page is visited');
        $model = new NewsModel();

        $data = [
            'news'  => $model->getNews(),
            'title' => 'News archive',
        ];

        echo view('templates/header', $data);
        echo view('news/overview', $data);
        echo view('templates/footer');
    }

}

标签: phpcodeignitercodeigniter-4chromelogger

解决方案


首先在 app\Config\Logger.php 中检查你的阈值:

public $threshold = 4;

出厂默认值为 4,但要记录“信息”级别的消息 $threshold 应该是 7!

其次,您可以在浏览器开发工具(网络 -> 标头 -> 响应)中验证您的服务器是否正在发送 ChromeLogger 数据。标题:

X-ChromeLogger-数据: ...

应包括在内。

第三,看起来原来的 ChromeLogger 扩展在 Chrome 中不起作用,但在 Firefox 中运行良好。日志消息应该在开发工具控制台中。

我安装了这个扩展:

Rasmus Schultz 的服务器日志

它通过在新的开发工具选项卡“服务器日志”中添加日志信息来工作。它还支持新的日志头X-ServerLog-Location


推荐阅读