ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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
4include_once './Services/Logging/lib/vendor/autoload.php';
5include_once './Services/Logging/classes/public/class.ilLogLevel.php';
6
14
15
28{
29 const DEFAULT_FORMAT = "[%suid%] [%datetime%] %channel%.%level_name%: %message% %context% %extra%\n";
30
31 const ROOT_LOGGER = 'root';
32 const COMPONENT_ROOT = 'log_root';
33 const SETUP_LOGGER = 'setup';
34
35 private static $instance = null;
36
37 private $settings = null;
38
39 private $enabled = FALSE;
40 private $loggers = array();
41
43 {
44 $this->settings = $settings;
45 $this->enabled = $this->getSettings()->isEnabled();
46
47 }
48
53 public static function getInstance()
54 {
55 if(!static::$instance)
56 {
57 include_once './Services/Logging/classes/class.ilLoggingDBSettings.php';
59 static::$instance = new ilLoggerFactory($settings);
60 }
61 return static::$instance;
62 }
63
69 public static function newInstance(ilLoggingSettings $settings)
70 {
71 return new self($settings);
72 }
73
74
82 public static function getLogger($a_component_id)
83 {
84 $factory = self::getInstance();
85 return $factory->getComponentLogger($a_component_id);
86 }
87
92 public static function getRootLogger()
93 {
94 $factory = self::getInstance();
95 return $factory->getComponentLogger(self::ROOT_LOGGER);
96 }
97
98
104 public function initUser($a_login)
105 {
106 if(!$this->getSettings()->isBrowserLogEnabledForUser($a_login))
107 {
108 return TRUE;
109 }
110
111 include_once("./Services/Logging/classes/extensions/class.ilLineFormatter.php");
112
113 foreach($this->loggers as $a_component_id => $logger)
114 {
115 if($this->isConsoleAvailable())
116 {
117 $browser_handler = new BrowserConsoleHandler();
118 $browser_handler->setLevel($this->getSettings()->getLevelByComponent($a_component_id));
119 $browser_handler->setFormatter(new ilLineFormatter(static::DEFAULT_FORMAT, 'Y-m-d H:i:s.u',TRUE,TRUE));
120 $logger->getLogger()->pushHandler($browser_handler);
121 }
122 }
123 }
124
129 protected function isConsoleAvailable()
130 {
131 include_once './Services/Context/classes/class.ilContext.php';
133 {
134 return FALSE;
135 }
136 if (isset($_GET["cmdMode"]) && $_GET["cmdMode"] == "asynch")
137 {
138 return FALSE;
139 }
140 return TRUE;
141 }
142
147 public function getSettings()
148 {
149 return $this->settings;
150 }
151
156 protected function getLoggers()
157 {
158 return $this->loggers;
159 }
160
166 public function getComponentLogger($a_component_id)
167 {
168 if(isset($this->loggers[$a_component_id]))
169 {
170 return $this->loggers[$a_component_id];
171 }
172
173 switch($a_component_id)
174 {
175 case 'root':
176 $logger = new Logger(CLIENT_ID.'_root');
177 break;
178
179 default:
180 $logger = new Logger(CLIENT_ID.'_'.$a_component_id);
181 break;
182
183 }
184
185 if(!$this->getSettings()->isEnabled())
186 {
187 $null_handler = new NullHandler();
188 $logger->pushHandler($null_handler);
189
190 include_once './Services/Logging/classes/class.ilComponentLogger.php';
191 return $this->loggers[$a_component_id] = new ilComponentLogger($logger);
192 }
193
194
195 // standard stream handler
196 $stream_handler = new StreamHandler(
197 $this->getSettings()->getLogDir().'/'.$this->getSettings()->getLogFile(),
198 TRUE
199 );
200
201 if($a_component_id == self::ROOT_LOGGER)
202 {
203 $stream_handler->setLevel($this->getSettings()->getLevelByComponent(self::COMPONENT_ROOT));
204 }
205 else
206 {
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 {
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 }
225 else
226 {
227 $logger->pushHandler($stream_handler);
228 }
229
230 if($GLOBALS['ilUser'] instanceof ilObjUser)
231 {
232 if($this->getSettings()->isBrowserLogEnabledForUser($GLOBALS['ilUser']->getLogin()))
233 {
234 if($this->isConsoleAvailable())
235 {
236 $browser_handler = new BrowserConsoleHandler();
237 #$browser_handler->setLevel($this->getSettings()->getLevelByComponent($a_component_id));
238 $browser_handler->setLevel($this->getSettings()->getLevel());
239 $browser_handler->setFormatter($line_formatter);
240 $logger->pushHandler($browser_handler);
241 }
242 }
243 }
244
245
246 // suid log
247 $logger->pushProcessor(function ($record) {
248 $record['suid'] = substr(session_id(),0,5);
249 return $record;
250 });
251
252 // append trace
253 include_once './Services/Logging/classes/extensions/class.ilTraceProcessor.php';
254 $logger->pushProcessor(new ilTraceProcessor(ilLogLevel::DEBUG));
255
256
257 // register new logger
258 include_once './Services/Logging/classes/class.ilComponentLogger.php';
259 $this->loggers[$a_component_id] = new ilComponentLogger($logger);
260
261 return $this->loggers[$a_component_id];
262 }
263}
264?>
$_GET["client_id"]
Formats incoming records into a one-line string.
Handler sending logs to browser's javascript console with no browser extension required.
Buffers all records until a certain level is reached.
Stores to any stream resource.
Monolog log channel.
Definition: Logger.php:28
Component logger with individual log levels by component id.
const CONTEXT_WEB
static getType()
Get context type.
Custom line formatter.
static newInstance(ilLoggingSettings $settings)
get new instance
getSettings()
Get settigns.
static getRootLogger()
The unique root logger has a fixed error level.
isConsoleAvailable()
Check if console handler is available.
static getLogger($a_component_id)
Get component logger.
initUser($a_login)
Init user specific log options.
getComponentLogger($a_component_id)
Get component logger.
__construct(ilLoggingSettings $settings)
static getInstance()
Get instance.
$GLOBALS['PHPCAS_CLIENT']
This global variable is used by the interface class phpCAS.
Definition: CAS.php:276