Log.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * This file is part of webman.
  4. *
  5. * Licensed under The MIT License
  6. * For full copyright and license information, please see the MIT-LICENSE.txt
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @author walkor<[email protected]>
  10. * @copyright walkor<[email protected]>
  11. * @link http://www.workerman.net/
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace support\bootstrap;
  15. use Webman\Bootstrap;
  16. use Monolog\Logger;
  17. /**
  18. * Class Redis
  19. * @package support
  20. *
  21. * @method static void log($level, $message, array $context = [])
  22. * @method static void debug($message, array $context = [])
  23. * @method static void info($message, array $context = [])
  24. * @method static void notice($message, array $context = [])
  25. * @method static void warning($message, array $context = [])
  26. * @method static void error($message, array $context = [])
  27. * @method static void critical($message, array $context = [])
  28. * @method static void alert($message, array $context = [])
  29. * @method static void emergency($message, array $context = [])
  30. */
  31. class Log implements Bootstrap {
  32. /**
  33. * @var array
  34. */
  35. protected static $_instance = [];
  36. /**
  37. * @param \Workerman\Worker $worker
  38. * @return void
  39. */
  40. public static function start($worker)
  41. {
  42. $configs = config('log', []);
  43. foreach ($configs as $channel => $config) {
  44. $logger = static::$_instance[$channel] = new Logger($channel);
  45. foreach ($config['handlers'] as $handler_config) {
  46. $handler = new $handler_config['class'](... \array_values($handler_config['constructor']));
  47. if (isset($handler_config['formatter'])) {
  48. $formatter = new $handler_config['formatter']['class'](... \array_values($handler_config['formatter']['constructor']));
  49. $handler->setFormatter($formatter);
  50. }
  51. $logger->pushHandler($handler);
  52. }
  53. }
  54. }
  55. /**
  56. * @param string $name
  57. * @return Logger;
  58. */
  59. public static function channel($name = 'default')
  60. {
  61. return static::$_instance[$name] ?? null;
  62. }
  63. /**
  64. * @param $name
  65. * @param $arguments
  66. * @return mixed
  67. */
  68. public static function __callStatic($name, $arguments)
  69. {
  70. return static::channel('default')->{$name}(... $arguments);
  71. }
  72. }