Tracking PHP error logs

I am coding within php file that is receiving data from AJAX. I am doing something with that data withing pjp file and would like to be able to see errors while I am writing code. Since this.php file is not rendering in the browser and therefore I cant see errors that I will be receiving how can I easily see errors,

I know I can do to apache log and maybe try to pull last 10 lines …something like this…

tail -10 /var/log/apache2/error.log

How do you guys keep your eyes on errors?

Thanks

Hi there,
I think you’re looking for:

sudo tail -f /var/log/apache2/error.log

That will show you the last bit of the file while it’s updating live.

Hope it helps…

1 Like

I also wanted to ask this…

Since my php script is not being rendered in browser how would I print my variable…

I want to say

echo $myvariable;

How would I see the value of this variable?

I would suggest using a string identifier so that you can easily locate it by running a search within the log file e.g. :

error_log("\n myVariable: $myVariable \n");

The “\n” is to create a line-break so that it doesn’t get muddled with everything else, I’m unsure now if you would require that with error_log…

Hope that helps…

That is a really inefficient way to develop. You should look into a local environment and smart debugging. The only time you should be doing that is when the problem can’t be recreated locally and you absolutely must implement debugging measures on production.

2 Likes

I am developing locally…If this script is being rendered in browser i wouldn’t be having this problem

With smart debugging you can call a php script using AJAX and stop the script in real time at the server and go line by line and execute it until you find the error. Using smart debugging doesn’t have anything to do with whether the script is meant to executed at the command line or output to the browser for display.

Is it rendering as literal PHP code or are you asking about how do you get the value of the code?

Well if I have something like this.

$var = 5;
Do something

I want to find what the value of $var is

Example from above where it shows to just output to error log file works ok

Then outputting it to the screen should work. If you are trying to figure out why there’s errors, error.log is where you go. Otherwise, this is not useful information if you want to find out what the value is. If something doesn’t work right in a logic, I normally use die() and I go one step at a time. If the die() is gone, that’s when you know it failed or triggers the other logic which would be something you want to debug more and find out why it is doing what it’s doing.

1 Like

Cant output to screen the script that is not returned to browser. I am outputting my variables in error-log file

Yes you can. The first thing is to create a div that will contain the results. Next, you output to that div by doing

$('.myDiv').html(data);

Where data will come from your success call.

1 Like

Good tip thanks :slight_smile:

This isn’t necessarily a PHP issue so I am going to move it to the Javascript category.

Any output during the script execution will be included in the response body (actually, is the response body), and you can always see the full response in the network panel of the browser dev tools (which also provide some nice features such as appropriately formatting the data, or even rendering HTML). Of course, doing so will likely lead to invalid data your JS won’t understand… e.g. if you expect a JSON response, echoing or var_dump()ing random values will give you something like

Debug message: the eggs are overcooking!
{
  "data": "This should be the actual response"
}

and JSON.parse() will throw an error. So in most cases you’ll want to log your debug info to a file, which you can monitor with tail -f like @Andres_Vaquero suggested… other than that, you might have a look at xdebug for some more sophisticated debugging with breakpoints and all.

1 Like

^ that

I confess I use echo, print_r and var_dump , and even alert() instead of console.log() at times - in pre-alpha code for quick and dirty debugging. It works OK for experimenting with short and simple code. True, xdebug has a bit of a learning curve getting it installed, configured and learning how to use. But if you want to get into seriously writing code it (or some other debugging tool) is well worth using and you will not regret it.

2 Likes

For building PHP API endpoints, you can also use PostMan. God’s gift to programmers. https://www.getpostman.com/

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.