ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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()
119  {
120  include_once './Services/Context/classes/class.ilContext.php';
122  return false;
123  }
124  if (isset($_GET["cmdMode"]) && $_GET["cmdMode"] == "asynch") {
125  return false;
126  }
127  return true;
128  }
129 
134  public function getSettings()
135  {
136  return $this->settings;
137  }
138 
143  protected function getLoggers()
144  {
145  return $this->loggers;
146  }
147 
153  public function getComponentLogger($a_component_id)
154  {
155  if (isset($this->loggers[$a_component_id])) {
156  return $this->loggers[$a_component_id];
157  }
158 
159  $loggerNamePrefix = '';
160  if (defined('CLIENT_ID')) {
161  $loggerNamePrefix = CLIENT_ID . '_';
162  }
163 
164  switch ($a_component_id) {
165  case 'root':
166  $logger = new Logger($loggerNamePrefix . 'root');
167  break;
168 
169  default:
170  $logger = new Logger($loggerNamePrefix . $a_component_id);
171  break;
172 
173  }
174 
175  if (!$this->getSettings()->isEnabled()) {
176  $null_handler = new NullHandler();
177  $logger->pushHandler($null_handler);
178 
179  include_once './Services/Logging/classes/class.ilComponentLogger.php';
180  return $this->loggers[$a_component_id] = new ilComponentLogger($logger);
181  }
182 
183 
184  // standard stream handler
185  $stream_handler = new StreamHandler(
186  $this->getSettings()->getLogDir() . '/' . $this->getSettings()->getLogFile(),
187  true
188  );
189 
190  if ($a_component_id == self::ROOT_LOGGER) {
191  $stream_handler->setLevel($this->getSettings()->getLevelByComponent(self::COMPONENT_ROOT));
192  } else {
193  $stream_handler->setLevel($this->getSettings()->getLevelByComponent($a_component_id));
194  }
195 
196  // format lines
197  include_once("./Services/Logging/classes/extensions/class.ilLineFormatter.php");
198  $line_formatter = new ilLineFormatter(static::DEFAULT_FORMAT, 'Y-m-d H:i:s.u', true, true);
199  $stream_handler->setFormatter($line_formatter);
200 
201  if ($this->getSettings()->isCacheEnabled()) {
202  // add new finger crossed handler
203  $finger_crossed_handler = new FingersCrossedHandler(
204  $stream_handler,
205  new ErrorLevelActivationStrategy($this->getSettings()->getCacheLevel()),
206  1000
207  );
208  $logger->pushHandler($finger_crossed_handler);
209  } else {
210  $logger->pushHandler($stream_handler);
211  }
212 
213  if (
214  $GLOBALS['DIC']->offsetExists('ilUser') &&
215  $GLOBALS['DIC']['ilUser'] instanceof ilObjUser
216  ) {
217  if ($this->getSettings()->isBrowserLogEnabledForUser($GLOBALS['DIC']->user()->getLogin())) {
218  if ($this->isConsoleAvailable()) {
219  $browser_handler = new BrowserConsoleHandler();
220  #$browser_handler->setLevel($this->getSettings()->getLevelByComponent($a_component_id));
221  $browser_handler->setLevel($this->getSettings()->getLevel());
222  $browser_handler->setFormatter($line_formatter);
223  $logger->pushHandler($browser_handler);
224  }
225  }
226  }
227 
228 
229  // suid log
230  $logger->pushProcessor(function ($record) {
231  $record['suid'] = substr(session_id(), 0, 5);
232  return $record;
233  });
234 
235  // append trace
236  include_once './Services/Logging/classes/extensions/class.ilTraceProcessor.php';
237  $logger->pushProcessor(new ilTraceProcessor(ilLogLevel::DEBUG));
238 
239 
240  // register new logger
241  include_once './Services/Logging/classes/class.ilComponentLogger.php';
242  $this->loggers[$a_component_id] = new ilComponentLogger($logger);
243 
244  return $this->loggers[$a_component_id];
245  }
246 }
settings()
Definition: settings.php:2
Buffers all records until a certain level is reached.
getSettings()
Get settigns.
$_GET["client_id"]
static newInstance(ilLoggingSettings $settings)
get new instance
Monolog log channel.
Definition: Logger.php:28
$factory
Definition: metadata.php:43
Component logger with individual log levels by component id.
initUser($a_login)
Init user specific log options.
Handler sending logs to browser&#39;s javascript console with no browser extension required.
user()
Definition: user.php:4
isConsoleAvailable()
Check if console handler is available.
Logging factory.
getComponentLogger($a_component_id)
Get component logger.
static getInstance()
Get instance.
Custom line formatter.
Stores to any stream resource.
const CONTEXT_WEB
static getLogger($a_component_id)
Get component logger.
__construct(ilLoggingSettings $settings)
static getType()
Get context type.
$GLOBALS['JPEG_Segment_Names']
Global Variable: XMP_tag_captions.
static getRootLogger()
The unique root logger has a fixed error level.