Checking to see if PHP is Running at the Command Line
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'){
Scott Keck-Warren
Scott is the Director of Technology at WeCare Connect where he strives to provide solutions for his customers needs. He's the father of two and can be found most weekends working on projects around the house with his loving partner.
Top Posts
- Working With Soft Deletes in Laravel (By Example)
- Fixing CMake was unable to find a build program corresponding to "Unix Makefiles"
- Upgrading to Laravel 8.x
- Get The Count of the Number of Users in an AD Group
- Multiple Vagrant VMs in One Vagrantfile
- Fixing the "this is larger than GitHub's recommended maximum file size of 50.00 MB" error
- Changing the Directory Vagrant Stores the VMs In
- Accepting Android SDK Licenses From The OSX Command Line
- Fixing the 'Target class [config] does not exist' Error
- Using Rectangle to Manage MacOS Windows