From 3628bfe141ef02299beb9514397cfc2c27a7dcb1 Mon Sep 17 00:00:00 2001 From: wei <> Date: Wed, 4 Jan 2006 08:10:56 +0000 Subject: adding TEventLog using ezcLog from ezComponents. --- framework/Log/EventLog/writers/writer_database.php | 216 +++++++++++++++++++++ 1 file changed, 216 insertions(+) create mode 100644 framework/Log/EventLog/writers/writer_database.php (limited to 'framework/Log/EventLog/writers/writer_database.php') diff --git a/framework/Log/EventLog/writers/writer_database.php b/framework/Log/EventLog/writers/writer_database.php new file mode 100644 index 00000000..6908a770 --- /dev/null +++ b/framework/Log/EventLog/writers/writer_database.php @@ -0,0 +1,216 @@ +db = $databaseInstance; + + $this->map = new ezcLogMap(); + $this->defaultTable = $defaultTable; + + $this->message = "message"; + $this->datetime = "time"; + $this->severity = "severity"; + $this->source = "source"; + $this->category = "category"; + } + + /** + * Sets the property $name to $value. + * + * @throws ezcBasePropertyNotFoundException if the property does not exist. + * @param string $name + * @param mixed $value + * @return void + */ + public function __set( $name, $value ) + { + switch ( $name ) + { + case 'table': $this->properties['table'] = $value; break; + case 'message': + case 'datetime': + case 'severity': + case 'source': + case 'category': $this->defaultColumns[ $name ] = $value; break; + default: $this->additionalColumns[ $name ] = $value; break; + } + } + + /** + * Returns the property $name. + * + * @throws ezcBasePropertyNotFoundException if the property does not exist. + * @param string $name + * @return mixed + */ + public function __get( $name ) + { + switch ( $name ) + { + case 'table': return $this->properties['table']; break; + case 'message': + case 'datetime': + case 'severity': + case 'source': + case 'category': return $this->defaultColumns[ $name ]; break; + + default: return $this->additionalColumns[ $name ]; break; + } + } + + /** + * 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 when 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 + * @return void + */ + public function writeLogMessage( $message, $severity, $source, $category, $optional = array() ) + { + $severityName = ezcLog::translateSeverityName( $severity ); + + $colStr = ""; + $valStr = ""; + + if ( is_array( $optional ) ) + { + foreach ( $optional as $key => $val ) + { + $colStr .= ", " . ( isset( $this->additionalColumns[$key] ) ? $this->additionalColumns[$key] : $key ); + $valStr .= ", " . $this->db->quote( $val ); + } + } + + $tables = $this->map->get( $severity, $source, $category ); + + $query = $this->db->createSelectQuery(); + if ( count( $tables ) > 0) + { + foreach ( $tables as $t ) + { + try + { + $this->db->exec( "INSERT INTO `{$t}` ( {$this->message}, {$this->severity}, {$this->source}, {$this->category}, {$this->datetime} $colStr ) ". + "VALUES( ".$this->db->quote( $message ).", ".$this->db->quote( $severityName ).", ".$this->db->quote( $source ).", ". + $this->db->quote( $category ).", ".$query->expr->now()." $valStr )" ); + } + catch ( PDOException $e ) + { + throw new ezcLogWriterException( "SQL query failed in ezcLogWriterDatabase.\n". $e->getMessage() ); + } + } + } + else + { + if ( $this->defaultTable !== false ) + { + try + { + $this->db->exec( "INSERT INTO `{$this->defaultTable}` ( {$this->message}, {$this->severity}, {$this->source}, {$this->category}, {$this->datetime} $colStr ) ". + "VALUES( ".$this->db->quote( $message ).", ".$this->db->quote( $severityName ).", ".$this->db->quote( $source ).", ". + $this->db->quote( $category ).", ".$query->expr->now()." $valStr )" ); + } + catch ( PDOException $e ) + { + throw new ezcLogWriterException( "SQL query failed in ezcLogWriterDatabase.\n". $e->getMessage() ); + } + } + } + } + + /** + * Returns an array that describes the coupling between the logMessage + * information and the columns in the database. + * + * @return array(string=>string) + */ + public function getColumnTranslations() + { + return array_merge( $this->defaultColumns, $this->additionalColumns ); + } + + + /** + * Maps the table $tableName to the messages specified by the {@link ezcLogFilter} $logFilter. + * + * Log messages that matches with the filter are written to the table $tableName. + * This method works the same as {@link ezclog::map()}. + * + * @param ezcLogFilter $logFilter + * @param string $tableName + * @return void + */ + public function map( ezcLogFilter $logFilter, $tableName ) + { + $this->map->map( $logFilter->severity, $logFilter->source, $logFilter->category, $tableName ); + } + + /** + * Unmaps the table $tableName from the messages specified by the {@link ezcLogFilter} $logFilter. + * + * Log messages that matches with the filter are no longer written to the table $tableName. + * This method works the same as {@link ezclog::unmap()}. + * + * @param ezcLogFilter $logFilter + * @param string $fileName + * @return void + */ + public function unmap( ezcLogFilter $logFilter, $tableName ) + { + $this->map->unmap( $logFilter->severity, $logFilter->source, $logFilter->category, $tableName ); + } +} + +?> -- cgit v1.2.3