karontek/bcons

bcons is console.log for backend, it allows you to send debug data from your PHP code to the browser devtools.

1.0.13 2024-05-03 05:40 UTC

This package is auto-updated.

Last update: 2024-05-04 09:13:07 UTC


README

This package enables you to send messages from your PHP code to the bcons console.

Please visit the demo page TODO links!! to see it in action, and consult the documentation for a detailed explanation of all available options.

Screenshots

bcons console in a devtools panel bcons messages shown in the devtools console Warning messages Error messages Payload sent to the server Session data Cookies data

Requirements

This package requires only PHP >= 5.3, allowing its use in legacy code (where it is most needed 😅).

You will also need a bcons account (there is a 'free forever' plan for 1 user and 1 project) and the free bcons browser extension.

Setup instructions

  1. Create an account at bcons.dev

    1. On the 'Account' page, copy your user token.
    2. Navigate to the Projects page, create your project, and a token will be assigned to it. Copy this token.
  2. Install the bcons browser extension.

    Chromium-based browsers (Google Chrome, Microsoft Edge, Opera, Vivaldi, Brave, etc.)

    Firefox

    After installation, click on the extension icon in your browser and select Options. Enter your user token. The browser extension adds a Bcons-User header to every request made to the domains defined in your project, using your user token. This enables the server code to send any debug messages generated by that request only to you.

  3. Install the PHP package

    composer require karontek/bcons

Usage

Instantiate the bcons class.

use Karontek\Bcons\Bcons;

$console = new Bcons('your project token');

When the bcons class is instantiated, it automatically sends debug messages including the request data payload, current session data, and cookies sent.

It will also send warning and error messages for every warning and error raised by PHP.

But the true power of bcons is unlocked with the ability to send your own messages. Use the log, warn, error methods (or any method of the Console API) to send messages and view them on your bcons console.

$console->log($currentUserData);
$console->warn("Zip code not available for user $userId");
$console->error("No user with id $userId found");

Where are all these messages displayed?

All these messages are displayed in the bcons console, which you can access via the bcons website (log in and navigate to the Console page) or through the browser extension devtools panel. They will also appear in your browser's devtools console.

Console API support notes

  • All methods that accept data can receive multiple parameters. Unless any of the parameters is an array/object, bcons will concatenate all values and show them as a single string, mimicking the behavior of the devtools console.

  • The group and groupCollapsed methods accept a second parameter that will be used as the CSS class name for the details element used to display the group data.

    Classes group1 to group22 are predefined in the bcons console with optimized colors for light and dark themes (you can also pass just a number between 1 and 22). They are also available with the named classes red, orange, amber, yellow, lime, green, emerald, teal, cyan, sky, blue, indigo, violet, purple, fuchsia, pink, rose, stone, neutral, zinc, gray and slate (see Tailwind Color Palette). Colored groups are exclusive to the bcons console. When a colored group is displayed in the devtools console, it appears as a regular group.

  • In the devtools console, a call to clear clears the console and displays a Console cleared message. The bcons clear method accepts an optional boolean parameter; if set to false, that message is not displayed.

  • In the devtools console, the table method displays a table with clickable headers to sort data by that column value. This sorting feature is not yet available in the bcons console.

  • The debug, info, dir and dirxml methods are all aliases of log.

Extra functions

We have added some extra functions for a better debugging experience:

  • ping: sometimes we just need to know that our script has reached a certain point. This function does just that: it displays the file name and line number, consuming little vertical space. You can add an optional caption.

    $console->ping('Begin checkout process');
    68747470733a2f2f62636f6e732e6465762f696d672f70696e674c696768742e706e67
  • clog: this function behaves like a regular log() call but displays the content in any of the 22 colors available for the group() function. Useful when you have many messages and want to emphasize one.

    for ($x = 1; $x < 23; ++$x)
      $console->clog($x, "Message with color $x");
    68747470733a2f2f62636f6e732e6465762f696d672f636c6f674c696768742e706e67