ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilLoggerFactory.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use Monolog\Logger;
22use Monolog\Handler\StreamHandler;
23use Monolog\Handler\BrowserConsoleHandler;
24use Monolog\Formatter\LineFormatter;
25use Monolog\Handler\FingersCrossedHandler;
26use Monolog\Handler\NullHandler;
27use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
29use Monolog\Processor\PsrLogMessageProcessor;
30
38{
39 protected const DEFAULT_FORMAT = "[%extra.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 {
91 $factory = self::getInstance();
92 return $factory->getComponentLogger($a_component_id);
93 }
94
98 public static function getRootLogger(): ilLogger
99 {
100 $factory = self::getInstance();
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 if (!$this->isLoggingEnabled()) {
192 $null_handler = new NullHandler();
193 $logger->pushHandler($null_handler);
194
195 return $this->loggers[$a_component_id] = new ilComponentLogger($logger);
196 }
197
198
199 // standard stream handler
200 $stream_handler = new StreamHandler(
201 $this->getSettings()->getLogDir() . '/' . $this->getSettings()->getLogFile(),
202 Logger::DEBUG, // default minimum level, will be overwritten by component log level
203 true
204 );
205
206 if ($a_component_id == self::ROOT_LOGGER) {
207 $stream_handler->setLevel($this->getSettings()->getLevelByComponent(self::COMPONENT_ROOT));
208 } else {
209 $stream_handler->setLevel($this->getSettings()->getLevelByComponent($a_component_id));
210 }
211
212 // format lines
213 $line_formatter = new ilLineFormatter(static::DEFAULT_FORMAT, 'Y-m-d H:i:s.u', true, true);
214 $stream_handler->setFormatter($line_formatter);
215
216 if ($this->getSettings()->isCacheEnabled()) {
217 // add new finger crossed handler
218 $finger_crossed_handler = new FingersCrossedHandler(
219 $stream_handler,
220 new ErrorLevelActivationStrategy($this->getSettings()->getCacheLevel()),
221 1000
222 );
223 $logger->pushHandler($finger_crossed_handler);
224 } else {
225 $logger->pushHandler($stream_handler);
226 }
227
228 if (
229 $this->dic->offsetExists('ilUser') &&
230 $this->dic->user() instanceof ilObjUser
231 ) {
232 if ($this->getSettings()->isBrowserLogEnabledForUser($this->dic->user()->getLogin())) {
233 if ($this->isConsoleAvailable()) {
234 $browser_handler = new BrowserConsoleHandler();
235 $browser_handler->setLevel($this->getSettings()->getLevel());
236 $browser_handler->setFormatter($line_formatter);
237 $logger->pushHandler($browser_handler);
238 }
239 }
240 }
241
242
243 // suid log
244 $logger->pushProcessor(function ($record) {
245 $record['extra']['suid'] = substr(session_id(), 0, 5);
246 return $record;
247 });
248
249 // append trace
250 $logger->pushProcessor(new ilTraceProcessor(ilLogLevel::DEBUG));
251
252 // Interpolate context variables.
253 $logger->pushProcessor(new PsrLogMessageProcessor());
254
255 // register new logger
256 $this->loggers[$a_component_id] = new ilComponentLogger($logger);
257
258 return $this->loggers[$a_component_id];
259 }
260}
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:36
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
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: shib_login.php:26