ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilLoggingActivity.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2016 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
5 require_once './Services/WorkflowEngine/interfaces/ilActivity.php';
7 require_once './Services/WorkflowEngine/interfaces/ilWorkflowEngineElement.php';
9 require_once './Services/WorkflowEngine/interfaces/ilNode.php';
10 
24 {
26  private $context;
27 
29  private $log_file = 'none.log';
30 
32  private $log_message = 'no message set';
33 
40  private $log_level = 'MESSAGE';
41 
43  protected $name;
44 
50  public function __construct(ilNode $a_context)
51  {
52  $this->context = $a_context;
53  }
54 
62  public function setLogFile($a_log_file)
63  {
64  $extension = substr($a_log_file, strlen($a_log_file) - 4, 4);
65  $this->checkExtensionValidity($extension);
66  $this->checkFileWriteability($a_log_file);
67  $this->log_file = $a_log_file;
68  }
69 
79  private function checkFileWriteability($a_log_file)
80  {
82  @$file_handle = fopen($a_log_file, 'a+');
83  if ($file_handle == null) {
85  require_once './Services/WorkflowEngine/exceptions/ilWorkflowFilesystemException.php';
86  throw new ilWorkflowFilesystemException('Could not write to filesystem - no pointer returned.', 1002);
87  }
88  fclose($file_handle);
89  }
90 
100  private function checkExtensionValidity($extension)
101  {
102  if ($extension != '.log' && $extension != '.txt') {
104  require_once './Services/WorkflowEngine/exceptions/ilWorkflowObjectStateException.php';
105  throw new ilWorkflowObjectStateException('Illegal extension. Log file must be either .txt or .log.', 1002);
106  }
107  }
108 
114  public function getLogFile()
115  {
116  return $this->log_file;
117  }
118 
126  public function setLogMessage($a_log_message)
127  {
128  $this->checkForExistingLogMessageContent($a_log_message);
129  $this->log_message = $a_log_message;
130  }
131 
141  private function checkForExistingLogMessageContent($a_log_message)
142  {
143  if ($a_log_message == null || $a_log_message == '') {
145  require_once './Services/WorkflowEngine/exceptions/ilWorkflowObjectStateException.php';
146  throw new ilWorkflowObjectStateException('Log message must not be null or empty.', 1002);
147  }
148  }
149 
155  public function getLogMessage()
156  {
157  return $this->log_message;
158  }
159 
171  public function setLogLevel($a_log_level)
172  {
173  $valid = $this->determineValidityOfLogLevel($a_log_level);
174  if ($valid == false) {
176  require_once './Services/WorkflowEngine/exceptions/ilWorkflowObjectStateException.php';
177  throw new ilWorkflowObjectStateException('Log level must be one of: message, warning, debug, info, fatal.', 1002);
178  }
179  $this->log_level = strtoupper($a_log_level);
180  }
181 
190  private function determineValidityOfLogLevel($a_log_level)
191  {
192  switch (strtolower($a_log_level)) {
193  case 'trace':
194  case 'message':
195  case 'warning':
196  case 'debug':
197  case 'info':
198  case 'fatal':
199  $valid = true;
200  break;
201  default:
202  $valid = false;
203  return $valid;
204  }
205  return $valid;
206  }
207 
213  public function getLogLevel()
214  {
215  return $this->log_level;
216  }
217 
223  public function getContext()
224  {
225  return $this->context;
226  }
227 
233  public function execute()
234  {
235  $file_pointer = null;
236  $file_pointer = $this->acquireFilePointer();
237  $this->writeLogMessage($file_pointer);
238  $this->closeFilePointer($file_pointer);
239  }
240 
250  private function closeFilePointer($file_pointer)
251  {
252  if (!fclose($file_pointer)) {
254  require_once './Services/WorkflowEngine/exceptions/ilWorkflowFilesystemException.php';
255  throw new ilWorkflowFilesystemException('Cannot write to filesystem - pointer returned did not close.', 1001);
256  }
257  }
258 
266  private function writeLogMessage($file_pointer)
267  {
269  require_once './Services/WorkflowEngine/classes/utils/class.ilWorkflowUtils.php';
270  fwrite($file_pointer, date('Y/m/d H:i:s') . substr((string) ilWorkflowUtils::microtime(), 1, 6) . ' :: ');
271  fwrite($file_pointer, $this->log_level . ' :: ');
272  fwrite($file_pointer, $this->log_message . "\r\n");
273  }
274 
282  private function acquireFilePointer()
283  {
284  $file_pointer = fopen($this->log_file, 'a');
285  if ($file_pointer == null) {
287  require_once './Services/WorkflowEngine/exceptions/ilWorkflowFilesystemException.php';
288  throw new ilWorkflowFilesystemException('Cannot write to filesystem - no pointer returned.', 1000);
289  }
290  return $file_pointer;
291  }
292 
293  public function setName($name)
294  {
295  $this->name = $name;
296  }
297 
298  public function getName()
299  {
300  return $this->name;
301  }
302 }
getLogFile()
Returns the log file name and path.
acquireFilePointer()
Acquires and returns a file pointer to the instances log file.
ilWorkflowEngineElement Interface is part of the petri net based workflow engine. ...
getLogMessage()
Returns the currently set log message.
$valid
PhpIncludeInspection
closeFilePointer($file_pointer)
Closes the file pointer.
setLogLevel($a_log_level)
Sets the log level of the message to be logged.
execute()
Executes this action according to its settings.
getContext()
Returns the parent object.
PhpIncludeInspection
Definition: ilNode.php:25
getLogLevel()
Returns the currently set log level.
__construct(ilNode $a_context)
Default constructor.
setLogFile($a_log_file)
Sets the log file name and path.
checkExtensionValidity($extension)
Checks if the given extension is a listed one.
checkFileWriteability($a_log_file)
Checks if the file is "really really" writeable.
writeLogMessage($file_pointer)
Writes the instances log message to the logfile.
determineValidityOfLogLevel($a_log_level)
Determines, if the given log level is a valid one.
ilActivity Interface is part of the petri net based workflow engine.
Definition: ilActivity.php:15
checkForExistingLogMessageContent($a_log_message)
Checks if an actual log message is set for the instance.
setLogMessage($a_log_message)
Sets the message to be logged.