ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilLoggerFactory.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once './libs/composer/vendor/autoload.php';
5 include_once './Services/Logging/classes/public/class.ilLogLevel.php';
6 
14 
22 {
23  const DEFAULT_FORMAT = "[%suid%] [%datetime%] %channel%.%level_name%: %message% %context% %extra%\n";
24 
25  const ROOT_LOGGER = 'root';
26  const COMPONENT_ROOT = 'log_root';
27  const SETUP_LOGGER = 'setup';
28 
29  private static $instance = null;
30 
31  private $settings = null;
32 
33  private $enabled = false;
34  private $loggers = array();
35 
37  {
38  $this->settings = $settings;
39  $this->enabled = $this->getSettings()->isEnabled();
40  }
41 
46  public static function getInstance()
47  {
48  if (!static::$instance) {
49  include_once './Services/Logging/classes/class.ilLoggingDBSettings.php';
51  static::$instance = new ilLoggerFactory($settings);
52  }
53  return static::$instance;
54  }
55 
61  public static function newInstance(ilLoggingSettings $settings)
62  {
63  return static::$instance = new self($settings);
64  }
65 
66 
74  public static function getLogger($a_component_id)
75  {
76  $factory = self::getInstance();
77  return $factory->getComponentLogger($a_component_id);
78  }
79 
84  public static function getRootLogger()
85  {
86  $factory = self::getInstance();
87  return $factory->getComponentLogger(self::ROOT_LOGGER);
88  }
89 
90 
96  public function initUser($a_login)
97  {
98  if (!$this->getSettings()->isBrowserLogEnabledForUser($a_login)) {
99  return true;
100  }
101 
102  include_once("./Services/Logging/classes/extensions/class.ilLineFormatter.php");
103 
104  foreach ($this->loggers as $a_component_id => $logger) {
105  if ($this->isConsoleAvailable()) {
106  $browser_handler = new BrowserConsoleHandler();
107  $browser_handler->setLevel($this->getSettings()->getLevelByComponent($a_component_id));
108  $browser_handler->setFormatter(new ilLineFormatter(static::DEFAULT_FORMAT, 'Y-m-d H:i:s.u', true, true));
109  $logger->getLogger()->pushHandler($browser_handler);
110  }
111  }
112  }
113 
118  protected function isConsoleAvailable() : bool
119  {
121  return false;
122  }
123 
124  if (isset($_SERVER['HTTP_ACCEPT']) && strpos($_SERVER['HTTP_ACCEPT'], 'text/html') !== false) {
125  // If the client expects HTML, allow console logging
126  return true;
127  }
128 
129  if (isset($_SERVER['HTTP_ACCEPT']) && strpos($_SERVER['HTTP_ACCEPT'], 'application/json') !== false) {
130  // If the client expects JSON, don't use console logging: https://mantis.ilias.de/view.php?id=37167
131  return false;
132  }
133 
134  if ((isset($_GET['cmdMode']) && $_GET['cmdMode'] === 'asynch') || (
135  isset($GLOBALS['DIC']['http']) &&
136  strtolower($GLOBALS['DIC']->http()->request()->getServerParams()['HTTP_X_REQUESTED_WITH'] ?? '') === 'xmlhttprequest'
137  )) {
138  return false;
139  }
140 
141  return true;
142  }
143 
148  public function getSettings()
149  {
150  return $this->settings;
151  }
152 
157  protected function getLoggers()
158  {
159  return $this->loggers;
160  }
161 
167  public function getComponentLogger($a_component_id)
168  {
169  if (isset($this->loggers[$a_component_id])) {
170  return $this->loggers[$a_component_id];
171  }
172 
173  $loggerNamePrefix = '';
174  if (defined('CLIENT_ID')) {
175  $loggerNamePrefix = CLIENT_ID . '_';
176  }
177 
178  switch ($a_component_id) {
179  case 'root':
180  $logger = new Logger($loggerNamePrefix . 'root');
181  break;
182 
183  default:
184  $logger = new Logger($loggerNamePrefix . $a_component_id);
185  break;
186 
187  }
188 
189  if (!$this->getSettings()->isEnabled()) {
190  $null_handler = new NullHandler();
191  $logger->pushHandler($null_handler);
192 
193  include_once './Services/Logging/classes/class.ilComponentLogger.php';
194  return $this->loggers[$a_component_id] = new ilComponentLogger($logger);
195  }
196 
197 
198  // standard stream handler
199  $stream_handler = new StreamHandler(
200  $this->getSettings()->getLogDir() . '/' . $this->getSettings()->getLogFile(),
201  true
202  );
203 
204  if ($a_component_id == self::ROOT_LOGGER) {
205  $stream_handler->setLevel($this->getSettings()->getLevelByComponent(self::COMPONENT_ROOT));
206  } else {
207  $stream_handler->setLevel($this->getSettings()->getLevelByComponent($a_component_id));
208  }
209 
210  // format lines
211  include_once("./Services/Logging/classes/extensions/class.ilLineFormatter.php");
212  $line_formatter = new ilLineFormatter(static::DEFAULT_FORMAT, 'Y-m-d H:i:s.u', true, true);
213  $stream_handler->setFormatter($line_formatter);
214 
215  if ($this->getSettings()->isCacheEnabled()) {
216  // add new finger crossed handler
217  $finger_crossed_handler = new FingersCrossedHandler(
218  $stream_handler,
219  new ErrorLevelActivationStrategy($this->getSettings()->getCacheLevel()),
220  1000
221  );
222  $logger->pushHandler($finger_crossed_handler);
223  } else {
224  $logger->pushHandler($stream_handler);
225  }
226 
227  if (
228  $GLOBALS['DIC']->offsetExists('ilUser') &&
229  $GLOBALS['DIC']['ilUser'] instanceof ilObjUser
230  ) {
231  if ($this->getSettings()->isBrowserLogEnabledForUser($GLOBALS['DIC']->user()->getLogin())) {
232  if ($this->isConsoleAvailable()) {
233  $browser_handler = new BrowserConsoleHandler();
234  #$browser_handler->setLevel($this->getSettings()->getLevelByComponent($a_component_id));
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['suid'] = substr(session_id(), 0, 5);
246  return $record;
247  });
248 
249  // append trace
250  include_once './Services/Logging/classes/extensions/class.ilTraceProcessor.php';
251  $logger->pushProcessor(new ilTraceProcessor(ilLogLevel::DEBUG));
252 
253 
254  // register new logger
255  include_once './Services/Logging/classes/class.ilComponentLogger.php';
256  $this->loggers[$a_component_id] = new ilComponentLogger($logger);
257 
258  return $this->loggers[$a_component_id];
259  }
260 }
settings()
Definition: settings.php:2
getSettings()
Get settigns.
$_GET["client_id"]
static newInstance(ilLoggingSettings $settings)
get new instance
Component logger with individual log levels by component id.
initUser($a_login)
Init user specific log options.
user()
Definition: user.php:4
static http()
Fetches the global http state from ILIAS.
isConsoleAvailable()
Check if console handler is available.
Logging factory.
$_SERVER['HTTP_HOST']
Definition: raiseError.php:10
const CLIENT_ID
Definition: constants.php:39
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
Definition: PEAR.php:64
getComponentLogger($a_component_id)
Get component logger.
static getInstance()
Get instance.
Custom line formatter.
const CONTEXT_WEB
static getLogger($a_component_id)
Get component logger.
__construct(ilLoggingSettings $settings)
static getType()
Get context type.
$factory
Definition: metadata.php:58
static getRootLogger()
The unique root logger has a fixed error level.