DEV Community

Cover image for A simple way to debug PHP applications.
Akshay Khale
Akshay Khale

Posted on

A simple way to debug PHP applications.

The Problem:

Debugging can be irritating sometimes and when it comes to debugging server-side code, it's can be daunting. Printing the variable values, stopping the execution, and then continue, sometimes it takes hours. In Client-side languages like Javascript, it's pretty simple, you just do a console.log and wait for the complete page to render without stopping.

Solution:

That was my inspiration behind creating console-logger.
A simple PHP library that can be installed using composer and get you started with logging details in your browser console.

How to use it?

  1. Install the package using composer
composer require akshaykhale1992/console-logger
  1. Start using it...

Simple Example:

<?php
include './vendor/autoload.php';
use consoleLogger\Logger;
(new Logger())->emergency("This is from Server Side");

Available Functions:

Logger Function Equivalent Console Log
(new Logger())->emergency console.error
(new Logger())->alert console.warn
(new Logger())->critical console.error
(new Logger())->error console.error
(new Logger())->warning console.warn
(new Logger())->notice console.log
(new Logger())->info console.info
(new Logger())->debug console.debug
(new Logger())->group console.group
(new Logger())->groupEnd console.groupEnd

The consoleLogger\Logger class follows PHP PSR-3: Logger Interface standards and implements all the functions for your specific needs.

I hope it will be useful for you and it's an open-source project so please feel free to tag along by creating issues or by adding new features.

Best.

Top comments (5)

Collapse
 
moopet profile image
Ben Sinclair

Hi Akshay.

I did something similar a while ago when I was frustrated with certain frameworks (Drupal I'm taking about you).

Something that stopped me from making it "complete" was output buffering. If you want the log messages to appear as they're processed, and not get delayed or removed by subsequent crashing parts of your program, you ideally want to send it out of band, but you can't do that with PHP directly.

What you can do is hack the output buffer:

$old_buffer = ob_get_clean();
echo "my script here"
ob_start();
echo $old_buffer;

This results in the message hitting the browser immediately... or at least it does if there's only one level of output buffering! You can (un)fortunately nest them, so that's not a complete solution either.

The other solution I had was to run a second process receiving log messages. In Drupal, that's effectively what drush ws --tail does, but you could write a web interface instead so you didn't have to leave your browser.

Collapse
 
akshaykhale1992 profile image
Akshay Khale

That is an interesting option, I will try to implement it, Thank you for the great suggestion.

Collapse
 
gwutama profile image
Galuh Utama

Instead of printing to console (poor man’s debugger, regardless of language), I would suggest xdebug for PHP. That way you can set breakpoints, inspect variables and objects, follow function calls, resume debugging, etc. I find it to be waaay more comfortable and productive than looking at console logs.

Collapse
 
akshaykhale1992 profile image
Akshay Khale

Yes, I completely agree with you. XDebug is the best but I was looking for something simple which does not break the flow when I am in the zone of continuous coding. I used to log the details in file and then check log files for the logged information but I found this option easy and as we all know "Empty mind is devil's workshop.", I wanted to build something simple.

Collapse
 
akshaykhale1992 profile image
Akshay Khale

I did not know about this, Thank you for sharing.