Skip to content

Commit 4a34a28

Browse files
authored
Collect log messages in Logger instead of in context system. (#3811)
1 parent 42acb4e commit 4a34a28

File tree

3 files changed

+85
-36
lines changed

3 files changed

+85
-36
lines changed

includes/backend.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,10 @@ function drush_backend_output() {
155155
}
156156

157157
$data['error_status'] = Runtime::exitCode();
158-
$data['log'] = drush_get_log(); // Append logging information
158+
$data['log'] = Drush::logger()->getLogs(); // Append logging information
159159
// The error log is a more specific version of the log, and may be used by calling
160160
// scripts to check for specific errors that have occurred.
161-
$data['error_log'] = drush_get_error_log();
161+
$data['error_log'] = Drush::logger()->getErrorLogs();
162162
// If there is a @self record, then include it in the result
163163
$self_record = drush_sitealias_get_record('@self');
164164
if (!empty($self_record)) {

includes/drush.inc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,8 @@ function drush_backend_packet_log($entry, $backend_options) {
493493
*
494494
* @return
495495
* Entire log history
496+
*
497+
* @deprecated
496498
*/
497499
function drush_get_log() {
498500
return drush_get_context('DRUSH_LOG', []);
@@ -598,6 +600,8 @@ function drush_set_error($error, $message = null, $output_label = "") {
598600
*
599601
* @return
600602
* The current aggregate error status
603+
*
604+
* @deprecated
601605
*/
602606
function drush_get_error() {
603607
return drush_get_context('DRUSH_ERROR_CODE', DRUSH_SUCCESS);
@@ -608,6 +612,8 @@ function drush_get_error() {
608612
*
609613
* @return
610614
* An associative array of error messages indexed by the type of message.
615+
*
616+
* @deprecated
611617
*/
612618
function drush_get_error_log() {
613619
return drush_get_context('DRUSH_ERROR_LOG', []);

src/Log/Logger.php

Lines changed: 77 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,71 @@
3333

3434
class Logger extends RoboLogger
3535
{
36+
/**
37+
* Array of logs. For use by backend responses.
38+
*
39+
* @var array
40+
*
41+
* @deprecated
42+
*/
43+
protected $logs = [];
44+
45+
/**
46+
* Array of error logs. For use by backend responses.
47+
*
48+
* @var array
49+
*
50+
* @deprecated
51+
*/
52+
protected $logs_error = [];
3653

3754
public function __construct(OutputInterface $output)
3855
{
3956
parent::__construct($output);
4057
}
4158

59+
/**
60+
* Get an array of logs for the current request.
61+
*
62+
* @return array
63+
*
64+
* @deprecated Used by drush_backend_output().
65+
*/
66+
public function getLogs()
67+
{
68+
return $this->logs;
69+
}
70+
71+
/**
72+
* Get an array of error logs for the current request.
73+
*
74+
* @return array
75+
*
76+
* @deprecated Used by drush_backend_output().
77+
*/
78+
public function getErrorLogs()
79+
{
80+
return $this->logs_error;
81+
}
82+
83+
/**
84+
* Empty log collections.
85+
*
86+
* @deprecated
87+
*/
88+
public function clearLogs()
89+
{
90+
$this->logs_error = $this->logs = [];
91+
}
92+
4293
public function log($level, $message, array $context = [])
4394
{
44-
// Convert to old $entry array for b/c calls
45-
$entry = $context + [
46-
'type' => $level,
47-
'message' => StringUtils::interpolate($message, $context),
48-
'timestamp' => microtime(true),
49-
'memory' => memory_get_usage(),
50-
];
95+
$entry = $this->buildEntry($level, $message, $context);
96+
97+
if (Drush::backend()) {
98+
$this->logs[] = $entry;
99+
}
51100

52-
// Drush\Log\Logger should take over all of the responsibilities
53-
// of drush_log, including caching the log messages and sending
54-
// log messages along to backend invoke.
55-
// TODO: move these implementations inside this class.
56-
$log =& drush_get_context('DRUSH_LOG', []);
57-
$log[] = $entry;
58101
if ($level != LogLevel::DEBUG_NOTIFY) {
59102
drush_backend_packet('log', $entry);
60103
}
@@ -151,33 +194,33 @@ public function log($level, $message, array $context = [])
151194
$message = $message . ' ' . $timer;
152195
}
153196

154-
/*
155-
// Drush-styled output
156-
157-
$message = $this->interpolate(
158-
$message,
159-
$this->getLogOutputStyler()->style($context)
160-
);
161-
162-
$width[0] = ($columns - 11);
163-
164-
$format = sprintf("%%-%ds%%%ds", $width[0], $width[1]);
165-
166-
// Place the status message right aligned with the top line of the error message.
167-
$message = wordwrap($message, $width[0]);
168-
$lines = explode("\n", $message);
169-
$lines[0] = sprintf($format, $lines[0], $type_msg);
170-
$message = implode("\n", $lines);
171-
$this->getErrorStreamWrapper()->writeln($message);
172-
*/
173197
// Robo-styled output
174198
parent::log($level, $message, $context);
175199
}
176200

177201
public function error($message, array $context = [])
178202
{
179-
$error_log =& drush_get_context('DRUSH_ERROR_LOG', []);
180-
$error_log[$message][] = $message;
203+
if (Drush::backend()) {
204+
$this->logs_error[] = $this->buildEntry(LogLevel::ERROR, $message, $context);
205+
}
181206
parent::error($message, $context);
182207
}
208+
209+
/**
210+
* @param $level
211+
* @param $message
212+
* @param array $context
213+
* @return array
214+
*/
215+
protected function buildEntry($level, $message, array $context)
216+
{
217+
// Convert to old $entry array for b/c calls
218+
$entry = $context + [
219+
'type' => $level,
220+
'message' => StringUtils::interpolate($message, $context),
221+
'timestamp' => microtime(true),
222+
'memory' => memory_get_usage(),
223+
];
224+
return $entry;
225+
}
183226
}

0 commit comments

Comments
 (0)