/** * Write the logEntries to a file. * * Each line in the log file represents a log message. The log * messages have the following style: * <pre> * MMM dd HH:mm:ss [Severity] [Source] [Category] Message (ExtraInfo) * </pre> * * With: * - MMM: The 3 letter abbreviation of the month. * - dd: The day of the month. * - HH: The hour. * - mm: The minutes. * - ss: The seconds. * * Example: * <pre> * Jan 24 15:32:56 [Debug] [Paynet] [Shop] Connecting to the paynet server (file: paynet_server.php, line: 224). * Jan 24 15:33:01 [Debug] [Paynet] [Shop] Connected with the server (file: paynet_server.php, line: 710). * </pre> * * This method will be called by the {@link ezcLog} class. The $eventSource and $eventCategory are either given * in the {@link ezcLog::log()} method or are the defaults from the {@link ezcLog} class. * * @param string $message * @param int $eventType * @param string $eventSource * @param string $eventCategory * @param array(string=>string) $extraInfo */ public function writeLogMessage($message, $eventType, $eventSource, $eventCategory, $extraInfo = array()) { $extra = ""; if (sizeof($extraInfo) > 0) { $extra = " (" . $this->implodeWithKey(", ", ": ", $extraInfo) . ")"; } if ($eventCategory == false) { $eventCategory = ""; } $logMsg = date("M d H:i:s") . " [" . ezcLog::translateSeverityName($eventType) . "] " . ($eventSource == "" ? "" : "[{$eventSource}] ") . ($eventCategory == "" ? "" : "[{$eventCategory}] ") . "{$message}{$extra}\n"; $this->write($eventType, $eventSource, $eventCategory, $logMsg); }
/** * 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. * * The array $optional contains 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, ezcLog::SUCCESS_AUDIT, ezcLog::FAILED_AUDIT, ezcLog::INFO, ezcLog::NOTICE, * ezcLog::WARNING, ezcLog::ERROR or ezcLog::FATAL. * @param string $source * @param string $category * @param array(string=>string) $extraInfo */ public function writeLogMessage($message, $severity, $source, $category, $extraInfo = array()) { // generate the log message $extra = ""; if (sizeof($extraInfo) > 0) { $extra = " (" . $this->implodeWithKey(", ", ": ", $extraInfo) . ")"; } $logMsg = "[" . ezcLog::translateSeverityName($severity) . "] " . ($source == "" ? "" : "[{$source}] ") . ($category == "" ? "" : "[{$category}] ") . "{$message}{$extra}"; // Map severity to syslog severity $syslogSeverity = LOG_INFO; switch ($severity) { case ezcLog::DEBUG: $syslogSeverity = LOG_DEBUG; break; case ezcLog::SUCCESS_AUDIT: case ezcLog::FAILED_AUDIT: case ezcLog::INFO: $syslogSeverity = LOG_INFO; break; case ezcLog::NOTICE: $syslogSeverity = LOG_NOTICE; break; case ezcLog::WARNING: $syslogSeverity = LOG_WARNING; break; case ezcLog::ERROR: $syslogSeverity = LOG_ERR; break; case ezcLog::FATAL: $syslogSeverity = LOG_CRIT; break; default: $syslogSeverity = LOG_INFO; break; } // write to syslog $success = syslog($syslogSeverity, $logMsg); if (!$success) { throw new ezcLogWriterException(new Exception("Couldn't not write to syslog")); } }
/** * 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); } } } }