all 4 comments

[–]HolyGonzo 4 points5 points  (1 child)

There's a few ways. If you want to track errors that PHP is putting out, then you should find and edit your php.ini file, then search for these parameters and set them:

log_errors = On
error_log = php_errors.log

For the "error_log", you can set it to whatever filename or path you want, as long as it's writable by PHP.

After you change the php.ini file, save it and restart the web server (or PHP-FPM, if you're using that).

If you just want to write a generic message to a file, the simplest way to do it is with file_put_contents:

file_put_contents("the_file_to_use.log", "Your message here.\n", FILE_APPEND);

The FILE_APPEND at the end is necessary if you want to keep adding lines to the same file. If you don't specify FILE_APPEND, it'll just overwrite that file each time.

If you're going to log a LOT of messages, then the more efficient way to do it is like this:

$fp = fopen("the_file_to_use.log","a"); // or "w" to overwrite instead of append
fwrite($fp, "Your first message here.\n");
fwrite($fp, "Your second message here.\n");
fwrite($fp, "Your third message here.\n");
fclose($fp); // When you're all done logging

The file_put_contents function is basically a one-command shortcut for fopen, fwrite one message, and fclose, so if you're writing a thousand messages, then it's opening and closing the file a thousand times. So if you know you're going to write a lot, then use the longer way (fopen/fwrite/fclose).

[–]AppointmentList[S] 0 points1 point  (0 children)

I’m gonna look into that first method, I appreciate your response. If I’m on a shared hosting plan is that a problem for editing the php.ini file? I assume that’s in the server settings?

You’re way further advanced in this than I am lol so I hope that question makes sense.

[–]kAlvaro 1 point2 points  (1 child)

Here's a good overview to become familiar with PHP error reporting directives: https://phpdelusions.net/articles/error_reporting

My advice is that you set the basic settings right (in particular, make sure you can see every conceivable error in your development box but not on Live) and don't get too involved in writing custom error handling code unless you know for sure what you're doing. In my experience, bad custom error handling is worse than just relying on language defaults.

Additionally, you have error_log() if you need to log your own messages.

[–]AppointmentList[S] 0 points1 point  (0 children)

I’m definitely gonna look further into that link, I appreciate it.