Why won't PHP CLI log errors?
When you configure your web server to use a dedicated error log, diagnosing issues is straightforward. You check /var/log/php_errors.log or Apache's access/error logs, and there it is: a stack trace, a notice about a missing key, or a fatal exception. However, when you switch to the command line interface (CLI) to run scripts via php artisan, cron jobs, or direct execution, those same errors often vanish into the void unless you explicitly handle them. The source material points out a common frustration: even with log_errors = On and error_log set in /etc/php5/cli/php.ini, no file is created.
This isn't because the CLI version of PHP lacks logging capabilities; it's because the default output stream for the CLI SAPI differs from the web server SAPI. In a standard web environment, PHP initializes an error log handler that writes to the configured file path. In the CLI context, the default behavior is to send errors directly to STDERR. If you haven't explicitly told PHP to redirect those errors to a file within your script or if the runtime environment suppresses the output, the logs simply never appear.
The configuration directive error_log in php.ini is generally respected by both SAPIs, but there are specific scenarios where it fails silently or behaves unexpectedly on the command line. One frequent pitfall involves how PHP determines whether to use the file handler versus the standard stream. If your php.ini has display_errors = On, you might see errors printed to the terminal, but if log_errors is also On, PHP attempts to write to both. However, depending on the PHP version and the specific SAPI implementation details, the CLI sometimes prioritizes the immediate stream output over the file handler unless the file path is explicitly validated and writable at runtime.
Another critical factor is the permissions of the target directory. Even if your php.ini is correct, if the user running the CLI command (often root, nobody, or a specific web user like www-data) lacks write permissions to /var/log/php_errors.log or its parent directory, PHP will fail silently or throw an error that gets lost if display_errors isn't configured to catch it. You must ensure the directory exists and is writable by the executing process before relying on file logging.
To force reliable logging in CLI environments without modifying php.ini globally, you can manipulate the error stream within your script. Adding this line at the very top of your PHP file ensures that all errors are redirected to the specified log path:
ini_set('display_errors', 0);
ini_set('log_errors', 1);
ini_set('error_log', '/var/log/php_cli_errors.log');
This approach overrides any conflicting settings and guarantees that STDERR is no longer the sole destination for fatal errors. Additionally, you can use a custom error handler to capture exceptions before they terminate execution:
set_error_handler(function($errno, $errstr, $errfile, $errline) {
file_put_contents('/var/log/php_cli_errors.log', "[$datetime] $errstr\n", FILE_APPEND);
});
Note: Ensure you define the $datetime variable appropriately based on your needs.
There is also a nuance regarding error_reporting. If E_ALL isn't set, minor warnings might not trigger the logging mechanism depending on how the error handler was initialized. Always verify that your error reporting level includes the specific errors you intend to log.
For production environments running scheduled tasks or CLI scripts, relying solely on terminal output is a liability. You need persistent logs for auditing and debugging without manual intervention. By explicitly setting the error_log path and ensuring write permissions are correct, you align your CLI behavior with your web server expectations. If you are using a framework like Laravel, similar logic applies: ensure your .env file points to the correct log channel or directory, as framework-specific configuration can sometimes override system-wide php.ini settings if not carefully managed.
Ultimately, the silence of your CLI logs is rarely a feature; it's a configuration gap. Addressing stream redirection and permission checks ensures that every critical failure is recorded, making troubleshooting significantly more efficient.