ILIAS  release_8 Revision v8.24
class.ilLoggerFactory.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
22use Monolog\Logger;
23use Monolog\Handler\StreamHandler;
24use Monolog\Handler\BrowserConsoleHandler;
25use Monolog\Formatter\LineFormatter;
26use Monolog\Handler\FingersCrossedHandler;
27use Monolog\Handler\NullHandler;
28use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
30
38{
39 protected const DEFAULT_FORMAT = "[%suid%] [%datetime%] %channel%.%level_name%: %message% %context% %extra%\n";
40
41 protected const ROOT_LOGGER = 'root';
42 protected const COMPONENT_ROOT = 'log_root';
43 protected const SETUP_LOGGER = 'setup';
44
45 private static ?ilLoggerFactory $instance = null;
46
48 protected Container $dic;
49
50 private bool $enabled = false; //ToDo PHP8 Review: This is a private var never read only written and should probably be removed.
51
55 private array $loggers = array();
56
58 {
59 global $DIC;
60
61 $this->dic = $DIC;
62 $this->settings = $settings;
63 $this->enabled = $this->getSettings()->isEnabled();
64 }
65
66 public static function getInstance(): ilLoggerFactory
67 {
68 if (!static::$instance instanceof ilLoggerFactory) {
70 static::$instance = new ilLoggerFactory($settings);
71 }
72 return static::$instance;
73 }
74
76 {
77 return static::$instance = new self($settings);
78 }
79
80 public function isLoggingEnabled(): bool
81 {
82 return $this->enabled;
83 }
84
85
89 public static function getLogger(string $a_component_id): ilLogger
90 {
92 return $factory->getComponentLogger($a_component_id);
93 }
94
98 public static function getRootLogger(): ilLogger
99 {
101 return $factory->getComponentLogger(self::ROOT_LOGGER);
102 }
103
104
108 public function initUser(string $a_login): void
109 {
110 if (!$this->getSettings()->isBrowserLogEnabledForUser($a_login)) {
111 return;
112 }
113
114 foreach ($this->loggers as $a_component_id => $logger) {
115 if ($this->isConsoleAvailable()) {
116 $browser_handler = new BrowserConsoleHandler();
117 $browser_handler->setLevel($this->getSettings()->getLevelByComponent($a_component_id));
118 $browser_handler->setFormatter(new ilLineFormatter(static::DEFAULT_FORMAT, 'Y-m-d H:i:s.u', true, true));
119 $logger->getLogger()->pushHandler($browser_handler);
120 }
121 }
122 }
123
127 protected function isConsoleAvailable(): bool
128 {
130 return false;
131 }
132 if (
133 $this->dic->isDependencyAvailable('ctrl') && $this->dic->ctrl()->isAsynch() ||
134 (
135 $this->dic->isDependencyAvailable('http') &&
136 strtolower($this->dic->http()->request()->getServerParams()['HTTP_X_REQUESTED_WITH'] ?? '') === 'xmlhttprequest'
137 )
138 ) {
139 return false;
140 }
141
142 if (
143 $this->dic->isDependencyAvailable('http') &&
144 strpos($this->dic->http()->request()->getServerParams()['HTTP_ACCEPT'], 'text/html') !== false
145 ) {
146 return true;
147 }
148 if (
149 $this->dic->isDependencyAvailable('http') &&
150 strpos($this->dic->http()->request()->getServerParams()['HTTP_ACCEPT'], 'application/json') !== false
151 ) {
152 return false;
153 }
154 return true;
155 }
156
158 {
159 return $this->settings;
160 }
161
165 protected function getLoggers(): array
166 {
167 return $this->loggers;
168 }
169
170 public function getComponentLogger(string $a_component_id): ilLogger
171 {
172 if (isset($this->loggers[$a_component_id])) {
173 return $this->loggers[$a_component_id];
174 }
175
176 $loggerNamePrefix = '';
177 if (defined('CLIENT_ID')) {
178 $loggerNamePrefix = CLIENT_ID . '_';
179 }
180
181 switch ($a_component_id) {
182 case 'root':
183 $logger = new Logger($loggerNamePrefix . 'root');
184 break;
185
186 default:
187 $logger = new Logger($loggerNamePrefix . $a_component_id);
188 break;
189
190 }
191
192 if (!$this->isLoggingEnabled()) {
193 $null_handler = new NullHandler();
194 $logger->pushHandler($null_handler);
195
196 return $this->loggers[$a_component_id] = new ilComponentLogger($logger);
197 }
198
199
200 // standard stream handler
201 $stream_handler = new StreamHandler(
202 $this->getSettings()->getLogDir() . '/' . $this->getSettings()->getLogFile(),
203 Logger::DEBUG, // default minimum level, will be overwritten by component log level
204 true
205 );
206
207 if ($a_component_id == self::ROOT_LOGGER) {
208 $stream_handler->setLevel($this->getSettings()->getLevelByComponent(self::COMPONENT_ROOT));
209 } else {
210 $stream_handler->setLevel($this->getSettings()->getLevelByComponent($a_component_id));
211 }
212
213 // format lines
214 $line_formatter = new ilLineFormatter(static::DEFAULT_FORMAT, 'Y-m-d H:i:s.u', true, true);
215 $stream_handler->setFormatter($line_formatter);
216
217 if ($this->getSettings()->isCacheEnabled()) {
218 // add new finger crossed handler
219 $finger_crossed_handler = new FingersCrossedHandler(
220 $stream_handler,
221 new ErrorLevelActivationStrategy($this->getSettings()->getCacheLevel()),
222 1000
223 );
224 $logger->pushHandler($finger_crossed_handler);
225 } else {
226 $logger->pushHandler($stream_handler);
227 }
228
229 if (
230 $this->dic->offsetExists('ilUser') &&
231 $this->dic->user() instanceof ilObjUser
232 ) {
233 if ($this->getSettings()->isBrowserLogEnabledForUser($this->dic->user()->getLogin())) {
234 if ($this->isConsoleAvailable()) {
235 $browser_handler = new BrowserConsoleHandler();
236 $browser_handler->setLevel($this->getSettings()->getLevel());
237 $browser_handler->setFormatter($line_formatter);
238 $logger->pushHandler($browser_handler);
239 }
240 }
241 }
242
243
244 // suid log
245 $logger->pushProcessor(function ($record) {
246 $record['suid'] = substr(session_id(), 0, 5);
247 return $record;
248 });
249
250 // append trace
251 $logger->pushProcessor(new ilTraceProcessor(ilLogLevel::DEBUG));
252
253
254 // register new logger
255 $this->loggers[$a_component_id] = new ilComponentLogger($logger);
256
257 return $this->loggers[$a_component_id];
258 }
259}
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:32
Component logger with individual log levels by component id.
const CONTEXT_WEB
static getType()
Get context type.
Custom line formatter.
static newInstance(ilLoggingSettings $settings)
static ilLoggerFactory $instance
static getRootLogger()
The unique root logger has a fixed error level.
initUser(string $a_login)
Init user specific log options.
isConsoleAvailable()
Check if console handler is available.
getComponentLogger(string $a_component_id)
static getLogger(string $a_component_id)
Get component logger.
ilLoggingSettings $settings
__construct(ilLoggingSettings $settings)
Component logger with individual log levels by component id.
User class.
const CLIENT_ID
Definition: constants.php:41
global $DIC
Definition: feed.php:28
$factory
Definition: metadata.php:75