We've been working on writing unit testing for our code. One of the things I struggle with is that on our production server we're logging a message to New Relic when we run into a condition that isn't planned for (in the future we'll have unit testing for this but it's a large code base and it needs a lot of work).

The problem that I've been running into is that when we do this we want these messages displayed on the command line when we run the unit tests and not send to New Relic. The code below allows us to do this:

<?php
class Application_Model_Error{
    public static function log($message){
        if(php_sapi_name() === 'cli'){
            echo PHP_EOL, PHP_EOL;
            echo '*****************************', PHP_EOL;
            echo $message, PHP_EOL;
            echo '*****************************', PHP_EOL;
            echo PHP_EOL, PHP_EOL;
        } elseif (extension_loaded('newrelic')) {
            newrelic_notice_error($message);
        }
    }
}

The following determines if PHP is running at the command line:

if(php_sapi_name() === 'cli'){