Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collect log messages in Logger instead of in context system. #3811

Merged
merged 6 commits into from
Dec 2, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Collect log messages in Logger instead of in context system.
  • Loading branch information
weitzman committed Dec 1, 2018
commit 4bd3aa37715d474e15b7f4f02fe0dadefbcd39fd
4 changes: 2 additions & 2 deletions includes/backend.inc
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ function drush_backend_output() {
}

$data['error_status'] = Runtime::exitCode();
$data['log'] = drush_get_log(); // Append logging information
$data['log'] = Drush::logger()->getLogs(); // Append logging information
// The error log is a more specific version of the log, and may be used by calling
// scripts to check for specific errors that have occurred.
$data['error_log'] = drush_get_error_log();
$data['error_log'] = Drush::logger()->getErrorLogs();
// If there is a @self record, then include it in the result
$self_record = drush_sitealias_get_record('@self');
if (!empty($self_record)) {
Expand Down
75 changes: 59 additions & 16 deletions src/Log/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,54 @@

class Logger extends RoboLogger
{
/**
* Array of logs. For use by backend responses.
*
* @var array
*
* @deprecated
*/
protected $logs = [];

/**
* Array of error logs. For use by backend responses.
*
* @var array
*
* @deprecated
*/
protected $logs_error = [];

public function __construct(OutputInterface $output)
{
parent::__construct($output);
}

/**
* Get an array of logs for the current request.
*
* @return array
*/
public function getLogs()
{
return $this->logs;
}

/**
* Get an array of error logs for the current request.
*
* @return array
*/
public function getErrorLogs() {
return $this->logs_error;
}

public function log($level, $message, array $context = [])
{
// Convert to old $entry array for b/c calls
$entry = $context + [
'type' => $level,
'message' => StringUtils::interpolate($message, $context),
'timestamp' => microtime(true),
'memory' => memory_get_usage(),
];

// Drush\Log\Logger should take over all of the responsibilities
// of drush_log, including caching the log messages and sending
// log messages along to backend invoke.
// TODO: move these implementations inside this class.
$log =& drush_get_context('DRUSH_LOG', []);
$log[] = $entry;
$entry = $this->buildEntry($level, $message, $context);

$this->logs[] = $entry;

if ($level != LogLevel::DEBUG_NOTIFY) {
drush_backend_packet('log', $entry);
}
Expand Down Expand Up @@ -176,8 +202,25 @@ public function log($level, $message, array $context = [])

public function error($message, array $context = [])
{
$error_log =& drush_get_context('DRUSH_ERROR_LOG', []);
$error_log[$message][] = $message;
$this->logs_error[] = $this->buildEntry(LogLevel::ERROR, $message, $context);
parent::error($message, $context);
}

/**
* @param $level
* @param $message
* @param array $context
* @return array
*/
protected function buildEntry($level, $message, array $context)
{
// Convert to old $entry array for b/c calls
$entry = $context + [
'type' => $level,
'message' => StringUtils::interpolate($message, $context),
'timestamp' => microtime(true),
'memory' => memory_get_usage(),
];
return $entry;
}
}