|Ratings||Unique User Downloads||Download Rankings|
|Not enough user ratings||Total: 160||All time: 8,785 This week: 347|
|log-delta-time 1.0.0||GNU General Publi...||5.4||PHP 5, Logging, Performance and optim...|
This class can log the time difference between PHP statements.
CLASS CREATING LOG FILE TO DEBUG PHP SCRIPT AND TRACKING EXECUTION TIME DIFFERENCE (DELTA TIME) BETWEEN LOG STATEMENTS
Alessandro Quintiliani <alex23rps at gmail dot com>
GNU GPL (see file COPYING.txt)
PHP >= 5.4
1) Place Class.LogDeltaTime.php in the root directory of your web application. If you still have in your web application a directory containing other classes, place Class.LogDeltaTime.php in the same directory, modifying its relative path
2) Include Class.LogDeltaTime.php at the top of the PHP script to debug with one of the following instructions
3) Instantiate LogDeltaTime class at the top of PHP script with the following syntax:
$log = new LogDeltaTime(<log directory name>, <log filename>, <writing mode>)
<log directory name> MANDATORY STRING ARGUMENT. Name of the directory containing the log file. It's created, if not exists, at the same level of the PHP script to debug the first time it runs.
<log filename> MANDATORY STRING ARGUMENT. Name of the log file. It's created, if not exists, inside <log directory name> the first time PHP script runs.
<writing mode> MANDATORY NON-NEGATIVE INTEGER ARGUMENT. Mode of writing to the log file.
This parameter can only have one of the following values:
when PHP script runs and LogDeltaTime class instance is encountered, the following header statement is written to the log file:
<MM-DD-YYYY> <hh:mm:ss> - <IP REMOTE ADDRESS> - <IP SERVER ADDRESS> - START EXECUTION FILE /<webroot>/<path to the PHP script>
elsewhere in the description of this README.md file, the above statements have the same meaning except for the timestamp, which is related to the current method invoked.
4) Anywhere in the PHP script, call the following method
$log->wlog("this is a useful message to debug the PHP script");
When the PHP script runs and this method is encountered, its statement is written to the log file according to one of the following formats
<MM-DD-YYYY> <hh:mm:ss> - <IP REMOTE ADDRESS> - <IP SERVER ADDRESS> - [block variable value] - step s<N> - DELTA[s<N> - s<N-1>)]=<time difference in seconds between the Nth log statement and the (N-1)th log statement> - This is a useful message to debug the PHP script
<MM-DD-YYYY> <hh:mm:ss> - <IP REMOTE ADDRESS> - <IP SERVER ADDRESS> - [block variable value] - This is a useful message to debug the PHP script
the choice of the format depends on whether calling the following method setDeltaLog having *true(1st log statement format) orfalse(2nd log statement format) as input argument.By default, if you don't callsetDeltaLogmethod, the first log statement format is set*
$log->setDeltaLog(true); # this is also the default choice if # setDeltaLog is not invoked
the first format can be useful to detect the steps in the program taking long time to execute (each log message is a step) or measure delay in the script, placing
$log->wlog("useful debug message");
just before and after all those instructions which could take long time to execute or potentially delay the execution script (i.e. connection to remote server, time to execute a query). If you are not interested in tracking the delta time steps all over the PHP script but only on some specific parts of it, you can call
from the point where you want all the statements to be written to the log file according to the second format. Again, you can switch back to the first format calling
for the meaning of [block variable value], read the description of setCtrlVar method in the EXTRA OPTIONAL METHODS section
5) At the very bottom of the PHP script, call the method
when the PHP script runs and this method is encountered, the following ending statement is written to the log file:
<MM-DD-YYYY> <hh:mm:ss> - <IP REMOTE ADDRESS> - <IP SERVER ADDRESS> - END EXECUTION FILE /<webroot>/<path to the PHP script> (t=<time elapsed between the Logger instance and the $log->end() execution> sec)
This statements gives the time in seconds elapsed since LogDeltaTime class instance (that is, since the timestamp of the header statement, which contains START EXECUTION FILE).
It is clear that if you instantiate the LogDeltaTime class at the very top of the PHP script (if possible) and call the end() method at the very bottom of the PHP script (if possible), you can get the PHP script time execution as real as possible
6) Release the resource with
EXTRA OPTIONAL METHODS
setDeltaMin(<min secs difference between two consecutive steps to track to log file>)
when log statements are written in the first format (which is the default), if you invoke setDeltaMin (with a positive integer number as input argument), only those statements whose time difference since their previous log statements is at least <min secs difference between two consecutive steps to track to log file> seconds are written to the log file (let's say we put *2as an input argument and the log file is namedmylogfile.txt*)
From this point on, only statements whose time difference with their predecessors is at least2seconds are tracked tomylogfile.txt
setCtrlVar(<block code control variable name>)
This method takes a string as input argument which is a control variable name in a block code (if..then..else, while..., do...while, for, foreach loop). If you have one of the above mentioned blocks code whose execution depends on the value of a variable (i.e. $myvar), and you call
just before or inside these blocks code or loops (or any instruction set), each debug message placed inside the block as an argument of wlog, when written to the log file, will have the value of $myvar prepended at: in this case you can check in the log file if any of the instructions inside each block code are executed according to the expected value of $myvar *
Place Class.LogDeltaTime.php, index.php, example1.php, example2.php, example3.php, example4.php, mainexample.php at the same level in a webroot directory, i.e. loggerdir on a web server having <my.domain.com> or localhost as a domain. Make sure that loggerdir has the writing permission properly configured, or the log directory and the log files won't be created.
Open a browser and type in the URL bar:
a page with four buttons (labelled RUN EXAMPLE1.PHP, RUN EXAMPLE2.PHP, RUN EXAMPLE3.PHP, RUN EXAMPLE4.PHP) is displayed on the browser. Pressing each button, its PHP script is executed and generates logexampleX.txt log file, whose link appears below the buttons after each PHP script execution. For a description about each logexampleX.txt format generated by exampleX.php:
Alternatively, you can type in the URL bar of your browser
a page with one button (labelled RUN MAINEXAMPLE.PHP) is displayed on the browser. Pressing the button, the script *mainexample.phpruns and generateslogmainexample.txt(whose link appears below the button), which is a miscellaneous of the four types of logfile mentioned above and generated by the execution ofexampleX.php*.
In all the above scripts, doesn't matter what each PHP script does when runs, but more how comprehensible each log file could be, which would help to detect if the flow execution is correct or not
|Class.LogDeltaTime.php||Class||Class PHP generating log file with delta time for debugging PHP scripts|
|COPYING.txt||Lic.||GNU GPL Terms of License|
|example1.php||Example||PHP script included in index.php and used as an example to show one type of log file (delta time between two consecutive statements)|
|example2.php||Example||PHP script included in index.php and used as an example to show one type of log file (NO delta time between two consecutive statements)|
|example3.php||Example||PHP script included in index.php and used as an example to show one type of log file (only statements with at least 2 seconds elapsed since their predecessor statements)|
|example4.php||Example||PHP script included in index.php and used as an example to show one type of log file (value of block code control variable prepended at each log statements)|
|index.php||Example||main example file on usage of LogDeltaTime Class. This file includes Class.LogDeltaTime.php, example1.php, example2.php, example3.php, example4.php|
|mainexample.php||Example||Example on usage of LogDeltaTime class and all of its methods to debug a PHP script and generating a log file with different kinds of information. This script is an alternative to index.php and contains a miscellaneous of example1.php, example2.php, example3.php, example4.php|
|README.md||Doc.||Description of LogDeltaTime class, prerequisites, usage and examples|
|Version Control||Unique User Downloads||Download Rankings|