bkdotcom / PHPDebugConsole

Browser/javascript-like logger/console class for PHP
http://www.bradkent.com/php/debug
Other
78 stars 5 forks source link

No works with codeigniter #18

Closed artlantis closed 4 years ago

artlantis commented 6 years ago

Dear Brad,

I tried every option in CodeIgniter with Google Chrome Logger but no success. Neither show any message in console nor throwing any php error.

I used composer technique to install and changed output setting to "chromeLogger".

Also, I tried old ChromePhp with Chrome Console which is working well.

Could you please check?

bkdotcom commented 6 years ago

Just to confirm: you have this plugin installed and active: https://chrome.google.com/webstore/detail/chrome-logger/noaneddfkdjfnfdakjjmocngnfkfehhd

(there are a couple other plugins, but I've had the best luck with this one)

Can you confirm the plugin is working? Do you get output from the two chromeLogger examples found on http://www.bradkent.com/php/debug#chromeLogger ?

how are you initializing PHPDebugConsole? for troubleshooting, lets try something like:

$debug = new \bdk\Debug(array(
    'collect' => true,
        'output' => true,
    'outputAs' => 'chromeLogger',    // "outputAs", not "output" (you mention setting "output")
));

(this will ensure that everything is enabled and outputting the chromeLogger headers)

Further... open up Chrome's console and view the network "tab" and make sure the filter is set to "All". Reload the page you're debugging and select the page request... now inspect the headers. You should see a X-ChromeLogger-Data: response header with a large base64 encoded value.

report back on whether you see the header.

(have you had luck with the default HTML or "script" output?)

artlantis commented 6 years ago

Can you confirm the plugin is working?

I tried, it works well over your website.

Here is my settings (tried both way, setCfg and array):

        require APPPATH."vendor/bdk/debug/src/Debug/Debug.php";
        $debug = new \bdk\Debug();

        $debug->setCfg('collect', true);
        $debug->setCfg('output', true);
        $debug->setCfg('outputAs', 'chromeLogger');
        $debug->log("Hello World...");

There is no X-ChromeLogger-Data in response header!

But, when I tried with ChromePhp I can see X-ChromeLogger-Data: in response header. image

bkdotcom commented 6 years ago

OK I know what's going on...

ChromePhp : each time you call log/info/warn/etc.. ChromePhp re-encodes all of it's log entries and calls php's header('X-ChromeLogger-Data', $data) which replaces the previous value of the header

PhpDebugConsole, on the other hand, only encodes the data and calls header() once (much more efficient) and does so when the script is shutting down. The problem here is that CodeIgniter has already output the response, so no further headers can be added. (Looks like I need to make PhpDebugConsole a bit more CodeIgniter / PSR-7 friendly).

The good news is there is a fix/workaround. I'm not familiar with CodeIgniter, but it appears we need to use a hook https://www.codeigniter.com/user_guide/general/hooks.html It appears either post_controller or display_override would work (display_override appears to be more powerful & could be used to inject PHPDebugConsole's HTML output into your page, etc)

application/config/config.php
$config['enable_hooks'] = true;
application/config/hooks.php
$hook['post_controller'] = function () {
    \bdk\Debug::_output(); // output the X-ChromeLogger-Data header
};
artlantis commented 6 years ago

Yes, it works well now. Thanks Brad for your help.

image

bkdotcom commented 6 years ago

:thumbsup:

I'm going to leave the issue open to document my todos:

bkdotcom commented 4 years ago

Finally released v2.3.. going to close this out v2.3 also includes a getHeaders method

one of the excuses why 2.3 took so long is that I've been distracted by 3.0.
v3.0 currently has a lot more options/methods for handling output