Logger.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace PhpXmlRpc\Helper;
  3. /**
  4. * @todo implement an interface
  5. * @todo make constructor private to force users to go through `instance()` ?
  6. */
  7. class Logger
  8. {
  9. protected static $instance = null;
  10. /**
  11. * This class can be used as singleton, so that later we can move to DI patterns (ish...)
  12. *
  13. * @return Logger
  14. */
  15. public static function instance()
  16. {
  17. if (self::$instance === null) {
  18. self::$instance = new self();
  19. }
  20. return self::$instance;
  21. }
  22. // *** Implement the same interface as PSR/LOG, for the sake of interoperability ***
  23. /**
  24. * NB: unlike other "traditional" loggers, this one echoes to screen the debug messages instead of logging them.
  25. *
  26. * @param string $message
  27. * @param array $context known key: 'encoding'
  28. * @return void
  29. */
  30. public function debug($message, $context = array())
  31. {
  32. if (isset($context['encoding'])) {
  33. $this->debugMessage($message, $context['encoding']);
  34. } else {
  35. $this->debugMessage($message);
  36. }
  37. }
  38. /**
  39. * Following the general principle of 'never break stdout', the default behaviour
  40. *
  41. * @param string $message
  42. * @param $context
  43. * @return void
  44. */
  45. public function warning($message, $context = array())
  46. {
  47. $this->errorLog(preg_replace('/^XML-RPC :/', 'XML-RPC Warning: ', $message));
  48. }
  49. /**
  50. * Triggers the writing of a message to php's error log
  51. *
  52. * @param string $message
  53. * @param array $context
  54. * @return void
  55. */
  56. public function error($message, $context = array())
  57. {
  58. $this->errorLog(preg_replace('/^XML-RPC :/', 'XML-RPC Error: ', $message));
  59. }
  60. // BC interface
  61. /**
  62. * Echoes a debug message, taking care of escaping it when not in console mode.
  63. * NB: if the encoding of the message is not known or wrong, and we are working in web mode, there is no guarantee
  64. * of 100% accuracy, which kind of defeats the purpose of debugging
  65. *
  66. * @param string $message
  67. * @param string $encoding deprecated
  68. * @return void
  69. *
  70. * @internal left in purely for BC
  71. */
  72. public function debugMessage($message, $encoding = null)
  73. {
  74. // US-ASCII is a warning for PHP and a fatal for HHVM
  75. if ($encoding == 'US-ASCII') {
  76. $encoding = 'UTF-8';
  77. }
  78. if (PHP_SAPI != 'cli') {
  79. $flags = ENT_COMPAT;
  80. // avoid warnings on php < 5.4...
  81. if (defined('ENT_HTML401')) {
  82. $flags = $flags | ENT_HTML401;
  83. }
  84. if (defined('ENT_SUBSTITUTE')) {
  85. $flags = $flags | ENT_SUBSTITUTE;
  86. }
  87. if ($encoding != null) {
  88. print "<PRE>\n".htmlentities($message, $flags, $encoding)."\n</PRE>";
  89. } else {
  90. print "<PRE>\n".htmlentities($message, $flags)."\n</PRE>";
  91. }
  92. } else {
  93. print "\n$message\n";
  94. }
  95. // let the user see this now in case there's a time-out later...
  96. flush();
  97. }
  98. /**
  99. * Writes a message to the error log.
  100. *
  101. * @param string $message
  102. * @return void
  103. *
  104. * @internal left in purely for BC
  105. */
  106. public function errorLog($message)
  107. {
  108. error_log($message);
  109. }
  110. }