Skip to content

Commit

Permalink
Merge pull request #5549 from wp-cli/make-logger-accessible
Browse files Browse the repository at this point in the history
Make logger accessible and add colorization control to `Quiet` logger
  • Loading branch information
schlessera committed Jul 26, 2021
2 parents 1e2aa11 + f854c96 commit ae8be15
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 30 deletions.
13 changes: 10 additions & 3 deletions php/WP_CLI/Loggers/Quiet.php
Expand Up @@ -9,6 +9,13 @@
*/
class Quiet extends Base {

/**
* @param bool $in_color Whether or not to Colorize strings.
*/
public function __construct( $in_color = false ) {
$this->in_color = $in_color;
}

/**
* Informational messages aren't logged.
*
Expand Down Expand Up @@ -42,7 +49,7 @@ public function warning( $message ) {
* @param string $message Message to write.
*/
public function error( $message ) {
$this->write( STDERR, WP_CLI::colorize( "%RError:%n $message\n" ) );
$this->_line( $message, 'Error', '%R', STDERR );
}

/**
Expand All @@ -53,7 +60,7 @@ public function error( $message ) {
public function error_multi_line( $message_lines ) {
$message = implode( "\n", $message_lines );

$this->write( STDERR, WP_CLI::colorize( "%RError:%n\n$message\n" ) );
$this->write( STDERR, WP_CLI::colorize( "%R---------%n\n\n" ) );
$this->_line( $message, 'Error', '%R', STDERR );
$this->_line( '', '---------', '%R', STDERR );
}
}
2 changes: 1 addition & 1 deletion php/WP_CLI/Runner.php
Expand Up @@ -862,7 +862,7 @@ public function init_colorization() {

public function init_logger() {
if ( $this->config['quiet'] ) {
$logger = new Loggers\Quiet();
$logger = new Loggers\Quiet( $this->in_color() );
} else {
$logger = new Loggers\Regular( $this->in_color() );
}
Expand Down
11 changes: 10 additions & 1 deletion php/class-wp-cli.php
Expand Up @@ -38,12 +38,21 @@ class WP_CLI {
/**
* Set the logger instance.
*
* @param object $logger
* @param object $logger Logger instance to set.
*/
public static function set_logger( $logger ) {
self::$logger = $logger;
}

/**
* Get the logger instance.
*
* @return object $logger Logger instance.
*/
public static function get_logger() {
return self::$logger;
}

/**
* Get the Configurator instance
*
Expand Down
8 changes: 3 additions & 5 deletions tests/test-extractor.php
@@ -1,6 +1,7 @@
<?php

use WP_CLI\Extractor;
use WP_CLI\Loggers;
use WP_CLI\Utils;

class Extractor_Test extends PHPUnit_Framework_TestCase {
Expand All @@ -27,12 +28,9 @@ class Extractor_Test extends PHPUnit_Framework_TestCase {
public function setUp() {
parent::setUp();

// Save and set logger.
$class_wp_cli_logger = new \ReflectionProperty( 'WP_CLI', 'logger' );
$class_wp_cli_logger->setAccessible( true );
self::$prev_logger = $class_wp_cli_logger->getValue();
self::$prev_logger = WP_CLI::get_logger();

self::$logger = new \WP_CLI\Loggers\Execution();
self::$logger = new Loggers\Execution();
WP_CLI::set_logger( self::$logger );

// Remove any failed tests detritus.
Expand Down
9 changes: 4 additions & 5 deletions tests/test-file-cache.php
@@ -1,6 +1,7 @@
<?php

use WP_CLI\FileCache;
use WP_CLI\Loggers;
use WP_CLI\Utils;

require_once dirname( __DIR__ ) . '/php/class-wp-cli.php';
Expand Down Expand Up @@ -32,11 +33,9 @@ public function testGetRoot() {
}

public function test_ensure_dir_exists() {
$class_wp_cli_logger = new ReflectionProperty( 'WP_CLI', 'logger' );
$class_wp_cli_logger->setAccessible( true );
$prev_logger = $class_wp_cli_logger->getValue();
$prev_logger = WP_CLI::get_logger();

$logger = new WP_CLI\Loggers\Execution();
$logger = new Loggers\Execution();
WP_CLI::set_logger( $logger );

$max_size = 32;
Expand Down Expand Up @@ -79,7 +78,7 @@ public function test_ensure_dir_exists() {
rmdir( $cache_dir . '/test1' );
unlink( $cache_dir . '/test2' );
rmdir( $cache_dir );
$class_wp_cli_logger->setValue( $prev_logger );
WP_CLI::set_logger( $prev_logger );
}

public function test_export() {
Expand Down
23 changes: 8 additions & 15 deletions tests/test-utils.php
Expand Up @@ -418,14 +418,12 @@ public function testGetTempDir() {

public function testHttpRequestBadAddress() {
// Save WP_CLI state.
$class_wp_cli_logger = new \ReflectionProperty( 'WP_CLI', 'logger' );
$class_wp_cli_logger->setAccessible( true );
$class_wp_cli_capture_exit = new \ReflectionProperty( 'WP_CLI', 'capture_exit' );
$class_wp_cli_capture_exit->setAccessible( true );

$prev_logger = $class_wp_cli_logger->getValue();
$prev_capture_exit = $class_wp_cli_capture_exit->getValue();

$prev_logger = WP_CLI::get_logger();

// Enable exit exception.
$class_wp_cli_capture_exit->setValue( true );

Expand All @@ -445,8 +443,8 @@ public function testHttpRequestBadAddress() {
$this->assertTrue( 0 === strpos( $logger->stderr, 'Error: Failed to get url' ) );

// Restore.
$class_wp_cli_logger->setValue( $prev_logger );
$class_wp_cli_capture_exit->setValue( $prev_capture_exit );
WP_CLI::set_logger( $prev_logger );
}

public function dataHttpRequestBadCAcert() {
Expand Down Expand Up @@ -498,18 +496,15 @@ public function testHttpRequestBadCAcert( $additional_options, $exception, $exce
$this->expectExceptionMessage( $exception_message );
} else {
// Save WP_CLI state.
$class_wp_cli_logger = new \ReflectionProperty( 'WP_CLI', 'logger' );
$class_wp_cli_logger->setAccessible( true );

$prev_logger = $class_wp_cli_logger->getValue();
$prev_logger = WP_CLI::get_logger();
$logger = new Loggers\Execution();
WP_CLI::set_logger( $logger );
}

Utils\http_request( 'GET', 'https://example.com', null, [], $options );

// Restore.
$class_wp_cli_logger->setValue( $prev_logger );
WP_CLI::set_logger( $prev_logger );

$this->assertTrue( empty( $logger->stdout ) );
$this->assertNotFalse( strpos( $logger->stderr, $exception_message ) );
Expand Down Expand Up @@ -644,14 +639,12 @@ public function dataExpandGlobs() {
*/
public function testReportBatchOperationResults( $stdout, $stderr, $noun, $verb, $total, $successes, $failures, $skips ) {
// Save WP_CLI state.
$class_wp_cli_logger = new \ReflectionProperty( 'WP_CLI', 'logger' );
$class_wp_cli_logger->setAccessible( true );
$class_wp_cli_capture_exit = new \ReflectionProperty( 'WP_CLI', 'capture_exit' );
$class_wp_cli_capture_exit->setAccessible( true );

$prev_logger = $class_wp_cli_logger->getValue();
$prev_capture_exit = $class_wp_cli_capture_exit->getValue();

$prev_logger = WP_CLI::get_logger();

// Enable exit exception.
$class_wp_cli_capture_exit->setValue( true );

Expand All @@ -669,8 +662,8 @@ public function testReportBatchOperationResults( $stdout, $stderr, $noun, $verb,
$this->assertSame( $stderr, $logger->stderr );

// Restore.
$class_wp_cli_logger->setValue( $prev_logger );
$class_wp_cli_capture_exit->setValue( $prev_capture_exit );
WP_CLI::set_logger( $prev_logger );
}

public function dataReportBatchOperationResults() {
Expand Down

0 comments on commit ae8be15

Please sign in to comment.