Example #1
0
 /**
  * Method to add an entry to the log.
  *
  * @param   JLogEntry  $entry  The log entry object to add to the log.
  *
  * @return  boolean  True on success.
  *
  * @since   11.1
  * @throws  RuntimeException
  */
 public function addEntry(JLogEntry $entry)
 {
     // Initialise the file if not already done.
     $this->initFile();
     // Set some default field values if not already set.
     if (!isset($entry->clientIP)) {
         // Check for proxies as well.
         if (isset($_SERVER['REMOTE_ADDR'])) {
             $entry->clientIP = $_SERVER['REMOTE_ADDR'];
         } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
             $entry->clientIP = $_SERVER['HTTP_X_FORWARDED_FOR'];
         } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
             $entry->clientIP = $_SERVER['HTTP_CLIENT_IP'];
         }
     }
     // If the time field is missing or the date field isn't only the date we need to rework it.
     if (strlen($entry->date) != 10 || !isset($entry->time)) {
         // Get the date and time strings in GMT.
         $entry->datetime = $entry->date->toISO8601();
         $entry->time = $entry->date->format('H:i:s', false);
         $entry->date = $entry->date->format('Y-m-d', false);
     }
     // Get a list of all the entry keys and make sure they are upper case.
     $tmp = array_change_key_case(get_object_vars($entry), CASE_UPPER);
     // Decode the entry priority into an English string.
     $tmp['PRIORITY'] = $this->priorities[$entry->priority];
     // Fill in field data for the line.
     $line = $this->format;
     foreach ($this->fields as $field) {
         $line = str_replace('{' . $field . '}', isset($tmp[$field]) ? $tmp[$field] : '-', $line);
     }
     // Write the new entry to the file.
     $line .= "\n";
     if (!JFile::append($this->path, $line)) {
         throw new RuntimeException('Cannot write to log file.');
     }
 }