Logger.php

  1. 8.0.x lib/Drush/Log/Logger.php
  2. 7.x lib/Drush/Log/Logger.php
  3. master lib/Drush/Log/Logger.php

Contains \Drush\Log\Logger.

This is the actual Logger for Drush that is responsible for logging messages.

This logger is designed such that it can be provided to other libraries that log to a Psr\Log\LoggerInterface. As such, it takes responsibility for passing log messages to backend invoke, as necessary (c.f. drush_backend_packet()).

Drush supports all of the required log levels from Psr\Log\LogLevel, and also defines its own. See Drush\Log\LogLevel.

Those who may wish to change the way logging works in Drush should therefore NOT attempt to replace this logger with their own LoggerInterface, as it will not work. It would be okay to extend Drush\Log\Logger, or perhaps we could provide a way to set an output I/O object here, in case output redirection was the only thing that needed to be swapped out.

Namespace

Drush\Log

Classes

Namesort descending Description
Logger

File

lib/Drush/Log/Logger.php
View source
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drush\Log\Logger.
  5. *
  6. * This is the actual Logger for Drush that is responsible
  7. * for logging messages.
  8. *
  9. * This logger is designed such that it can be provided to
  10. * other libraries that log to a Psr\Log\LoggerInterface.
  11. * As such, it takes responsibility for passing log messages
  12. * to backend invoke, as necessary (c.f. drush_backend_packet()).
  13. *
  14. * Drush supports all of the required log levels from Psr\Log\LogLevel,
  15. * and also defines its own. See Drush\Log\LogLevel.
  16. *
  17. * Those who may wish to change the way logging works in Drush
  18. * should therefore NOT attempt to replace this logger with their
  19. * own LoggerInterface, as it will not work. It would be okay
  20. * to extend Drush\Log\Logger, or perhaps we could provide a way
  21. * to set an output I/O object here, in case output redirection
  22. * was the only thing that needed to be swapped out.
  23. */
  24. namespace Drush\Log;
  25. use Drush\Log\LogLevel;
  26. use Psr\Log\AbstractLogger;
  27. use Robo\Log\RoboLogger;
  28. use Symfony\Component\Console\Output\OutputInterface;
  29. class Logger extends RoboLogger {
  30. public function __construct(OutputInterface $output) {
  31. parent::__construct($output);
  32. }
  33. public function log($level, $message, array $context = array()) {
  34. // Convert to old $entry array for b/c calls
  35. $entry = $context + [
  36. 'type' => $level,
  37. 'message' => $message,
  38. 'timestamp' => microtime(TRUE),
  39. 'memory' => memory_get_usage(),
  40. ];
  41. // Drush\Log\Logger should take over all of the responsibilities
  42. // of drush_log, including caching the log messages and sending
  43. // log messages along to backend invoke.
  44. // TODO: move these implementations inside this class.
  45. $log =& drush_get_context('DRUSH_LOG', array());
  46. $log[] = $entry;
  47. if ($level != LogLevel::DEBUG_NOTIFY) {
  48. drush_backend_packet('log', $entry);
  49. }
  50. if (drush_get_context('DRUSH_NOCOLOR')) {
  51. $red = "[%s]";
  52. $yellow = "[%s]";
  53. $green = "[%s]";
  54. }
  55. else {
  56. $red = "\033[31;40m\033[1m[%s]\033[0m";
  57. $yellow = "\033[1;33;40m\033[1m[%s]\033[0m";
  58. $green = "\033[1;32;40m\033[1m[%s]\033[0m";
  59. }
  60. $verbose = drush_get_context('DRUSH_VERBOSE');
  61. $debug = drush_get_context('DRUSH_DEBUG');
  62. $debugnotify = drush_get_context('DRUSH_DEBUG_NOTIFY');
  63. // Save the original level in the context name, then
  64. // map it to a standard log level.
  65. $context['name'] = $level;
  66. switch ($level) {
  67. case LogLevel::WARNING :
  68. case LogLevel::CANCEL :
  69. $type_msg = sprintf($yellow, $level);
  70. $level = LogLevel::WARNING;
  71. break;
  72. case 'failed' : // Obsolete; only here in case contrib is using it.
  73. case LogLevel::EMERGENCY : // Not used by Drush
  74. case LogLevel::ALERT : // Not used by Drush
  75. case LogLevel::ERROR :
  76. $type_msg = sprintf($red, $level);
  77. break;
  78. case LogLevel::OK :
  79. case 'completed' : // Obsolete; only here in case contrib is using it.
  80. case LogLevel::SUCCESS :
  81. case 'status': // Obsolete; only here in case contrib is using it.
  82. // In quiet mode, suppress progress messages
  83. if (drush_get_context('DRUSH_QUIET')) {
  84. return TRUE;
  85. }
  86. $type_msg = sprintf($green, $level);
  87. $level = LogLevel::NOTICE;
  88. break;
  89. case LogLevel::NOTICE :
  90. $type_msg = sprintf("[%s]", $level);
  91. break;
  92. case 'message' : // Obsolete; only here in case contrib is using it.
  93. case LogLevel::INFO :
  94. if (!$verbose) {
  95. // print nothing. exit cleanly.
  96. return TRUE;
  97. }
  98. $type_msg = sprintf("[%s]", $level);
  99. $level = LogLevel::INFO;
  100. break;
  101. case LogLevel::DEBUG_NOTIFY :
  102. $level = LogLevel::DEBUG; // Report 'debug', handle like 'preflight'
  103. case LogLevel::PREFLIGHT :
  104. if (!$debugnotify) {
  105. // print nothing unless --debug AND --verbose. exit cleanly.
  106. return TRUE;
  107. }
  108. $type_msg = sprintf("[%s]", $level);
  109. $level = LogLevel::DEBUG;
  110. break;
  111. case LogLevel::BOOTSTRAP :
  112. case LogLevel::DEBUG :
  113. default :
  114. if (!$debug) {
  115. // print nothing. exit cleanly.
  116. return TRUE;
  117. }
  118. $type_msg = sprintf("[%s]", $level);
  119. $level = LogLevel::DEBUG;
  120. break;
  121. }
  122. // When running in backend mode, log messages are not displayed, as they will
  123. // be returned in the JSON encoded associative array.
  124. if (drush_get_context('DRUSH_BACKEND')) {
  125. return;
  126. }
  127. $columns = drush_get_context('DRUSH_COLUMNS', 80);
  128. $width[1] = 11;
  129. // Append timer and memory values.
  130. if ($debug) {
  131. $timer = sprintf('[%s sec, %s]', round($entry['timestamp']-DRUSH_REQUEST_TIME, 2), drush_format_size($entry['memory']));
  132. $entry['message'] = $entry['message'] . ' ' . $timer;
  133. }
  134. $message = $entry['message'];
  135. /*
  136. // Drush-styled output
  137. $message = $this->interpolate(
  138. $message,
  139. $this->getLogOutputStyler()->style($context)
  140. );
  141. $width[0] = ($columns - 11);
  142. $format = sprintf("%%-%ds%%%ds", $width[0], $width[1]);
  143. // Place the status message right aligned with the top line of the error message.
  144. $message = wordwrap($message, $width[0]);
  145. $lines = explode("\n", $message);
  146. $lines[0] = sprintf($format, $lines[0], $type_msg);
  147. $message = implode("\n", $lines);
  148. $this->getErrorStreamWrapper()->writeln($message);
  149. */
  150. // Robo-styled output
  151. parent::log($level, $message, $context);
  152. }
  153. }