Logger.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * This file is part of the Composer Merge plugin.
  4. *
  5. * Copyright (C) 2015 Bryan Davis, Wikimedia Foundation, and contributors
  6. *
  7. * This software may be modified and distributed under the terms of the MIT
  8. * license. See the LICENSE file for details.
  9. */
  10. namespace Wikimedia\Composer;
  11. use Composer\IO\IOInterface;
  12. /**
  13. * Simple logging wrapper for Composer\IO\IOInterface
  14. *
  15. * @author Bryan Davis <bd808@bd808.com>
  16. */
  17. class Logger
  18. {
  19. /**
  20. * @var string $name
  21. */
  22. protected $name;
  23. /**
  24. * @var IOInterface $inputOutput
  25. */
  26. protected $inputOutput;
  27. /**
  28. * @param string $name
  29. * @param IOInterface $io
  30. */
  31. public function __construct($name, IOInterface $io)
  32. {
  33. $this->name = $name;
  34. $this->inputOutput = $io;
  35. }
  36. /**
  37. * Log a debug message
  38. *
  39. * Messages will be output at the "very verbose" logging level (eg `-vv`
  40. * needed on the Composer command).
  41. *
  42. * @param string $message
  43. */
  44. public function debug($message)
  45. {
  46. if ($this->inputOutput->isVeryVerbose()) {
  47. $message = " <info>[{$this->name}]</info> {$message}";
  48. $this->log($message);
  49. }
  50. }
  51. /**
  52. * Log an informative message
  53. *
  54. * Messages will be output at the "verbose" logging level (eg `-v` needed
  55. * on the Composer command).
  56. *
  57. * @param string $message
  58. */
  59. public function info($message)
  60. {
  61. if ($this->inputOutput->isVerbose()) {
  62. $message = " <info>[{$this->name}]</info> {$message}";
  63. $this->log($message);
  64. }
  65. }
  66. /**
  67. * Log a warning message
  68. *
  69. * @param string $message
  70. */
  71. public function warning($message)
  72. {
  73. $message = " <error>[{$this->name}]</error> {$message}";
  74. $this->log($message);
  75. }
  76. /**
  77. * Write a message
  78. *
  79. * @param string $message
  80. */
  81. protected function log($message)
  82. {
  83. if (method_exists($this->inputOutput, 'writeError')) {
  84. $this->inputOutput->writeError($message);
  85. } else {
  86. // @codeCoverageIgnoreStart
  87. // Backwards compatiblity for Composer before cb336a5
  88. $this->inputOutput->write($message);
  89. // @codeCoverageIgnoreEnd
  90. }
  91. }
  92. }
  93. // vim:sw=4:ts=4:sts=4:et: