class UpdateLogger in Update helper 2.x
Same name and namespace in other branches
- 8 src/UpdateLogger.php \Drupal\update_helper\UpdateLogger
Helper service for logging in update hooks provided by update helper.
It provides output of logs to HTML, when update is executed over update.php. And it also provides output of logs for Drush command, when update is executed over drush command.
@package Drupal\update_helper
Hierarchy
- class \Drupal\update_helper\UpdateLogger extends \Psr\Log\AbstractLogger
Expanded class hierarchy of UpdateLogger
1 string reference to 'UpdateLogger'
1 service uses UpdateLogger
File
- src/
UpdateLogger.php, line 19
Namespace
Drupal\update_helperView source
class UpdateLogger extends AbstractLogger {
/**
* Container for logs.
*
* @var array
*/
protected $logs = [];
/**
* The console logger.
*
* @var \Psr\Log\LoggerInterface
*/
protected $cliLogger;
/**
* The console output.
*
* @var string
*/
protected $cliOutput = 'php://stderr';
/**
* {@inheritdoc}
*/
public function log($level, $message, array $context = []) {
$this->logs[] = [
$level,
$message,
$context,
];
}
/**
* Clear logs and returns currenlty collected logs.
*
* @return array
* Returns collected logs, since last clear.
*/
protected function cleanLogs() {
$logs = $this->logs;
$this->logs = [];
return $logs;
}
/**
* Output logs in format suitable for HTML and clear logs too.
*
* @return string
* Returns HTML.
*/
protected function outputHtml() {
$full_log = '';
$current_logs = $this
->cleanLogs();
foreach ($current_logs as $log_entry) {
$full_log .= $log_entry[1] . '<br /><br />';
}
return $full_log;
}
/**
* Returns console logger.
*
* @return \Psr\Log\LoggerInterface
* Returns console logger.
*/
protected function getCliLogger() {
if (empty($this->cliLogger)) {
$this->cliLogger = new ConsoleLogger(new StreamOutput(fopen($this->cliOutput, 'w'), OutputInterface::VERBOSITY_DEBUG));
}
return $this->cliLogger;
}
/**
* Output logs in format suitable for console command and clear logs too.
*/
protected function outputCli() {
$console_logger = $this
->getCliLogger();
foreach ($this
->cleanLogs() as $log_entry) {
$console_logger
->log($log_entry[0], $log_entry[1]);
}
}
/**
* Output log result, depending on channel used and clean log.
*
* @return string
* Returns HTML string in case of non console execution.
*/
public function output() {
if (PHP_SAPI === 'cli') {
$this
->outputCli();
return '';
}
return $this
->outputHtml();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
UpdateLogger:: |
protected | property | The console logger. | |
UpdateLogger:: |
protected | property | The console output. | |
UpdateLogger:: |
protected | property | Container for logs. | |
UpdateLogger:: |
protected | function | Clear logs and returns currenlty collected logs. | |
UpdateLogger:: |
protected | function | Returns console logger. | |
UpdateLogger:: |
public | function | ||
UpdateLogger:: |
public | function | Output log result, depending on channel used and clean log. | |
UpdateLogger:: |
protected | function | Output logs in format suitable for console command and clear logs too. | |
UpdateLogger:: |
protected | function | Output logs in format suitable for HTML and clear logs too. |