ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
LazyInternalFactory.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Logging\Logger;
22
23use ILIAS\Logging\Config\Basic\ConfigInterface as BasicConfigInterface;
24use ILIAS\Logging\Logger\Monolog\FactoryInterface as MonologFactoryInterface;
25use Monolog\Logger as MonologLogger;
27use ReflectionClass;
29use Monolog\Handler\HandlerInterface as MonologHandlerInterface;
30use ILIAS\Logging\Logger\Monolog\Handler\ProviderInterface as MonologHandlerProviderInterface;
31
33{
37 protected array $loggers = [];
38
42 public function __construct(
43 protected MonologFactoryInterface $monolog_factory,
44 protected BasicConfigInterface $basic_config,
45 protected array $handler_providers
46 ) {
47 }
48
49 public function getLazyGhost(
50 string $component_id,
51 LevelFetcherInterface $level_fetcher
53 if (isset($this->loggers[$component_id])) {
54 return $this->loggers[$component_id];
55 }
56
58 $lazy_logger = new ReflectionClass(Logger::class)->newLazyGhost(
59 function (Logger $logger) use ($component_id, $level_fetcher): void {
60 $monolog_logger = $this->buildMonologLogger($component_id, $level_fetcher);
61 $logger->__construct($monolog_logger);
62 }
63 );
64 return $this->loggers[$component_id] = $lazy_logger;
65 }
66
67 protected function buildMonologLogger(
68 string $component_id,
69 LevelFetcherInterface $level_fetcher
70 ): MonologLogger {
71 if (!$this->basic_config->isLoggingEnabled()) {
72 return $this->monolog_factory->nullLogger($component_id);
73 }
74 $level = $level_fetcher->fetchLevel();
75 if ($level === ILIASLogLevel::OFF) { // OFF is not a standard log level, so Monolog rejects it
76 return $this->monolog_factory->nullLogger($component_id);
77 }
78 return $this->monolog_factory->logger(
79 $component_id,
80 ...$this->getApplicableHandlers($component_id, $level)
81 );
82 }
83
87 protected function getApplicableHandlers(
88 string $component_id,
89 ILIASLogLevel $level
90 ): array {
91 $applicable = [];
92 foreach ($this->handler_providers as $handler_provider) {
93 if (!$handler_provider->appliesTo($component_id)) {
94 continue;
95 }
96 $applicable[] = $handler_provider->getHandler($component_id, $level);
97 }
98 return $applicable;
99 }
100}
buildMonologLogger(string $component_id, LevelFetcherInterface $level_fetcher)
getApplicableHandlers(string $component_id, ILIASLogLevel $level)
__construct(protected MonologFactoryInterface $monolog_factory, protected BasicConfigInterface $basic_config, protected array $handler_providers)
getLazyGhost(string $component_id, LevelFetcherInterface $level_fetcher)
This exists to encapsulate reading the level of a logger from the config, to be able to delay it as m...
if(!file_exists('../ilias.ini.php'))