Enable remote logging feature flag (#50351)

* Enable remote logging feature flag

* Do not send log in local or dev envs

* Add changelog

* Fix env check
This commit is contained in:
Chi-Hsuan Huang 2024-08-07 10:33:47 +08:00 committed by GitHub
parent a307c22d1e
commit c37795d694
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 72 additions and 5 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: update
Enable remote logging feature flag

View File

@ -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,

View File

@ -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 );
}
}

View File

@ -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