Equivalent of PHP error_log for info logs?

php error log windows
php error_log not working
php log to console
php syslog
php error logs path
php debug log
error log example
how to check php server logs

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 ?

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

error_log - Manual, Sends an error message to the web server's error log or to a file. Version, Description How much of a risk it is depends on how much and what type of information you are sending, but the mere act of sending an email when something  Look for the line containing the error_log entry in the php.ini file; Ensure there is not a semicolon (;) in front of the entry; Set the error_log entry to the desired path of the log file. For example, you might use error_log = /logs/php-errors.log.

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

PHP Logging Basics - The Ultimate Guide To Logging, This guide explores the basics of logging in PHP including how to configure With the built-in error log, there are a few different elements you'll want to keep in Logging user actions and activities provides a valuable source of information for  Relative paths are accepted as the destination of message_type 3, but beware that the root directory is determined by the context of the call to error_log(), which can change, so that one instance of error_log in your code can lead to the creation of multiple log files in different locations.

I would recommend you to use Monolog: https://github.com/Seldaek/monolog

You can add the dependency by composer:

composer require monolog/monolog

Then you can initialize a file streaming log, for an app called your-app-name, into the file path/to/your.log, as follow:

<?php

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

// create a log channel
$log = new Logger('your-app-name');
$log->pushHandler(new StreamHandler('path/to/your.log', Logger::WARNING));

Monolog is very powerful, you can implement many types of handlers, formatters, and processors. It worth having a look: https://github.com/Seldaek/monolog/blob/master/doc/02-handlers-formatters-processors.md

And finally call it like:

// add records to the log
$log->warning('Foo');
$log->error('Bar');

In order to make it global, I recommend you to add it to your dependency injector, if you have one, or use a singleton with static calls.

Let me know if you want more details about this.

PHP error_log() Function, get_server_info get_server_version info init insert_id kill more_results multi_query next_result options ping poll Send error messages to the web server's error log and to a mail account: Message is sent to PHP's system logger, using the OS' system logging mechanism or a file, depending on PHP Version: 4.0+. There are two possible values for error_log: a custom log file and the syslog. If the syslog is used, then all PHP errors will be sent directly to the default system log file—in Linux, this is typically /var/log/syslog. The more manageable method is to use a custom log file.

Equivalent of PHP error_log for info logs?, hi guys : I use error_log for my logging, but Im realizing that there must be a more idiomatic way to log application progress. is there an info_log  And it's much more than mere INFO log data - a log level often abused for spamming logs with minor operational messages of no historical value whatsoever. As the case dictates this information may be directed to a separate invocation log, or may be obtained by filtering it out of a large log recording more information.

PHP Log Files: Tips for Developers, Error Log Confguration. Error logs represent the most basic form of logging; i.e., capturing additional information and detail when problems occur. So, in an ideal​  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more Where does IIS 7.5 log errors?

PHP Logging & PHP Syslog Management, We'll show how to manage your PHP logging which is to send syslog to Rsyslog and then forward it to Loggly. for loss of business profits, business interruption, loss of business information, Note: Replace “5.x” and “7.x” with your PHP version number. Log errors to syslog (Event Log on Windows). error_log = syslog. How can I see the log generated by an application or tool? Webinoly supports the visualization of events generated by PHP, MySQL, mail, syslog, SSH and Let’s Encrypt. sudo log -php sudo log -ssh sudo log -mail sudo log -syslog sudo log -le. The -fpm and -php options are equivalent and -le is for “Let’s Encrypt”. MySQL logs

Comments
  • yes... but what if something is not an error? this seems like it isn't idiomatic. In java, we have log.info("..."), log.error("error msg") , etc, as the common idiom for logging .
  • Yes, other languages are better than php at naming conventions, etc. Since php doesn't actually have a logging system (for just normal messages) you have to create your own function to do it. The function above will add the message to the file log.txt, not to the regular php error log. So if something was an error, you would use error_log($message) and if something was NOT an error, then you would use log_message($message) and read it later at log.txt. If you want to get fancy, you can add time and file locations to the message log.