PHP Execution Tracker
January 20, 2024A PHP light library for tracking and logging process execution in PHP applications.
Table of Contents
Why I created it?
While developing and maintaining the API at my first job, I had to navigate through extensive undocumented legacy code without support from the original creators. Unable to find a suitable debugging library, I ended up creating my own.
Usage
First, install the library with composer:
composer require chus/php-execution-tracker
Once installed, you can use it in your code usign the vendor autoload:
<?php
require_once "/vendor/autoload.php";
use ExecutionTracker\Tracker;
Then, you can start tracking a process like this:
$track = Tracker::track("Wait 1 second");
sleep(1);
$track->end("Waited 1 second");
$track->asJson();
Output:
{
"name": "Wait 1 second",
"result": "Waited 1 second",
"startTime": 1705765167.568996,
"endTime": 1705765168.583068,
"errorOccurred": false,
"warningOccurred": false,
"errors": [],
"warnings": [],
"logs": [],
"subProcedures": []
}
As you can see, the library is very simple to use. You just have to call the track
method with the name of the process you want to track, and then call the end
method with the result of the process.
The idea is to generate a tree of processes that can be easily converted to JSON for logging purposes and display it in a simple way.