NOTE : This question is posed with the idea that there is only one answer that is marked as Community Wiki , to encourage the use of the error log, which will save us perhaps hours helping to find the cause of almost all errors in a concrete way. of programming.
I use PHP online, linked to a domain hosted on shared hosting.
In my case, the error log is a file called error_log
, which is created in each folder where there are php files and one of those files generates an error.
When something fails, the first thing is to check errorlog
the folder where the PHP script that is failing runs .
From my experience answering questions here, I realize that a large number of users do not check the error log before asking . Some say that they have been racking their brains for days or hours without finding the error, and when I read that I always think: Oh, if I only knew there was an error log!
Partial view of the error log, indicating the path of the file that fails, the line and the cause of the error.
The idea of this question is to generate an answer that could be a community Wiki, in which the way to access the error log is indicated (according to the frameworks, or according to the hostings in which we have experience).
It is also intended to sensitize beginner programmers, or those who started some time ago, to use such a useful debugging tool that, in my opinion, is not used in many cases due to lack of knowledge about it.
Then:
How to find the error log to debug our programs according to the platform or PHP framework that we are using?
In PHP the error log is used to:
This file therefore contains valuable information about the errors that could be occurring in our program.
It would be the first source to consult to determine where exactly the error occurs , since the errorlog gives information about the path of the failed script, the cause of the failure and the page number where the error is found.
The question that many might ask is: but where do I find the error log to review it? The answer is not that simple, because its location and name vary depending on the platform/environment we are using.
Let's consider this answer using two ways to find such an important source of information.
the fast track
It will at least let us know what our file is called. Knowing your name, we will be able to search for your location more easily.
To do this we can execute on any page:
This call will show all the configuration of our PHP. It is usually a lot of information, so you can do a search with the word
error
for example. By the way, check if the error log is activated or not.Here is almost everything we want to know:
error_log
display_errors
, which isOff
. That is so that the errors are not shown on the client screen.Now we just have to find the location of our error file. Which can vary depending on the context. For example in my case: Bluehost shared hosting for a web page, a file is created in each folder. When a file in that folder fails, it is written to the error log.
Another thing to keep in mind is that the last errors are always written at the end of the file , not at the beginning of it. If there are a lot of errors the file can grow, even many megabytes. To avoid this, the first thing is to have an optimized code, of course, avoiding Undefined index type messages from occurring ... Don't be lazy and check the variables before trying to use them.
The direct route (depending on your platform)
Here is a table to complete with the location of the error log according to the most used frameworks or hostings.
The answer is Community Wiki, if you have experience with some environment / framework, take 3 minutes to edit it, putting in the last row the way the error log is accessed.
Be careful not to damage the board. When writing some vertical line will move, re-position it so that it is below the others.
(*) It is important to note that the error will be written to the folder of the calling script. If for example, I have a script called
llamador.php
that works fine, but it calls another file with an error that is calledllamado.php
that is in another folder. The error will be written to the folder where is locatedllamador.php
, even if it isllamado.php
the one that fails. Of course, the log will tell us what error is inllamado.php
.Resource 1: Link
Error log in development
If we are developing on Windows with Xampp (version 3.x) the PHP error log is accessible by following these steps:
It is a little used option since if we have display_errors ON , "normally" the interpreter itself will show us the errors, but it may be necessary when we work with third-party code in which the errors have been silenced within the code itself.
Log of own errors of frameworks
Frameworks and some other tools can implement their own logging system that records errors and sometimes other types of messages.
The Laravel 5 logs can be found in the "storage/logs/" directory.
To access the PHP error log if we are developing with XAMPP on Windows (version 3.x):
As for the frameworks and some other tools, each one implements its own log system, in Laravel 5 we can find them in the "storage/logs/" directory.
You can get the Logs in the PHP route if you marked it, but what happens if you don't mark it?? Well, it also exists.
Assuming you have basic knowledge of Apache2 and have installed on a gnu/linux server
You must look in the default folder
/var/log/apache2/
there you will find all the logs generated by day ofapache2/PHP
But, what if you have multiple virtualhosts and you don't know which PHP web page gave the error. Well, let's just look at a virtualhost:
As you can see there is a variable
ErrorLog
that tells you where it is located (it${APACHE_LOG_DIR}
is the default location), It also stores PHP errors so you do not need to enable the PHP error unless you want to store it in another routeADDITIONAL: