ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 )
84  {
86  require_once './Services/WorkflowEngine/exceptions/ilWorkflowFilesystemException.php';
87  throw new ilWorkflowFilesystemException( 'Could not write to filesystem - no pointer returned.', 1002 );
88  }
89  fclose( $file_handle );
90  }
91 
101  private function checkExtensionValidity( $extension )
102  {
103  if ( $extension != '.log' && $extension != '.txt' )
104  {
106  require_once './Services/WorkflowEngine/exceptions/ilWorkflowObjectStateException.php';
107  throw new ilWorkflowObjectStateException( 'Illegal extension. Log file must be either .txt or .log.', 1002 );
108  }
109  }
110 
116  public function getLogFile()
117  {
118  return $this->log_file;
119  }
120 
128  public function setLogMessage($a_log_message)
129  {
130  $this->checkForExistingLogMessageContent( $a_log_message );
131  $this->log_message = $a_log_message;
132  }
133 
143  private function checkForExistingLogMessageContent( $a_log_message )
144  {
145  if ( $a_log_message == null || $a_log_message == '' )
146  {
148  require_once './Services/WorkflowEngine/exceptions/ilWorkflowObjectStateException.php';
149  throw new ilWorkflowObjectStateException( 'Log message must not be null or empty.', 1002 );
150  }
151  }
152 
158  public function getLogMessage()
159  {
160  return $this->log_message;
161  }
162 
174  public function setLogLevel($a_log_level)
175  {
176  $valid = $this->determineValidityOfLogLevel( $a_log_level );
177  if ($valid == false)
178  {
180  require_once './Services/WorkflowEngine/exceptions/ilWorkflowObjectStateException.php';
181  throw new ilWorkflowObjectStateException('Log level must be one of: message, warning, debug, info, fatal.', 1002);
182  }
183  $this->log_level = strtoupper($a_log_level);
184  }
185 
194  private function determineValidityOfLogLevel( $a_log_level )
195  {
196  switch ( strtolower( $a_log_level ) )
197  {
198  case 'trace':
199  case 'message':
200  case 'warning':
201  case 'debug':
202  case 'info':
203  case 'fatal':
204  $valid = true;
205  break;
206  default:
207  $valid = false;
208  return $valid;
209  }
210  return $valid;
211  }
212 
218  public function getLogLevel()
219  {
220  return $this->log_level;
221  }
222 
228  public function getContext()
229  {
230  return $this->context;
231  }
232 
238  public function execute()
239  {
240  $file_pointer = null;
241  $file_pointer = $this->acquireFilePointer();
242  $this->writeLogMessage( $file_pointer );
243  $this->closeFilePointer( $file_pointer );
244  }
245 
255  private function closeFilePointer( $file_pointer )
256  {
257  if ( !fclose( $file_pointer ) )
258  {
260  require_once './Services/WorkflowEngine/exceptions/ilWorkflowFilesystemException.php';
261  throw new ilWorkflowFilesystemException( 'Cannot write to filesystem - pointer returned did not close.', 1001 );
262  }
263  }
264 
272  private function writeLogMessage( $file_pointer )
273  {
275  require_once './Services/WorkflowEngine/classes/utils/class.ilWorkflowUtils.php';
276  fwrite( $file_pointer, date( 'Y/m/d H:i:s' ).substr( (string)ilWorkflowUtils::microtime(), 1, 6 ).' :: ' );
277  fwrite( $file_pointer, $this->log_level.' :: ' );
278  fwrite( $file_pointer, $this->log_message."\r\n" );
279  }
280 
288  private function acquireFilePointer()
289  {
290  $file_pointer = fopen( $this->log_file, 'a' );
291  if ( $file_pointer == null )
292  {
294  require_once './Services/WorkflowEngine/exceptions/ilWorkflowFilesystemException.php';
295  throw new ilWorkflowFilesystemException( 'Cannot write to filesystem - no pointer returned.', 1000 );
296  }
297  return $file_pointer;
298  }
299 
300  public function setName($name)
301  {
302  $this->name = $name;
303  }
304 
305  public function getName()
306  {
307  return $this->name;
308  }
309 }
getLogFile()
Returns the log file name and path.
writeLogMessage( $file_pointer)
Writes the instances log message to the logfile.
checkExtensionValidity( $extension)
Checks if the given extension is a listed one.
acquireFilePointer()
Acquires and returns a file pointer to the instances log file.
ilWorkflowEngineElement Interface is part of the petri net based workflow engine. ...
closeFilePointer( $file_pointer)
Closes the file pointer.
getLogMessage()
Returns the currently set log message.
$valid
PhpIncludeInspection
setLogLevel($a_log_level)
Sets the log level of the message to be logged.
execute()
Executes this action according to its settings.
checkFileWriteability( $a_log_file)
Checks if the file is "really really" writeable.
getContext()
Returns the parent object.
date( 'd-M-Y', $objPHPExcel->getProperties() ->getCreated())
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.
determineValidityOfLogLevel( $a_log_level)
Determines, if the given log level is a valid one.
checkForExistingLogMessageContent( $a_log_message)
Checks if an actual log message is set for the instance.
ilActivity Interface is part of the petri net based workflow engine.
Definition: ilActivity.php:15
setLogMessage($a_log_message)
Sets the message to be logged.