How does one enable error logging in PHP ?

Dani AI

Generated

— useful pointers already from and . The replies show the basic runtime approach; below are the practical next steps, checks and tests that people often miss when enabling PHP error logging for real apps.

A minimal runtime example to send PHP errors to a specific file and suppress on-screen output (safer for production):

ini_set('error_log', '/var/log/php/php-errors.log');
ini_set('display_errors', '0');
ini_set('display_startup_errors', '0');

Make sure the logging flag is enabled in php.ini (or with ini_set at runtime) and that the active php.ini is the one you think it is — use phpinfo() in a web page or php --ini / php -i on the command line to confirm the "Loaded Configuration File."

Quick test and a shutdown catcher to capture fatal errors:

trigger_error('Logging test', E_USER_WARNING);

register_shutdown_function(function() {
    $err = error_get_last();
    if ($err) {
        error_log("Fatal error: " . json_encode($err));
    }
});

Troubleshooting checklist:

  • Ensure the log file exists and the webserver/PHP user can write to it (e.g. chown www-data:www-data /var/log/php/php-errors.log + conservative perms).
  • On systems with SELinux, confirm file context allows httpd/PHP write access.
  • If using php-fpm, check the pool and fpm master logs — some SAPIs redirect PHP errors to the server log rather than error_log.
  • Check webserver error logs (Apache/nginx) — they sometimes contain PHP startup errors.
  • Rotate logs (logrotate) and avoid display_errors=On in production; use a PSR-3 compatible logger (Monolog) for structured, auditable logs in larger apps.

This complements ’s runtime approach by focusing on where logs end up, how to test them, and real-world operational issues that prevent errors from being recorded.

Recommended Answers

All 2 Replies

You can do this at runtime with the ini_set() command at the top of your PHP script, or you can change the setting permanently from within the php.ini file. Via runtime, you would do it as so:

ini_set('log_errors', 1);

Then, as copied from https://www.php.net/manual/en/function.error-reporting.php

<?php

// Turn off all error reporting
error_reporting(0);

// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

// Report all errors except E_NOTICE
error_reporting(E_ALL & ~E_NOTICE);

// Report all PHP errors
error_reporting(E_ALL);

// Report all PHP errors
error_reporting(-1);

// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);

?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.