all posts
2026-01-08·3 min read

New Relic: Supressing New Relic PHP Warnings

LogsNew RelicPluginWordPress

1. Objective

To reduce “alert fatigue” and improve the accuracy of our New Relic APM “Error Rate” metric.

On certain managed hosts, it might not be possible to configure the New Relic agent running on the server and disable warnings in the default way. These APM configurations can report a high volume of non-critical PHP Warnings, depending on the site, Notices, and Deprecated errors (e.g., E_WARNING, E_NOTICE). These can inflate the error rate, making it impossible to distinguish between a noisy log and a critical site outage.

The Goal:

  1. Stop sending PHP Warnings/notices to New Relic (clearing the dashboard).
  2. Continue writing these errors to the server’s debug.log (preserving data for debugging).
  3. Ensure valid Transactions are still recorded even if a warning occurs.

2. The Solution

We utilise a concept WordPress Must-Use (MU) Plugin named nr-warning-suppress.php.

Why an MU-Plugin?

Standard methods (like php.ini or .user.ini) are restricted on our setup or do not offer the granular control we need. An MU-Plugin loads before standard plugins and themes, allowing us to register a custom error handler that intercepts errors globally across the application.

How It Works

The script uses a “catch and release” logic:

  1. Intercept: It listens for specific PHP error types (E_WARNING, E_NOTICE, etc.).
  2. Log: It manually writes the error details to the local debug.log file, prefixed with [NR-SUPPRESSED].
  3. Suppress: It returns true to PHP. This tells the PHP engine “I have handled this error, do not pass it to the default reporter.” Consequently, New Relic never sees the error.
  4. Passthrough: For critical errors (like E_ERROR crashes), it returns false, allowing them to bubble up to New Relic as normal.

3. Implementation Guide

Prerequisites

  • SSH to the environment.
  • Got to the wp-content/mu-plugins/ directory.

Step-by-Step

  1. Create a file named nr-warning-suppress.php.
  2. Paste the code below into the file.
  3. Upload the file to the /wp-content/mu-plugins/ directory on the server.
  4. No activation is required; MU-plugins run automatically.

The Code

<?php
/*
Plugin Name: New Relic Warning Suppressor
Description: Captures Warnings, Notices, and Deprecated errors to keep New Relic APM clean, while preserving them in debug.log.
Version: 1.0
*/

set_error_handler('nr_suppress_warnings_handler');

function nr_suppress_warnings_handler($errno, $errstr, $errfile, $errline) {
    static $is_handling = false;
    if ($is_handling) {
        return false;
    }

    // DEFINE TYPES TO HIDE FROM NEW RELIC
    $ignored_types = [
        E_WARNING,
        E_NOTICE,
        E_USER_WARNING,
        E_USER_NOTICE,
        E_DEPRECATED,
        E_USER_DEPRECATED
    ];

    // CHECK IF CURRENT ERROR IS IN OUR LIST
    if (in_array($errno, $ignored_types, true)) {
        $is_handling = true; // Lock to prevent loops

        // Get a readable name for the log file
        $type_str = 'Error';
        switch ($errno) {
            case E_WARNING: $type_str = 'Warning'; break;
            case E_NOTICE: $type_str = 'Notice'; break;
            case E_DEPRECATED: $type_str = 'Deprecated'; break;
            case E_USER_WARNING: $type_str = 'User Warning'; break;
            case E_USER_NOTICE: $type_str = 'User Notice'; break;
            case E_USER_DEPRECATED: $type_str = 'User Deprecated'; break;
        }

        // Create the log message
        // [NR-SUPPRESSED] tag helps us grep/filter these lines later
        $log_message = "[NR-SUPPRESSED] PHP $type_str: $errstr in $errfile on line $errline";

        // Write to debug.log (using @ to silence legitimate logging errors)
        @error_log($log_message);

        $is_handling = false; // Unlock

        // Return TRUE: Tells PHP "I handled it, do not send to New Relic"
        return true;
    }

    // Return FALSE: Let legitimate crashes (E_ERROR, etc.) bubble up to New Relic
    return false;
}

4. Verification & Testing

To confirm the solution is working:

  1. Generate a Warning: (Temporarily) add a line of code that triggers a warning, or visit a page known to generate one.
    • Example Trigger: fopen("non_existent_file.txt", "r");
    • Example of a file you can add to the root of the environment to generate warnings:
<?php
require( './wp-load.php' ); // Loads WordPress environment

// Trigger Warning
echo "Triggering Warning...";
$file = fopen("non_existent_file_123.txt", "r");
echo "Done.";
?>
  1. Check New Relic:
    • Go to APM > Events > Errors.
    • Ensure the specific warning is NOT listed.
    • Ensure the Transaction for that page load IS recorded.
  1. Check Server Logs:
    • Connect via SSH and run phpfatal -v to view the warnings being generated.
  1. Check User Portal:
    • Go to the environment and then the logs to view the warnings being displayed.

Found this useful?

There's more where that came from — explore the rest of the documentation, experiments, and production work.

Back to the index