Console.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace AlibabaCloud\Tea\Console;
  3. use Monolog\Handler\AbstractProcessingHandler;
  4. use Monolog\Handler\StreamHandler;
  5. use Monolog\Logger;
  6. /**
  7. * This is a console module.
  8. */
  9. class Console
  10. {
  11. /**
  12. * @var Logger
  13. */
  14. private static $loggerDriver;
  15. /**
  16. * Console val with log level into stdout.
  17. *
  18. * @param string $val the printing string
  19. *
  20. * @throws \Exception
  21. *
  22. * @example \[LOG\] tea console example
  23. */
  24. public static function log($val)
  25. {
  26. self::logger()->log(200, $val);
  27. }
  28. /**
  29. * Console val with info level into stdout.
  30. *
  31. * @param string $val the printing string
  32. *
  33. * @throws \Exception
  34. *
  35. * @example \[INFO\] tea console example
  36. */
  37. public static function info($val)
  38. {
  39. self::logger()->info($val);
  40. }
  41. /**
  42. * Console val with warning level into stdout.
  43. *
  44. * @param string $val the printing string
  45. *
  46. * @throws \Exception
  47. *
  48. * @example \[WARNING\] tea console example
  49. */
  50. public static function warning($val)
  51. {
  52. self::logger()->warning($val);
  53. }
  54. /**
  55. * Console val with debug level into stdout.
  56. *
  57. * @param string $val the printing string
  58. *
  59. * @throws \Exception
  60. *
  61. * @example \[DEBUG\] tea console example
  62. */
  63. public static function debug($val)
  64. {
  65. self::logger()->debug($val);
  66. }
  67. /**
  68. * Console val with error level into stderr.
  69. *
  70. * @param string $val the printing string
  71. *
  72. * @throws \Exception
  73. *
  74. * @example \[ERROR\] tea console example
  75. */
  76. public static function error($val)
  77. {
  78. self::logger()->error($val);
  79. }
  80. /**
  81. * @param AbstractProcessingHandler $handler
  82. */
  83. public static function pushHandler($handler)
  84. {
  85. self::$loggerDriver->pushHandler($handler);
  86. }
  87. /**
  88. * @return Logger
  89. */
  90. public static function logger()
  91. {
  92. if (null === self::$loggerDriver) {
  93. self::$loggerDriver = new Logger('tea-console-log');
  94. self::$loggerDriver->pushHandler(new StreamHandler('php://stderr', 0));
  95. }
  96. return self::$loggerDriver;
  97. }
  98. }