LogServiceProvider.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. /*
  8. * This file is part of the overtrue/wechat.
  9. *
  10. * (c) overtrue <i@overtrue.me>
  11. *
  12. * This source file is subject to the MIT license that is bundled
  13. * with this source code in the file LICENSE.
  14. */
  15. namespace ByteDance\Kernel\Providers;
  16. use ByteDance\Kernel\Log\LogManager;
  17. use Pimple\Container;
  18. use Pimple\ServiceProviderInterface;
  19. /**
  20. * Class LoggingServiceProvider.
  21. *
  22. * @author overtrue <i@overtrue.me>
  23. */
  24. class LogServiceProvider implements ServiceProviderInterface
  25. {
  26. /**
  27. * Registers services on the given container.
  28. *
  29. * This method should only be used to configure services and parameters.
  30. * It should not get services.
  31. *
  32. * @param Container $pimple A container instance
  33. */
  34. public function register(Container $pimple)
  35. {
  36. $pimple['logger'] = $pimple['log'] = function ($app) {
  37. $config = $this->formatLogConfig($app);
  38. if (!empty($config)) {
  39. $app->rebind('config', $app['config']->merge($config));
  40. }
  41. return new LogManager($app);
  42. };
  43. }
  44. public function formatLogConfig($app)
  45. {
  46. if (!empty($app['config']->get('log.channels'))) {
  47. return $app['config']->get('log');
  48. }
  49. if (empty($app['config']->get('log'))) {
  50. return [
  51. 'log' => [
  52. 'default' => 'errorlog',
  53. 'channels' => [
  54. 'errorlog' => [
  55. 'driver' => 'errorlog',
  56. 'level' => 'debug',
  57. ],
  58. ],
  59. ],
  60. ];
  61. }
  62. return [
  63. 'log' => [
  64. 'default' => 'single',
  65. 'channels' => [
  66. 'single' => [
  67. 'driver' => 'single',
  68. 'path' => $app['config']->get('log.file') ?: \sys_get_temp_dir().'/logs/easywechat.log',
  69. 'level' => $app['config']->get('log.level', 'debug'),
  70. ],
  71. ],
  72. ],
  73. ];
  74. }
  75. }