Reputation: 1675
I have a website working and I would like to add log to be able to monitor if some errors append during the day.
I've search on stackoverflow and on the internet about it and there is a lot of information about logging framework, what to log... but for a beginner it is confusing. I do not know how to start.
Here are my questions :
file_put_contents
in my PHP code, inside the catch {}
. Is this solution possible ? If not, why? If yes, why is everybody using framework?Upvotes: 0
Views: 318
Reputation: 2778
Personally I use this function
error_log();
I can then log any manual errors, along with other things such as index not found or parse errors, etc (which should not be occurring regularly). This function will assume the error log is wherever it's specified in php.ini
Upvotes: 1
Reputation: 37119
Your php.ini will have a log file specified where PHP will log errors. Your web server, such as Apache, will also have its own log file to record access and error. It is completely OK to have your own application log that will record abnormalities you observe in general operations.
Upvotes: 1
Reputation: 2149
If I had no info, I would create a .txt file and use file_put_contents in my PHP code, inside the catch {}. Is this solution possible ? If not, why?
You basically have three options here:
Personally it depends on the scale of your application. For small and medium web pages I think a simple logfile would be sufficient. But if you have lots of different errors and are willing to put some effort in, I can see the benefits of a database solution, especially the reporting on your errors can be a helpful tool.
If yes, why is everybody using framework?
A framework can help you, so that you don't have to code your error management from scratch.
How do you use a log file. Do you monitor it once a day, twice a week ?
There is no hard truth here. I would probably use two apporaches at the same time. I'd create a cronjob which gathers the recent errors and email those once a week to a specific email address. The recipient has to go through the errors (maybe just a summary) and will check if there is anything out of the ordinary. I would also implement a service that monitors your database/log file and create an alert (in form of an email for example) if there is a peek in error messages that is unusual. That way you can easily monitor error peaks.
Where will be located this log file on sever ? In the www (public) directory ? or elsewhere?
I would host them on the webserver but not in a public directory.
Upvotes: 1