Mansur Khan
Mansur Khan

Reputation: 1675

Adding log to your code : The beginning

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 :

Upvotes: 0

Views: 318

Answers (3)

MrCarrot
MrCarrot

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

zedfoxus
zedfoxus

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.

  1. Yes, it is fine to have a text file that will log business process abnormalities. I'd recommend having a standard for logging. Line would start with a timestamp, include [ERROR], [WARNING] or [INFO] messages so that logs can be parsed for type or error and between X and Y times. Frameworks are being used because they have a lot of built-in methods (scaffolding) already done so you don't have to write code from scratch. Frameworks also make it easier for other developers to hop on-board and start developing in a standard way. Take a look at this answer about why use a framework.
  2. If a framework has a default location where log files go, I'd use that as long as the log file is not exposed to the public
  3. Monitoring/Audit of a log file is different from logging. Depending on the critical nature of business abnormalities, I'd do auditing. For example, if the code is logging a warning that customer is attempting to order a particular part in mass, I'd monitor for WARNING messages every hour. I'd monitor for ERRORS every half hour (again, depending on the critical nature of the error). I'd prefer an overnight audit to see what all went wrong. More importantly, it would be ideal to have someone take charge of attending to those issues. You may find that some WARNINGS and ERRORS are not as important or relevant anymore. In those cases, someone should pick up the task of improving code to remove such logs. That way your logs say relevant and easy to attend to
  4. The file should be located outside of the www public directory and should not be exposed through a URL. I prefer logs to be stored on a different drive altogether.

Upvotes: 1

Mirco Widmer
Mirco Widmer

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:

  • Send an email to a specific email address every time an error ocures.
  • Gather error messages in a logfile as you mentioned.
  • Create a database and log your error messages in there. You could have different severity levels and can build an extensive logging backend if you wish.

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

Related Questions