diff --git a/plugins/woocommerce/changelog/update-enable-remote-logging b/plugins/woocommerce/changelog/update-enable-remote-logging new file mode 100644 index 00000000000..292cb46acfa --- /dev/null +++ b/plugins/woocommerce/changelog/update-enable-remote-logging @@ -0,0 +1,4 @@ +Significance: minor +Type: update + +Enable remote logging feature flag diff --git a/plugins/woocommerce/src/Internal/Features/FeaturesController.php b/plugins/woocommerce/src/Internal/Features/FeaturesController.php index c332e769f65..9aec6dcaa11 100644 --- a/plugins/woocommerce/src/Internal/Features/FeaturesController.php +++ b/plugins/woocommerce/src/Internal/Features/FeaturesController.php @@ -253,7 +253,7 @@ class FeaturesController { 'Enable this feature to log errors and related data to Automattic servers for debugging purposes and to improve WooCommerce', 'woocommerce' ), - 'enabled_by_default' => false, + 'enabled_by_default' => true, 'disable_ui' => true, 'is_legacy' => false, 'is_experimental' => true, diff --git a/plugins/woocommerce/src/Internal/Logging/RemoteLogger.php b/plugins/woocommerce/src/Internal/Logging/RemoteLogger.php index f3945c8b29d..cb726177681 100644 --- a/plugins/woocommerce/src/Internal/Logging/RemoteLogger.php +++ b/plugins/woocommerce/src/Internal/Logging/RemoteLogger.php @@ -89,6 +89,10 @@ class RemoteLogger extends \WC_Log_Handler { WC_Rate_Limiter::set_rate_limit( self::RATE_LIMIT_ID, self::RATE_LIMIT_DELAY ); + if ( $this->is_dev_or_local_environment() ) { + return false; + } + $response = wp_safe_remote_post( self::LOG_ENDPOINT, array( @@ -407,4 +411,15 @@ class RemoteLogger extends \WC_Log_Handler { return implode( "\n", $sanitized_trace ); } + + /** + * Check if the current environment is development or local. + * + * Creates a helper method so we can easily mock this in tests. + * + * @return bool + */ + protected function is_dev_or_local_environment() { + return in_array( wp_get_environment_type(), array( 'development', 'local' ), true ); + } } diff --git a/plugins/woocommerce/tests/php/src/Internal/Logging/RemoteLoggerTest.php b/plugins/woocommerce/tests/php/src/Internal/Logging/RemoteLoggerTest.php index 407dd08f72d..247be3b62f0 100644 --- a/plugins/woocommerce/tests/php/src/Internal/Logging/RemoteLoggerTest.php +++ b/plugins/woocommerce/tests/php/src/Internal/Logging/RemoteLoggerTest.php @@ -386,14 +386,30 @@ class RemoteLoggerTest extends \WC_Unit_Test_Case { $this->assertFalse( $this->sut->handle( time(), 'error', 'Test message', array() ) ); } + /** + * @testdox Test handle() method does not send logs in dev environment + */ + public function test_handle_does_not_send_logs_in_dev_environment() { + $this->sut = $this->getMockBuilder( RemoteLoggerWithEnvironmentOverride::class ) + ->setMethods( array( 'is_remote_logging_allowed' ) ) + ->getMock(); + + $this->sut->set_is_dev_or_local( true ); + $this->sut->method( 'is_remote_logging_allowed' )->willReturn( true ); + $result = $this->sut->handle( time(), 'error', 'Test message', array() ); + + $this->assertFalse( $result, 'Handle should return false in dev environment' ); + } + /** * @testdox Test handle() method successfully sends log. */ public function test_handle_successful() { - $this->sut = $this->getMockBuilder( RemoteLogger::class ) - ->onlyMethods( array( 'is_remote_logging_allowed' ) ) - ->getMock(); + $this->sut = $this->getMockBuilder( RemoteLoggerWithEnvironmentOverride::class ) + ->setMethods( array( 'is_remote_logging_allowed' ) ) + ->getMock(); + $this->sut->set_is_dev_or_local( false ); $this->sut->method( 'is_remote_logging_allowed' )->willReturn( true ); // Mock wp_safe_remote_post using pre_http_request filter. @@ -427,11 +443,12 @@ class RemoteLoggerTest extends \WC_Unit_Test_Case { $mock_local_logger->expects( $this->once() ) ->method( 'error' ); - $this->sut = $this->getMockBuilder( RemoteLogger::class ) + $this->sut = $this->getMockBuilder( RemoteLoggerWithEnvironmentOverride::class ) ->setConstructorArgs( array( $mock_local_logger ) ) ->onlyMethods( array( 'is_remote_logging_allowed' ) ) ->getMock(); + $this->sut->set_is_dev_or_local( false ); $this->sut->method( 'is_remote_logging_allowed' )->willReturn( true ); // Mock wp_safe_remote_post to throw an exception using pre_http_request filter. @@ -551,3 +568,34 @@ class RemoteLoggerTest extends \WC_Unit_Test_Case { return $method->invokeArgs( $obj, $parameters ); } } + + +//phpcs:disable Generic.Files.OneObjectStructurePerFile.MultipleFound, Squiz.Classes.ClassFileName.NoMatch +/** + * Mock class that extends RemoteLogger to allow overriding is_dev_or_local_environment. + */ +class RemoteLoggerWithEnvironmentOverride extends RemoteLogger { + /** + * The is_dev_or_local value. + * + * @var bool + */ + private $is_dev_or_local = false; + + /** + * Set the is_dev_or_local value. + * + * @param bool $value The value to set. + */ + public function set_is_dev_or_local( $value ) { + $this->is_dev_or_local = $value; + } + + /** + * @inheritDoc + */ + protected function is_dev_or_local_environment() { + return $this->is_dev_or_local; + } +} +//phpcs:enable Generic.Files.OneObjectStructurePerFile.MultipleFound, Squiz.Classes.ClassFileName.NoMatch