Author: Manuel Lemos
Viewers: 121
Last month viewers: 8
Categories: PHP Tutorials, Sponsored
Often developers can find many errors using PHP error log processing methods and tools. If they have many errors, they have another challenge: decide which bugs they should fix first.
One good criterion is first to fix the bugs causing more PHP errors. Counting the PHP errors that appear in a PHP error log may be a task that consumes a lot of time when you do it manually. Fortunately, there are methods and tools that you use to count and sort the PHP errors that appear in PHP logs.
Please read this short article to learn how to use the Scout Application Performance Monitoring tool to quickly determine the lines of code of a PHP application causing more PHP errors so that you can fix those frequent errors first.
In this article you will learn about:
1. Why You Need to Fix The Most Frequent PHP Errors First
2. What You Can do to Find the Most Frequent PHP Errors
3. How to Find the Most Frequent PHP Errors
4. How to Find the Most Frequent PHP Errors Faster
1. Why You Need to Fix The Most Frequent PHP Errors First
When you need to fix too many errors that you found in your PHP error log, you need to have criteria to decide which of all those errors you will fix first.
If you have specific lines of PHP code causing more PHP errors than others, you should fix that code first because a single bug-fixing effort will make the most significant part of the errors stop.
So fixing the code that causes more errors first is a good criterion.
2. What You Can do to Find the Most Frequent PHP Errors
To find the most frequent errors, you need to process the lines of the PHP error log and count how many times the same error appears in the record, pointing to the same line of the PHP source code causing the PHP errors.
3. How to Find the Most Frequent PHP Errors
You can process the PHP errors at run time using the PHP set_error_handler function, but that is not advisable because you may need to handle too many errors, which may slow down your PHP application.
It is better to configure PHP to send the PHP error logs to an error log file. This way, you can process the error logs with a separate script or application on a computer that is not the same as running PHP. You can achieve this by setting the error_log option in the php.ini configuration file like this:
error_log = logs/php-errors.log
Then you can develop a parser for the php-errors.log file that will extract the PHP error log lines and parses them to extract the source code file name and the line number of each error found in the log file.
Here is an example of PHP code that can parse a line of a PHP error log to extract the source file name and line number.
<?php
$error_log_line = 'PHP Warning: Use of undefined constant sdfsdf - assumed \'sdfsdf\' (this wil throw an Error in a future version of PHP) in /home/mlemos/garbage/x.php on line 3';
if(preg_match('/([^ ]+) on line ([0-9]+)/', $error_log_line, $m))
{
print_r($m);
}
If you run the example code above, it will output this:
Array
(
[0] => /home/mlemos/garbage/x.php on line 3
[1] => /home/mlemos/garbage/x.php
[2] => 3
)
As you may see, the code creates an array with the name of the PHP source file and the line number of the code that caused the error.
A complete error log processing script would be more elaborated. It would also traverse all error log file lines and count the number of times an error happened in the same line of the source code file.
I will leave that part for you as an exercise to keep this article brief.
4. How to Find the Most Frequent PHP Errors Faster
As you may understand by now, creating a basic script to process errors to find the most frequent errors to fix first may not be so simple.
Fortunately, there are several PHP error monitoring solutions available in the market that you can use, so you do not have to developer your key.
For instance, you can use a tool like Scout Application Monitoring to monitor the errors of PHP applications and show excellent charts that help you figure out the most frequent mistakes you should fix.
Below is an example screenshot of the Scout APM tool showing a report of recent errors in an application sorted by frequency.
Maybe in the future, I will write more about Demo of Scout in PHP. For now, I invite you to watch this video of a Scout Application Monitoring. By watching this video, you can learn to monitor the errors of PHP applications and sort the errors to find the most frequent mistakes.
Then you can also try this tool for free during a trial period so you can use it in simple PHP applications you developed.
You need to be a registered user or login to post a comment
Login Immediately with your account on:
Comments:
No comments were submitted yet.