Giter VIP home page Giter VIP logo

debug-php's Introduction

debug-php

Simple debugging utility for PHP to print out messages to stdout or the browser's JavaScript console, if supported.

Heavily inspired by debug for node.js.

Latest Stable Version Total Downloads License

Installation

Use Composer and add this to your composer.json:

"require": {
    "zidizei/debug", "~0.1.1"
}

Usage

debug-php lets you print out short debbuging messages for your PHP project either to stdout or to the JavaScript console of your browser, in case you are using this library on your website.

require "vendor/autoload.php";

// initiate debug profile named 'parser'
\Debug\profile("parser");

\Debug\debug("Parsing some stuff now");
/* Parse the stuff */
\Debug\debug("Stuff successfully parsed");

// close the current debug profile (in this case 'parser')
\Debug\profile();

The above code will produce something like this when run using a web server and browser:

parser debug

Output to stdout will look more or less the same using colors defined by your shell.

Disabling debugging messages

By default, debug-php is good to go by just calling its methods. Since 0.1.1 you can call \Debug\off() to explicitly disable debugging messages. In combination with \Debug\on(), this could be used to programmatically decide to skip debug messages for certain sections of your code.

Multiple Profiles

You can use profiles to better distinguish some of your debug messages. The example above only uses one profile by calling:

\Debug\profile("parser")

If a profile by that name already exists, it will be closed. You can close the currently active (as in last opened) profile by omitting the parameter:

\Debug\profile()

If there are no active profiles, it will open a default one (aptly named default). Note that there has to be at least one profile open for debugging.

The following code example demonstrates the use of multiple debugging profiles:

require "../vendor/autoload.php";

// initiate debug profile named 'parser'
\Debug\profile("parser");

\Debug\debug("Parsing some stuff now");
/* Parse the stuff */
\Debug\debug("Stuff successfully parsed");


// initiate debug profile named 'worker'
\Debug\profile("worker");

\Debug\debug("Working with the stuff now");
/* Work with the parsed stuff */
\Debug\debug("Stuff successfully worked with.. or something like that.");

// close the current debug profile (in this case 'worker')
\Debug\profile();


// close the current debug profile (in this case 'parser')
\Debug\profile();

The above code will produce something like this when run using a web server and browser:

multiple profiles debug

Output to stdout will look more or less the same using colors defined by your shell.

Time measurements

The milliseconds at the end of each debug line indicate the time difference between the execution of the current debug message and the previous one.

Additionally, each profile duration is measured and displayed once it has been closed.

debug-php's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

debug-php's Issues

There should be a setting to disable it

The only way I can see disabling it is by making a local variable with a boolean value and then using the ternary operator to quickly check to see if we should engage the debugger or not.

Example of how I'm doing it:

$debug = true;
($debug ? \Debug\profile("parser") : null );

It would be cool to use \Debug\on to turn it on and \Debug\off to turn it off.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.