Halloween Sale 15% off Ends in See the plans
PHP

Daily Email Summary of New PHP Errors

Reads new lines in debug.log once a day and emails a short summary of fatal errors, warnings and notices with how often each occurred, so problems do not go unnoticed.

Don’t have WPIDE yet? Install it on your site

Code

$wpide_errors_to = ''; // Recipient; empty = admin email.

add_action( 'init', function () {
    if ( ! wp_next_scheduled( 'wpide_php_error_summary' ) ) {
        wp_schedule_event( time() + HOUR_IN_SECONDS, 'daily', 'wpide_php_error_summary' );
    }
} );

add_action( 'wpide_php_error_summary', function () use ( $wpide_errors_to ) {
    $file = ( defined( 'WP_DEBUG_LOG' ) && is_string( WP_DEBUG_LOG ) && '' !== WP_DEBUG_LOG ) ? WP_DEBUG_LOG : WP_CONTENT_DIR . '/debug.log';

    if ( ! is_readable( $file ) ) {
        return;
    }

    $size   = filesize( $file );
    $offset = (int) get_option( 'wpide_php_error_offset', 0 );
    if ( $offset > $size ) {
        $offset = 0; // The log was rotated or cleared.
    }

    $handle = fopen( $file, 'rb' );
    if ( ! $handle ) {
        return;
    }
    fseek( $handle, $offset );
    $chunk = (string) fread( $handle, MB_IN_BYTES );
    update_option( 'wpide_php_error_offset', ftell( $handle ), false );
    fclose( $handle );

    $counts = array();
    foreach ( preg_split( '/\R/', $chunk ) as $line ) {
        if ( preg_match( '/PHP (Fatal error|Parse error|Warning|Notice|Deprecated):\s*(.+?)( in \/.+? on line \d+)?$/', $line, $m ) ) {
            $key            = $m[1] . ': ' . wp_html_excerpt( $m[2] . ( isset( $m[3] ) ? $m[3] : '' ), 300, '…' );
            $counts[ $key ] = isset( $counts[ $key ] ) ? $counts[ $key ] + 1 : 1;
        }
    }

    if ( ! $counts ) {
        return;
    }

    arsort( $counts );
    $lines = array();
    foreach ( array_slice( $counts, 0, 30, true ) as $message => $count ) {
        $lines[] = sprintf( '%4dx  %s', $count, $message );
    }

    $to = is_email( $wpide_errors_to ) ? $wpide_errors_to : get_option( 'admin_email' );
    wp_mail( $to, sprintf( '[%s] %d distinct PHP error(s) in the last day', wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ), count( $counts ) ), implode( "\n", $lines ) );
} );

Notes

Needs WP_DEBUG_LOG enabled, reads at most 1 MB of new log per day and lists the 30 most frequent messages; set the recipient at the top.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.