I use error_log
for my logging, but I'm realizing that there must be a more idiomatic way to log application progress. is there an info_log
? or equivalent ?
Enabling the PHP Error LogLog messages can be generated manually by calling error_log() or automatically when notices, warnings, or errors come up during execution. By default, the error log in PHP is disabled. You can enable the error log in one of two ways: by editing php. ini or by using ini_set.
The location of the error log file itself can be set manually in the php. ini file. On a Windows server, in IIS, it may be something like "'error_log = C:\log_files\php_errors. log'" in Linux it may be a value of "'/var/log/php_errors.
Enable Error Logging in php. To log errors in PHP, open the php. ini file and uncomment/add the following lines of code. If you want to enable PHP error logging in individual files, add this code at the top of the PHP file. ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
PHP error_log() Function The error_log() function sends an error message to a log, to a file, or to a mail account.
You can use error_log
to append to a specified file.
error_log($myMessage, 3, 'my/file/path/log.txt');
Note that you need to have the 3 (message type) in order to append to the given file.
You can create a function early on in your script to wrap this functionality:
function log_message($message) { error_log($message, 3, 'my/file/path/log.txt'); }
The equivalent is syslog() with the LOG_INFO
constant:
syslog(LOG_INFO, 'Message');
If you want to use a file (not a good idea because there is no log rotation and it can fail because of concurrency), you can do:
file_put_contents($filename, 'INFO Message', FILE_APPEND);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With