• Blogs (9)
    • 📱 236 - 992 - 3846

      đź“§ jxjwilliam@gmail.com

    • Version: ‍🚀 1.1.0
  • PHP: Project logs setting other than webserver's

    Blogs20142014-02-23


    PHP: Project logs setting other than webserver’s

    It is important to configure PHP’s error reporting settings depending on which phase of development. Generally, it is a good idea to see all warnings and errors in the web browser during the initial phase; and later, once the site has launched, send error messages to a log file so that visitors do not see them.

    1. Error Reporting for Development

    During development, you want to display all errors and warnings to the browser.

    // Report all PHP errors
    ini_set('error_reporting', E_ALL);
    // Set the display_errors directive to On
    ini_set('display_errors', 1);

    2. Error Reporting for Production

    In production, you may want to lower the error reporting level and not display errors to the browser.

    // Report simple running errors
    ini_set('error_reporting', E_ALL ^ E_NOTICE);
    // Set the display_errors directive to Off
    ini_set('display_errors', 0);
    // Log errors to the web server's error log
    ini_set('log_errors', 1);
    ini_set('error_log', LOGDIR.'/error_log'); 

    3. Logging errors

    You can use the PHP function error_log() to send errors to your own log file or an e-mail address:

    // Destinations
    define("ADMIN_EMAIL", "nobody@site.com");
    define("LOG_FILE", "/home/log/errors.log");
    // Destination types
    define("DEST_EMAIL", "1");
    define("DEST_LOGFILE", "3");
    /* Examples */
    // Send an e-mail to the administrator
    error_log("Fix me!", DEST_EMAIL, ADMIN_EMAIL);
    // Write the error to our log file
    error_log("Error", DEST_LOGFILE, LOG_FILE);