Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of PHP error_log for info logs?

Tags:

php

logging

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 ?

like image 713
jayunit100 Avatar asked Oct 04 '11 23:10

jayunit100


People also ask

Is there a PHP log?

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.

Where can I find PHP logs?

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.

How do I create a PHP error log?

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);

How can we log error messages to an email in PHP?

PHP error_log() Function The error_log() function sends an error message to a log, to a file, or to a mail account.


2 Answers

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');    } 
like image 99
Andrew Avatar answered Oct 02 '22 07:10

Andrew


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);
like image 34
Maxence Avatar answered Oct 02 '22 09:10

Maxence