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. class Logger extends AbstractLogger {
  28. public function log($level, $message, array $context = array()) {
  29. // Convert to old $entry array for b/c calls
  30. $entry = $context;
  31. $entry['type'] = $level;
  32. $entry['message'] = $message;
  33. // Drush\Log\Logger should take over all of the responsibilities
  34. // of drush_log, including caching the log messages and sending
  35. // log messages along to backend invoke.
  36. // TODO: move these implementations inside this class.
  37. $log =& drush_get_context('DRUSH_LOG', array());
  38. $log[] = $entry;
  39. drush_backend_packet('log', $entry);
  40. if (drush_get_context('DRUSH_NOCOLOR')) {
  41. $red = "[%s]";
  42. $yellow = "[%s]";
  43. $green = "[%s]";
  44. }
  45. else {
  46. $red = "\033[31;40m\033[1m[%s]\033[0m";
  47. $yellow = "\033[1;33;40m\033[1m[%s]\033[0m";
  48. $green = "\033[1;32;40m\033[1m[%s]\033[0m";
  49. }
  50. $verbose = drush_get_context('DRUSH_VERBOSE');
  51. $debug = drush_get_context('DRUSH_DEBUG');
  52. switch ($level) {
  53. case LogLevel::WARNING :
  54. case 'cancel' :
  55. $type_msg = sprintf($yellow, $level);
  56. break;
  57. case 'failed' : // Obsolete; only here in case contrib is using it.
  58. case 'error' :
  59. $type_msg = sprintf($red, $level);
  60. break;
  61. case 'ok' :
  62. case 'completed' : // Obsolete; only here in case contrib is using it.
  63. case 'success' :
  64. case 'status': // Obsolete; only here in case contrib is using it.
  65. // In quiet mode, suppress progress messages
  66. if (drush_get_context('DRUSH_QUIET')) {
  67. return TRUE;
  68. }
  69. $type_msg = sprintf($green, $level);
  70. break;
  71. case 'notice' :
  72. case 'message' : // Obsolete; only here in case contrib is using it.
  73. case 'info' :
  74. if (!$verbose) {
  75. // print nothing. exit cleanly.
  76. return TRUE;
  77. }
  78. $type_msg = sprintf("[%s]", $level);
  79. break;
  80. default :
  81. if (!$debug) {
  82. // print nothing. exit cleanly.
  83. return TRUE;
  84. }
  85. $type_msg = sprintf("[%s]", $level);
  86. break;
  87. }
  88. // When running in backend mode, log messages are not displayed, as they will
  89. // be returned in the JSON encoded associative array.
  90. if (drush_get_context('DRUSH_BACKEND')) {
  91. return;
  92. }
  93. $columns = drush_get_context('DRUSH_COLUMNS', 80);
  94. $width[1] = 11;
  95. // Append timer and memory values.
  96. if ($debug) {
  97. $timer = sprintf('[%s sec, %s]', round($entry['timestamp']-DRUSH_REQUEST_TIME, 2), drush_format_size($entry['memory']));
  98. $entry['message'] = $entry['message'] . ' ' . $timer;
  99. }
  100. $width[0] = ($columns - 11);
  101. $format = sprintf("%%-%ds%%%ds", $width[0], $width[1]);
  102. // Place the status message right aligned with the top line of the error message.
  103. $message = wordwrap($entry['message'], $width[0]);
  104. $lines = explode("\n", $message);
  105. $lines[0] = sprintf($format, $lines[0], $type_msg);
  106. $message = implode("\n", $lines);
  107. drush_print($message, 0, STDERR);
  108. }
  109. }