示例#1
0
 /**
  * This method writes the $string to a file.
  *
  * The file to which the string will be written depends on the $eventType, $eventSource, and
  * $eventCategory.
  *
  * @throws ezcLogWriterException if it was not possible to write to the log file.
  * @param int $eventType
  * @param string $eventSource
  * @param string $eventCategory
  * @param string $string
  */
 protected function write($eventType, $eventSource, $eventCategory, $string)
 {
     $fileHandles = $this->fileMap->get($eventType, $eventSource, $eventCategory);
     if (count($fileHandles) > 0) {
         $failure = false;
         foreach ($fileHandles as $filename => $fh) {
             if (fwrite($fh, $string) === false) {
                 $failure = $filename;
             }
         }
         if ($failure) {
             throw new ezcLogWriterException(new ezcBaseFileIoException($failure, ezcBaseFileIoException::WRITE));
         }
     } else {
         if (!is_null($this->defaultFile)) {
             $path = $this->logDirectory . "/" . $this->defaultFile;
             if (fwrite($this->openFiles[$path], $string) === false) {
                 throw new ezcLogWriterException(new ezcBaseFileIoException($this->defaultFile, ezcBaseFileIoException::WRITE));
             }
         }
     }
 }
 /**
  * Writes the message $message to the log.
  *
  * The writer can use the severity, source, and category to filter the
  * incoming messages and determine the location where the messages should
  * be written.
  *
  * $optional may contain extra information that can be added to the log. For example:
  * line numbers, file names, usernames, etc.
  *
  * @throws ezcLogWriterException
  *         If the log writer was unable to write the log message
  * @param string $message
  * @param int $severity
  *        ezcLog:: DEBUG, SUCCES_AUDIT, FAILED_AUDIT, INFO, NOTICE, WARNING, ERROR or FATAL.
  * $param string $source
  * @param string $category
  * @param array(string=>string) $optional
  */
 public function writeLogMessage($message, $severity, $source, $category, $optional = array())
 {
     $severityName = ezcLog::translateSeverityName($severity);
     $tables = $this->map->get($severity, $source, $category);
     $query = $this->db->createSelectQuery();
     if (count($tables) > 0) {
         foreach ($tables as $t) {
             try {
                 $q = $this->db->createInsertQuery();
                 $q->insertInto($t)->set($this->message, $q->bindValue($message))->set($this->severity, $q->bindValue($severityName))->set($this->source, $q->bindValue($source))->set($this->category, $q->bindValue($category))->set($this->datetime, $query->expr->now());
                 foreach ($optional as $key => $val) {
                     $q->set(isset($this->additionalColumns[$key]) ? $this->additionalColumns[$key] : $key, $q->bindValue($val));
                 }
                 $stmt = $q->prepare();
                 $stmt->execute();
             } catch (PDOException $e) {
                 throw new ezcLogWriterException($e);
             }
         }
     } else {
         if ($this->defaultTable !== false) {
             try {
                 $q = $this->db->createInsertQuery();
                 $q->insertInto($this->defaultTable)->set($this->message, $q->bindValue($message))->set($this->severity, $q->bindValue($severityName))->set($this->source, $q->bindValue($source))->set($this->category, $q->bindValue($category))->set($this->datetime, $query->expr->now());
                 foreach ($optional as $key => $val) {
                     $q->set(isset($this->additionalColumns[$key]) ? $this->additionalColumns[$key] : $key, $q->bindValue($val));
                 }
                 $stmt = $q->prepare();
                 $stmt->execute();
             } catch (PDOException $e) {
                 throw new ezcLogWriterException($e);
             }
         }
     }
 }
示例#3
0
 /**
  * Write the message $message with additional information to one or multiple log writers.
  *
  * The log message $message, severity $severity, and extra attributes $attributes are sent to
  * the writers that matches with the {@link ezcLogFilter}. The following parameters are 
  * taken in the comparation with the ezcLogFilter:
  * - $severity: the severity of the log message.
  * - $attributes[ "source" ]: the source from where the log message comes.
  * - $attributes[ "category" ]: the category of the log message.
  *
  * See for more information about filter matching the classes {@link ezcLog} and 
  * {@link ezcLogFilter}. 
  *
  * The message $message describes what happened. The severity $severity is one of the ezcLog constants:
  * - DEBUG: Records information about the progress in the program and references 
  *   source code functions. Knowledge of the source code is needed to interpret
  *   this log message.
  * - INFO: Informative logging at a detailed level. This logging method produces a
  *   high level of logging, which is unmanageable on a production environment.
  *   Usually INFO logging is only enabled to help by analysing a problem. 
  * - NOTICE: Informative logging at a lower detail level than INFO logging.
  *   Only major stages are recorded and is useful to monitor a low volume system.
  * - WARNING: Something unexpected happened, but did not cause any loss of service.
  * - ERROR: An error occured, which may cause partial loss of service. Usually the
  *   system can recover.
  * - FATAL: An serious error occured and the system is unlikely to recover. 
  * - SUCCESS_AUDIT: Informative logging about a successful completion of work by
  *   a module completed. Useful to trace system changes directly or indirectly 
  *   done by a user.
  * - FAILED_AUDIT: Informative logging about an action from a module
  *   with a negative result. A failed login will most likely added to this severity.
  *
  * The attributes array $attributes can have one or multiple attributes that will
  * be added to the log. If source and category are given, they will override the default
  * source or category given as property to this object. Further more it is up to the 
  * application what to include in the log. It may be useful to add the
  * file and linenumber to the attributes array. Use the magic PHP constants: {@link __FILE__}
  * and {@link __LINE__}  for this purpose. The next example adds an warning to the log.
  *
  * <code>
  * ezcLog::getInstance()->source = "templateEngine"; // Set the default source.
  * ezcLog::getInstance()->log( "ezcPersistentObject <$obj> does not exist.", 
  *     ezcLog::WARNING, 
  *     array( "category" => "Database", "line" => __LINE__, "file" => __FILE__, "code" => 123 )
  *     );
  * </code>
  * 
  * The methods {@link setSeverityAttributes()} and {@link setSourceAttributes()} can automatically
  * add attributes to log messages based on, respectively, the severity and source.
  *
  * See also {@link logHandler()} on how to use {@link trigger_error()} to write log messages.
  *
  * @throws ezcLogWriterException if {@link throwWriterExceptions} are enabled and a log entry 
  *                               could not be written. 
  *
  * @param string $message
  * @param int $severity  One of the following severity constants: 
  *                       DEBUG, SUCCES_AUDIT, FAIL_AUDIT, INFO, NOTICE, WARNING, ERROR, or FATAL.
  * @param array(string=>string) $attributes
  */
 public function log($message, $severity, array $attributes = array())
 {
     $source = isset($attributes["source"]) ? $attributes["source"] : $this->properties["source"];
     $category = isset($attributes["category"]) ? $attributes["category"] : $this->properties["category"];
     unset($attributes["source"]);
     unset($attributes["category"]);
     $attributes = array_merge($this->context->getContext($severity, $source), $attributes);
     $writers = $this->writers->get($severity, $source, $category);
     foreach ($writers as $writer) {
         try {
             $writer->writeLogMessage($message, $severity, $source, $category, $attributes);
         } catch (ezcLogWriterException $e) {
             if ($this->throwWriterExceptions) {
                 throw $e;
             }
         }
     }
 }