Log.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /*
  3. * This file is part of the overtrue/wechat.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. /**
  11. * Log.php.
  12. *
  13. * This file is part of the wechat-components.
  14. *
  15. * (c) overtrue <i@overtrue.me>
  16. *
  17. * This source file is subject to the MIT license that is bundled
  18. * with this source code in the file LICENSE.
  19. */
  20. namespace EasyWeChat\Support;
  21. use Monolog\Handler\ErrorLogHandler;
  22. use Monolog\Handler\NullHandler;
  23. use Monolog\Logger;
  24. use Psr\Log\LoggerInterface;
  25. /**
  26. * Class Log.
  27. *
  28. * @method static debug($message, $context = null)
  29. * @method static info($message, $context = null)
  30. * @method static notice($message, $context = null)
  31. * @method static warning($message, $context = null)
  32. * @method static error($message, $context = null)
  33. * @method static critical($message, $context = null)
  34. * @method static alert($message, $context = null)
  35. * @method static emergency($message, $context = null)
  36. */
  37. class Log
  38. {
  39. /**
  40. * Logger instance.
  41. *
  42. * @var \Psr\Log\LoggerInterface
  43. */
  44. protected static $logger;
  45. /**
  46. * Return the logger instance.
  47. *
  48. * @return \Psr\Log\LoggerInterface
  49. */
  50. public static function getLogger()
  51. {
  52. return self::$logger ?: self::$logger = self::createDefaultLogger();
  53. }
  54. /**
  55. * Set logger.
  56. *
  57. * @param \Psr\Log\LoggerInterface $logger
  58. */
  59. public static function setLogger(LoggerInterface $logger)
  60. {
  61. self::$logger = $logger;
  62. }
  63. /**
  64. * Tests if logger exists.
  65. *
  66. * @return bool
  67. */
  68. public static function hasLogger()
  69. {
  70. return self::$logger ? true : false;
  71. }
  72. /**
  73. * Forward call.
  74. *
  75. * @param string $method
  76. * @param array $args
  77. *
  78. * @return mixed
  79. */
  80. public static function __callStatic($method, $args)
  81. {
  82. return forward_static_call_array([self::getLogger(), $method], $args);
  83. }
  84. /**
  85. * Forward call.
  86. *
  87. * @param string $method
  88. * @param array $args
  89. *
  90. * @return mixed
  91. */
  92. public function __call($method, $args)
  93. {
  94. return call_user_func_array([self::getLogger(), $method], $args);
  95. }
  96. /**
  97. * Make a default log instance.
  98. *
  99. * @return \Monolog\Logger
  100. */
  101. private static function createDefaultLogger()
  102. {
  103. $log = new Logger('EasyWeChat');
  104. if (defined('PHPUNIT_RUNNING') || 'cli' === php_sapi_name()) {
  105. $log->pushHandler(new NullHandler());
  106. } else {
  107. $log->pushHandler(new ErrorLogHandler());
  108. }
  109. return $log;
  110. }
  111. }