You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ih...@apache.org on 2013/11/28 17:04:07 UTC
[37/43] Fixed code formatting to conform to PSR-2
http://git-wip-us.apache.org/repos/asf/logging-log4php/blob/35dfd5d3/src/Appenders/PdoAppender.php
----------------------------------------------------------------------
diff --git a/src/Appenders/PdoAppender.php b/src/Appenders/PdoAppender.php
index 87c81bf..431b9d6 100644
--- a/src/Appenders/PdoAppender.php
+++ b/src/Appenders/PdoAppender.php
@@ -44,244 +44,263 @@ use PDOException;
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @link http://logging.apache.org/log4php/docs/appenders/pdo.html Appender documentation
*/
-class PDOAppender extends AbstractAppender {
-
- // ******************************************
- // *** Configurable parameters ***
- // ******************************************
-
- /**
- * DSN string used to connect to the database.
- * @see http://www.php.net/manual/en/pdo.construct.php
- */
- protected $dsn;
-
- /** Database user name. */
- protected $user;
-
- /** Database password. */
- protected $password;
-
- /**
- * The insert query.
- *
- * The __TABLE__ placeholder will be replaced by the table name from
- * {@link $table}.
- *
- * The questionmarks are part of the prepared statement, and they must
- * match the number of conversion specifiers in {@link insertPattern}.
- */
- protected $insertSQL = "INSERT INTO __TABLE__ (timestamp, logger, level, message, thread, file, line) VALUES (?, ?, ?, ?, ?, ?, ?)";
-
- /**
- * A comma separated list of {@link LoggerPatternLayout} format strings
- * which replace the "?" in {@link $insertSQL}.
- *
- * Must contain the same number of comma separated conversion patterns as
- * there are question marks in {@link insertSQL}.
- *
- * @see LoggerPatternLayout For conversion patterns.
- */
- protected $insertPattern = "%date{Y-m-d H:i:s},%logger,%level,%message,%pid,%file,%line";
-
- /** Name of the table to which to append log events. */
- protected $table = 'log4php_log';
-
- /** The number of recconect attempts to make on failed append. */
- protected $reconnectAttempts = 3;
-
-
- // ******************************************
- // *** Private memebers ***
- // ******************************************
-
- /**
- * The PDO instance.
- * @var PDO
- */
- protected $db;
-
- /**
- * Prepared statement for the insert query.
- * @var PDOStatement
- */
- protected $preparedInsert;
-
- /** This appender does not require a layout. */
- protected $requiresLayout = false;
-
-
- // ******************************************
- // *** Appender methods ***
- // ******************************************
-
- /**
- * Acquires a database connection based on parameters.
- * Parses the insert pattern to create a chain of converters which will be
- * used in forming query parameters from logging events.
- */
- public function activateOptions() {
- try {
- $this->establishConnection();
- } catch (PDOException $e) {
- $this->warn("Failed connecting to database. Closing appender. Error: " . $e->getMessage());
- $this->close();
- return;
- }
-
- // Parse the insert patterns; pattern parts are comma delimited
- $pieces = explode(',', $this->insertPattern);
- $converterMap = PatternLayout::getDefaultConverterMap();
- foreach($pieces as $pattern) {
- $parser = new PatternParser($pattern, $converterMap);
- $this->converters[] = $parser->parse();
- }
-
- $this->closed = false;
- }
-
- /**
- * Connects to the database, and prepares the insert query.
- * @throws PDOException If connect or prepare fails.
- */
- protected function establishConnection() {
- // Acquire database connection
- $this->db = new PDO($this->dsn, $this->user, $this->password);
- $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
-
- // Prepare the insert statement
- $insertSQL = str_replace('__TABLE__', $this->table, $this->insertSQL);
- $this->preparedInsert = $this->db->prepare($insertSQL);
- }
-
- /**
- * Appends a new event to the database.
- *
- * If writing to database fails, it will retry by re-establishing the
- * connection up to $reconnectAttempts times. If writing still fails,
- * the appender will close.
- */
- public function append(LoggingEvent $event) {
-
- for ($attempt = 1; $attempt <= $this->reconnectAttempts + 1; $attempt++) {
- try {
- // Attempt to write to database
- $this->preparedInsert->execute($this->format($event));
- $this->preparedInsert->closeCursor();
- break;
- } catch (PDOException $e) {
- $this->warn("Failed writing to database: ". $e->getMessage());
-
- // Close the appender if it's the last attempt
- if ($attempt > $this->reconnectAttempts) {
- $this->warn("Failed writing to database after {$this->reconnectAttempts} reconnect attempts. Closing appender.");
- $this->close();
- // Otherwise reconnect and try to write again
- } else {
- $this->warn("Attempting a reconnect (attempt $attempt of {$this->reconnectAttempts}).");
- $this->establishConnection();
- }
- }
- }
- }
-
- /**
- * Converts the logging event to a series of database parameters by using
- * the converter chain which was set up on activation.
- */
- protected function format(LoggingEvent $event) {
- $params = array();
- foreach($this->converters as $converter) {
- $buffer = '';
- while ($converter !== null) {
- $converter->format($buffer, $event);
- $converter = $converter->next;
- }
- $params[] = $buffer;
- }
- return $params;
- }
-
- /**
- * Closes the connection to the logging database
- */
- public function close() {
- // Close the connection (if any)
- $this->db = null;
-
- // Close the appender
- $this->closed = true;
- }
-
- // ******************************************
- // *** Accessor methods ***
- // ******************************************
-
- /**
- * Returns the active database handle or null if not established.
- * @return PDO
- */
- public function getDatabaseHandle() {
- return $this->db;
- }
-
- /** Sets the username. */
- public function setUser($user) {
- $this->setString('user', $user);
- }
-
- /** Returns the username. */
- public function getUser($user) {
- return $this->user;
- }
-
- /** Sets the password. */
- public function setPassword($password) {
- $this->setString('password', $password);
- }
-
- /** Returns the password. */
- public function getPassword($password) {
- return $this->password;
- }
-
- /** Sets the insert SQL. */
- public function setInsertSQL($sql) {
- $this->setString('insertSQL', $sql);
- }
-
- /** Returns the insert SQL. */
- public function getInsertSQL($sql) {
- return $this->insertSQL;
- }
-
- /** Sets the insert pattern. */
- public function setInsertPattern($pattern) {
- $this->setString('insertPattern', $pattern);
- }
-
- /** Returns the insert pattern. */
- public function getInsertPattern($pattern) {
- return $this->insertPattern;
- }
-
- /** Sets the table name. */
- public function setTable($table) {
- $this->setString('table', $table);
- }
-
- /** Returns the table name. */
- public function getTable($table) {
- return $this->table;
- }
-
- /** Sets the DSN string. */
- public function setDSN($dsn) {
- $this->setString('dsn', $dsn);
- }
-
- /** Returns the DSN string. */
- public function getDSN($dsn) {
- return $this->setString('dsn', $dsn);
- }
+class PdoAppender extends AbstractAppender
+{
+ // ******************************************
+ // *** Configurable parameters ***
+ // ******************************************
+
+ /**
+ * DSN string used to connect to the database.
+ * @see http://www.php.net/manual/en/pdo.construct.php
+ */
+ protected $dsn;
+
+ /** Database user name. */
+ protected $user;
+
+ /** Database password. */
+ protected $password;
+
+ /**
+ * The insert query.
+ *
+ * The __TABLE__ placeholder will be replaced by the table name from
+ * {@link $table}.
+ *
+ * The questionmarks are part of the prepared statement, and they must
+ * match the number of conversion specifiers in {@link insertPattern}.
+ */
+ protected $insertSQL = "INSERT INTO __TABLE__ (timestamp, logger, level, message, thread, file, line) VALUES (?, ?, ?, ?, ?, ?, ?)";
+
+ /**
+ * A comma separated list of {@link LoggerPatternLayout} format strings
+ * which replace the "?" in {@link $insertSQL}.
+ *
+ * Must contain the same number of comma separated conversion patterns as
+ * there are question marks in {@link insertSQL}.
+ *
+ * @see LoggerPatternLayout For conversion patterns.
+ */
+ protected $insertPattern = "%date{Y-m-d H:i:s},%logger,%level,%message,%pid,%file,%line";
+
+ /** Name of the table to which to append log events. */
+ protected $table = 'log4php_log';
+
+ /** The number of recconect attempts to make on failed append. */
+ protected $reconnectAttempts = 3;
+
+
+ // ******************************************
+ // *** Private memebers ***
+ // ******************************************
+
+ /**
+ * The PDO instance.
+ * @var PDO
+ */
+ protected $db;
+
+ /**
+ * Prepared statement for the insert query.
+ * @var PDOStatement
+ */
+ protected $preparedInsert;
+
+ /** This appender does not require a layout. */
+ protected $requiresLayout = false;
+
+
+ // ******************************************
+ // *** Appender methods ***
+ // ******************************************
+
+ /**
+ * Acquires a database connection based on parameters.
+ * Parses the insert pattern to create a chain of converters which will be
+ * used in forming query parameters from logging events.
+ */
+ public function activateOptions()
+ {
+ try {
+ $this->establishConnection();
+ } catch (PDOException $e) {
+ $this->warn("Failed connecting to database. Closing appender. Error: " . $e->getMessage());
+ $this->close();
+
+ return;
+ }
+
+ // Parse the insert patterns; pattern parts are comma delimited
+ $pieces = explode(',', $this->insertPattern);
+ $converterMap = PatternLayout::getDefaultConverterMap();
+ foreach ($pieces as $pattern) {
+ $parser = new PatternParser($pattern, $converterMap);
+ $this->converters[] = $parser->parse();
+ }
+
+ $this->closed = false;
+ }
+
+ /**
+ * Connects to the database, and prepares the insert query.
+ * @throws PDOException If connect or prepare fails.
+ */
+ protected function establishConnection()
+ {
+ // Acquire database connection
+ $this->db = new PDO($this->dsn, $this->user, $this->password);
+ $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+
+ // Prepare the insert statement
+ $insertSQL = str_replace('__TABLE__', $this->table, $this->insertSQL);
+ $this->preparedInsert = $this->db->prepare($insertSQL);
+ }
+
+ /**
+ * Appends a new event to the database.
+ *
+ * If writing to database fails, it will retry by re-establishing the
+ * connection up to $reconnectAttempts times. If writing still fails,
+ * the appender will close.
+ */
+ public function append(LoggingEvent $event)
+ {
+ for ($attempt = 1; $attempt <= $this->reconnectAttempts + 1; $attempt++) {
+ try {
+ // Attempt to write to database
+ $this->preparedInsert->execute($this->format($event));
+ $this->preparedInsert->closeCursor();
+ break;
+ } catch (PDOException $e) {
+ $this->warn("Failed writing to database: ". $e->getMessage());
+
+ // Close the appender if it's the last attempt
+ if ($attempt > $this->reconnectAttempts) {
+ $this->warn("Failed writing to database after {$this->reconnectAttempts} reconnect attempts. Closing appender.");
+ $this->close();
+ // Otherwise reconnect and try to write again
+ } else {
+ $this->warn("Attempting a reconnect (attempt $attempt of {$this->reconnectAttempts}).");
+ $this->establishConnection();
+ }
+ }
+ }
+ }
+
+ /**
+ * Converts the logging event to a series of database parameters by using
+ * the converter chain which was set up on activation.
+ */
+ protected function format(LoggingEvent $event)
+ {
+ $params = array();
+ foreach ($this->converters as $converter) {
+ $buffer = '';
+ while ($converter !== null) {
+ $converter->format($buffer, $event);
+ $converter = $converter->next;
+ }
+ $params[] = $buffer;
+ }
+
+ return $params;
+ }
+
+ /**
+ * Closes the connection to the logging database
+ */
+ public function close()
+ {
+ // Close the connection (if any)
+ $this->db = null;
+
+ // Close the appender
+ $this->closed = true;
+ }
+
+ // ******************************************
+ // *** Accessor methods ***
+ // ******************************************
+
+ /**
+ * Returns the active database handle or null if not established.
+ * @return PDO
+ */
+ public function getDatabaseHandle()
+ {
+ return $this->db;
+ }
+
+ /** Sets the username. */
+ public function setUser($user)
+ {
+ $this->setString('user', $user);
+ }
+
+ /** Returns the username. */
+ public function getUser($user)
+ {
+ return $this->user;
+ }
+
+ /** Sets the password. */
+ public function setPassword($password)
+ {
+ $this->setString('password', $password);
+ }
+
+ /** Returns the password. */
+ public function getPassword($password)
+ {
+ return $this->password;
+ }
+
+ /** Sets the insert SQL. */
+ public function setInsertSQL($sql)
+ {
+ $this->setString('insertSQL', $sql);
+ }
+
+ /** Returns the insert SQL. */
+ public function getInsertSQL($sql)
+ {
+ return $this->insertSQL;
+ }
+
+ /** Sets the insert pattern. */
+ public function setInsertPattern($pattern)
+ {
+ $this->setString('insertPattern', $pattern);
+ }
+
+ /** Returns the insert pattern. */
+ public function getInsertPattern($pattern)
+ {
+ return $this->insertPattern;
+ }
+
+ /** Sets the table name. */
+ public function setTable($table)
+ {
+ $this->setString('table', $table);
+ }
+
+ /** Returns the table name. */
+ public function getTable($table)
+ {
+ return $this->table;
+ }
+
+ /** Sets the DSN string. */
+ public function setDSN($dsn)
+ {
+ $this->setString('dsn', $dsn);
+ }
+
+ /** Returns the DSN string. */
+ public function getDSN($dsn)
+ {
+ return $this->setString('dsn', $dsn);
+ }
}
http://git-wip-us.apache.org/repos/asf/logging-log4php/blob/35dfd5d3/src/Appenders/PhpAppender.php
----------------------------------------------------------------------
diff --git a/src/Appenders/PhpAppender.php b/src/Appenders/PhpAppender.php
index 287c56a..6d6c6c9 100644
--- a/src/Appenders/PhpAppender.php
+++ b/src/Appenders/PhpAppender.php
@@ -35,16 +35,17 @@ use Apache\Log4php\LoggingEvent;
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @link http://logging.apache.org/log4php/docs/appenders/php.html Appender documentation
*/
-class PhpAppender extends AbstractAppender {
-
- public function append(LoggingEvent $event) {
- $level = $event->getLevel();
- if($level->isGreaterOrEqual(Level::getLevelError())) {
- trigger_error($this->layout->format($event), E_USER_ERROR);
- } else if ($level->isGreaterOrEqual(Level::getLevelWarn())) {
- trigger_error($this->layout->format($event), E_USER_WARNING);
- } else {
- trigger_error($this->layout->format($event), E_USER_NOTICE);
- }
- }
+class PhpAppender extends AbstractAppender
+{
+ public function append(LoggingEvent $event)
+ {
+ $level = $event->getLevel();
+ if ($level->isGreaterOrEqual(Level::getLevelError())) {
+ trigger_error($this->layout->format($event), E_USER_ERROR);
+ } elseif ($level->isGreaterOrEqual(Level::getLevelWarn())) {
+ trigger_error($this->layout->format($event), E_USER_WARNING);
+ } else {
+ trigger_error($this->layout->format($event), E_USER_NOTICE);
+ }
+ }
}
http://git-wip-us.apache.org/repos/asf/logging-log4php/blob/35dfd5d3/src/Appenders/RollingFileAppender.php
----------------------------------------------------------------------
diff --git a/src/Appenders/RollingFileAppender.php b/src/Appenders/RollingFileAppender.php
index 8d54a4a..8e7024b 100644
--- a/src/Appenders/RollingFileAppender.php
+++ b/src/Appenders/RollingFileAppender.php
@@ -18,8 +18,6 @@
namespace Apache\Log4php\Appenders;
-use Apache\Log4php\LoggingEvent;
-
/**
* RollingFileAppender writes logging events to a specified file. The
* file is rolled over after a specified size has been reached.
@@ -40,264 +38,280 @@ use Apache\Log4php\LoggingEvent;
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @link http://logging.apache.org/log4php/docs/appenders/rolling-file.html Appender documentation
*/
-class RollingFileAppender extends FileAppender {
-
- /** Compressing backup files is done in chunks, this determines how large. */
- const COMPRESS_CHUNK_SIZE = 102400; // 100KB
-
- /**
- * The maximum size (in bytes) that the output file is allowed to reach
- * before being rolled over to backup files.
- *
- * The default maximum file size is 10MB (10485760 bytes). Maximum value
- * for this option may depend on the file system.
- *
- * @var integer
- */
- protected $maxFileSize = 10485760;
-
- /**
- * Set the maximum number of backup files to keep around.
- *
- * Determines how many backup files are kept before the oldest is erased.
- * This option takes a positive integer value. If set to zero, then there
- * will be no backup files and the log file will be truncated when it
- * reaches <var>maxFileSize</var>.
- *
- * There is one backup file by default.
- *
- * @var integer
- */
- protected $maxBackupIndex = 1;
-
- /**
- * The <var>compress</var> parameter determindes the compression with zlib.
- * If set to true, the rollover files are compressed and saved with the .gz extension.
- * @var boolean
- */
- protected $compress = false;
-
- /**
- * Set to true in the constructor if PHP >= 5.3.0. In that case clearstatcache
- * supports conditional clearing of statistics.
- * @var boolean
- * @see http://php.net/manual/en/function.clearstatcache.php
- */
- private $clearConditional = false;
-
- /**
- * Get the maximum size that the output file is allowed to reach
- * before being rolled over to backup files.
- * @return integer
- */
- public function getMaximumFileSize() {
- return $this->maxFileSize;
- }
-
- public function __construct($name = '') {
- parent::__construct($name);
- if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
- $this->clearConditional = true;
- }
- }
-
- /**
- * Implements the usual roll over behaviour.
- *
- * If MaxBackupIndex is positive, then files File.1, ..., File.MaxBackupIndex -1 are renamed to File.2, ..., File.MaxBackupIndex.
- * Moreover, File is renamed File.1 and closed. A new File is created to receive further log output.
- *
- * If MaxBackupIndex is equal to zero, then the File is truncated with no backup files created.
- *
- * Rollover must be called while the file is locked so that it is safe for concurrent access.
- *
- * @throws LoggerException If any part of the rollover procedure fails.
- */
- private function rollOver() {
- // If maxBackups <= 0, then there is no file renaming to be done.
- if($this->maxBackupIndex > 0) {
- // Delete the oldest file, to keep Windows happy.
- $file = $this->file . '.' . $this->maxBackupIndex;
-
- if (file_exists($file) && !unlink($file)) {
- throw new LoggerException("Unable to delete oldest backup file from [$file].");
- }
-
- // Map {(maxBackupIndex - 1), ..., 2, 1} to {maxBackupIndex, ..., 3, 2}
- $this->renameArchievedLogs($this->file);
-
- // Backup the active file
- $this->moveToBackup($this->file);
- }
-
- // Truncate the active file
- ftruncate($this->fp, 0);
- rewind($this->fp);
- }
-
- private function moveToBackup($source) {
- if ($this->compress) {
- $target = $source . '.1.gz';
- $this->compressFile($source, $target);
- } else {
- $target = $source . '.1';
- copy($source, $target);
- }
- }
-
- private function compressFile($source, $target) {
- $target = 'compress.zlib://' . $target;
-
- $fin = fopen($source, 'rb');
- if ($fin === false) {
- throw new LoggerException("Unable to open file for reading: [$source].");
- }
-
- $fout = fopen($target, 'wb');
- if ($fout === false) {
- throw new LoggerException("Unable to open file for writing: [$target].");
- }
-
- while (!feof($fin)) {
- $chunk = fread($fin, self::COMPRESS_CHUNK_SIZE);
- if (false === fwrite($fout, $chunk)) {
- throw new LoggerException("Failed writing to compressed file.");
- }
- }
-
- fclose($fin);
- fclose($fout);
- }
-
- private function renameArchievedLogs($fileName) {
- for($i = $this->maxBackupIndex - 1; $i >= 1; $i--) {
-
- $source = $fileName . "." . $i;
- if ($this->compress) {
- $source .= '.gz';
- }
-
- if(file_exists($source)) {
- $target = $fileName . '.' . ($i + 1);
- if ($this->compress) {
- $target .= '.gz';
- }
-
- rename($source, $target);
- }
- }
- }
-
- /**
- * Writes a string to the target file. Opens file if not already open.
- * @param string $string Data to write.
- */
- protected function write($string) {
- // Lazy file open
- if(!isset($this->fp)) {
- if ($this->openFile() === false) {
- return; // Do not write if file open failed.
- }
- }
-
- // Lock the file while writing and possible rolling over
- if(flock($this->fp, LOCK_EX)) {
-
- // Write to locked file
- if(fwrite($this->fp, $string) === false) {
- $this->warn("Failed writing to file. Closing appender.");
- $this->closed = true;
- }
-
- // Stats cache must be cleared, otherwise filesize() returns cached results
- // If supported (PHP 5.3+), clear only the state cache for the target file
- if ($this->clearConditional) {
- clearstatcache(true, $this->file);
- } else {
- clearstatcache();
- }
-
- // Rollover if needed
- if (filesize($this->file) > $this->maxFileSize) {
- try {
- $this->rollOver();
- } catch (LoggerException $ex) {
- $this->warn("Rollover failed: " . $ex->getMessage() . " Closing appender.");
- $this->closed = true;
- }
- }
-
- flock($this->fp, LOCK_UN);
- } else {
- $this->warn("Failed locking file for writing. Closing appender.");
- $this->closed = true;
- }
- }
-
- public function activateOptions() {
- parent::activateOptions();
-
- if ($this->compress && !extension_loaded('zlib')) {
- $this->warn("The 'zlib' extension is required for file compression. Disabling compression.");
- $this->compression = false;
- }
- }
-
- /**
- * Set the 'maxBackupIndex' parameter.
- * @param integer $maxBackupIndex
- */
- public function setMaxBackupIndex($maxBackupIndex) {
- $this->setPositiveInteger('maxBackupIndex', $maxBackupIndex);
- }
-
- /**
- * Returns the 'maxBackupIndex' parameter.
- * @return integer
- */
- public function getMaxBackupIndex() {
- return $this->maxBackupIndex;
- }
-
- /**
- * Set the 'maxFileSize' parameter.
- * @param mixed $maxFileSize
- */
- public function setMaxFileSize($maxFileSize) {
- $this->setFileSize('maxFileSize', $maxFileSize);
- }
-
- /**
- * Returns the 'maxFileSize' parameter.
- * @return integer
- */
- public function getMaxFileSize() {
- return $this->maxFileSize;
- }
-
- /**
- * Set the 'maxFileSize' parameter (kept for backward compatibility).
- * @param mixed $maxFileSize
- * @deprecated Use setMaxFileSize() instead.
- */
- public function setMaximumFileSize($maxFileSize) {
- $this->warn("The 'maximumFileSize' parameter is deprecated. Use 'maxFileSize' instead.");
- return $this->setMaxFileSize($maxFileSize);
- }
-
- /**
- * Sets the 'compress' parameter.
- * @param boolean $compress
- */
- public function setCompress($compress) {
- $this->setBoolean('compress', $compress);
- }
-
- /**
- * Returns the 'compress' parameter.
- * @param boolean
- */
- public function getCompress() {
- return $this->compress;
- }
+class RollingFileAppender extends FileAppender
+{
+ /** Compressing backup files is done in chunks, this determines how large. */
+ const COMPRESS_CHUNK_SIZE = 102400; // 100KB
+
+ /**
+ * The maximum size (in bytes) that the output file is allowed to reach
+ * before being rolled over to backup files.
+ *
+ * The default maximum file size is 10MB (10485760 bytes). Maximum value
+ * for this option may depend on the file system.
+ *
+ * @var integer
+ */
+ protected $maxFileSize = 10485760;
+
+ /**
+ * Set the maximum number of backup files to keep around.
+ *
+ * Determines how many backup files are kept before the oldest is erased.
+ * This option takes a positive integer value. If set to zero, then there
+ * will be no backup files and the log file will be truncated when it
+ * reaches <var>maxFileSize</var>.
+ *
+ * There is one backup file by default.
+ *
+ * @var integer
+ */
+ protected $maxBackupIndex = 1;
+
+ /**
+ * The <var>compress</var> parameter determindes the compression with zlib.
+ * If set to true, the rollover files are compressed and saved with the .gz extension.
+ * @var boolean
+ */
+ protected $compress = false;
+
+ /**
+ * Set to true in the constructor if PHP >= 5.3.0. In that case clearstatcache
+ * supports conditional clearing of statistics.
+ * @var boolean
+ * @see http://php.net/manual/en/function.clearstatcache.php
+ */
+ private $clearConditional = false;
+
+ /**
+ * Get the maximum size that the output file is allowed to reach
+ * before being rolled over to backup files.
+ * @return integer
+ */
+ public function getMaximumFileSize()
+ {
+ return $this->maxFileSize;
+ }
+
+ public function __construct($name = '')
+ {
+ parent::__construct($name);
+ if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
+ $this->clearConditional = true;
+ }
+ }
+
+ /**
+ * Implements the usual roll over behaviour.
+ *
+ * If MaxBackupIndex is positive, then files File.1, ..., File.MaxBackupIndex -1 are renamed to File.2, ..., File.MaxBackupIndex.
+ * Moreover, File is renamed File.1 and closed. A new File is created to receive further log output.
+ *
+ * If MaxBackupIndex is equal to zero, then the File is truncated with no backup files created.
+ *
+ * Rollover must be called while the file is locked so that it is safe for concurrent access.
+ *
+ * @throws LoggerException If any part of the rollover procedure fails.
+ */
+ private function rollOver()
+ {
+ // If maxBackups <= 0, then there is no file renaming to be done.
+ if ($this->maxBackupIndex > 0) {
+ // Delete the oldest file, to keep Windows happy.
+ $file = $this->file . '.' . $this->maxBackupIndex;
+
+ if (file_exists($file) && !unlink($file)) {
+ throw new LoggerException("Unable to delete oldest backup file from [$file].");
+ }
+
+ // Map {(maxBackupIndex - 1), ..., 2, 1} to {maxBackupIndex, ..., 3, 2}
+ $this->renameArchievedLogs($this->file);
+
+ // Backup the active file
+ $this->moveToBackup($this->file);
+ }
+
+ // Truncate the active file
+ ftruncate($this->fp, 0);
+ rewind($this->fp);
+ }
+
+ private function moveToBackup($source)
+ {
+ if ($this->compress) {
+ $target = $source . '.1.gz';
+ $this->compressFile($source, $target);
+ } else {
+ $target = $source . '.1';
+ copy($source, $target);
+ }
+ }
+
+ private function compressFile($source, $target)
+ {
+ $target = 'compress.zlib://' . $target;
+
+ $fin = fopen($source, 'rb');
+ if ($fin === false) {
+ throw new LoggerException("Unable to open file for reading: [$source].");
+ }
+
+ $fout = fopen($target, 'wb');
+ if ($fout === false) {
+ throw new LoggerException("Unable to open file for writing: [$target].");
+ }
+
+ while (!feof($fin)) {
+ $chunk = fread($fin, self::COMPRESS_CHUNK_SIZE);
+ if (false === fwrite($fout, $chunk)) {
+ throw new LoggerException("Failed writing to compressed file.");
+ }
+ }
+
+ fclose($fin);
+ fclose($fout);
+ }
+
+ private function renameArchievedLogs($fileName)
+ {
+ for ($i = $this->maxBackupIndex - 1; $i >= 1; $i--) {
+
+ $source = $fileName . "." . $i;
+ if ($this->compress) {
+ $source .= '.gz';
+ }
+
+ if (file_exists($source)) {
+ $target = $fileName . '.' . ($i + 1);
+ if ($this->compress) {
+ $target .= '.gz';
+ }
+
+ rename($source, $target);
+ }
+ }
+ }
+
+ /**
+ * Writes a string to the target file. Opens file if not already open.
+ * @param string $string Data to write.
+ */
+ protected function write($string)
+ {
+ // Lazy file open
+ if (!isset($this->fp)) {
+ if ($this->openFile() === false) {
+ return; // Do not write if file open failed.
+ }
+ }
+
+ // Lock the file while writing and possible rolling over
+ if (flock($this->fp, LOCK_EX)) {
+
+ // Write to locked file
+ if (fwrite($this->fp, $string) === false) {
+ $this->warn("Failed writing to file. Closing appender.");
+ $this->closed = true;
+ }
+
+ // Stats cache must be cleared, otherwise filesize() returns cached results
+ // If supported (PHP 5.3+), clear only the state cache for the target file
+ if ($this->clearConditional) {
+ clearstatcache(true, $this->file);
+ } else {
+ clearstatcache();
+ }
+
+ // Rollover if needed
+ if (filesize($this->file) > $this->maxFileSize) {
+ try {
+ $this->rollOver();
+ } catch (LoggerException $ex) {
+ $this->warn("Rollover failed: " . $ex->getMessage() . " Closing appender.");
+ $this->closed = true;
+ }
+ }
+
+ flock($this->fp, LOCK_UN);
+ } else {
+ $this->warn("Failed locking file for writing. Closing appender.");
+ $this->closed = true;
+ }
+ }
+
+ public function activateOptions()
+ {
+ parent::activateOptions();
+
+ if ($this->compress && !extension_loaded('zlib')) {
+ $this->warn("The 'zlib' extension is required for file compression. Disabling compression.");
+ $this->compression = false;
+ }
+ }
+
+ /**
+ * Set the 'maxBackupIndex' parameter.
+ * @param integer $maxBackupIndex
+ */
+ public function setMaxBackupIndex($maxBackupIndex)
+ {
+ $this->setPositiveInteger('maxBackupIndex', $maxBackupIndex);
+ }
+
+ /**
+ * Returns the 'maxBackupIndex' parameter.
+ * @return integer
+ */
+ public function getMaxBackupIndex()
+ {
+ return $this->maxBackupIndex;
+ }
+
+ /**
+ * Set the 'maxFileSize' parameter.
+ * @param mixed $maxFileSize
+ */
+ public function setMaxFileSize($maxFileSize)
+ {
+ $this->setFileSize('maxFileSize', $maxFileSize);
+ }
+
+ /**
+ * Returns the 'maxFileSize' parameter.
+ * @return integer
+ */
+ public function getMaxFileSize()
+ {
+ return $this->maxFileSize;
+ }
+
+ /**
+ * Set the 'maxFileSize' parameter (kept for backward compatibility).
+ * @param mixed $maxFileSize
+ * @deprecated Use setMaxFileSize() instead.
+ */
+ public function setMaximumFileSize($maxFileSize)
+ {
+ $this->warn("The 'maximumFileSize' parameter is deprecated. Use 'maxFileSize' instead.");
+
+ return $this->setMaxFileSize($maxFileSize);
+ }
+
+ /**
+ * Sets the 'compress' parameter.
+ * @param boolean $compress
+ */
+ public function setCompress($compress)
+ {
+ $this->setBoolean('compress', $compress);
+ }
+
+ /**
+ * Returns the 'compress' parameter.
+ * @param boolean
+ */
+ public function getCompress()
+ {
+ return $this->compress;
+ }
}
http://git-wip-us.apache.org/repos/asf/logging-log4php/blob/35dfd5d3/src/Appenders/SocketAppender.php
----------------------------------------------------------------------
diff --git a/src/Appenders/SocketAppender.php b/src/Appenders/SocketAppender.php
index 024c91f..4f8325b 100644
--- a/src/Appenders/SocketAppender.php
+++ b/src/Appenders/SocketAppender.php
@@ -35,89 +35,100 @@ use Apache\Log4php\LoggingEvent;
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @link http://logging.apache.org/log4php/docs/appenders/socket.html Appender documentation
*/
-class SocketAppender extends AbstractAppender {
-
- /**
- * Target host.
- * @see http://php.net/manual/en/function.fsockopen.php
- */
- protected $remoteHost;
-
- /** Target port */
- protected $port = 4446;
-
- /** Connection timeout in ms. */
- protected $timeout;
-
- // ******************************************
- // *** Appender methods ***
- // ******************************************
-
- /** Override the default layout to use serialized. */
- public function getDefaultLayout() {
- return new SerializedLayout();
- }
-
- public function activateOptions() {
- if (empty($this->remoteHost)) {
- $this->warn("Required parameter [remoteHost] not set. Closing appender.");
- $this->closed = true;
- return;
- }
-
- if (empty($this->timeout)) {
- $this->timeout = ini_get("default_socket_timeout");
- }
-
- $this->closed = false;
- }
-
- public function append(LoggingEvent $event) {
- $socket = fsockopen($this->remoteHost, $this->port, $errno, $errstr, $this->timeout);
- if ($socket === false) {
- $this->warn("Could not open socket to {$this->remoteHost}:{$this->port}. Closing appender.");
- $this->closed = true;
- return;
- }
-
- if (false === fwrite($socket, $this->layout->format($event))) {
- $this->warn("Error writing to socket. Closing appender.");
- $this->closed = true;
- }
- fclose($socket);
- }
-
- // ******************************************
- // *** Accessor methods ***
- // ******************************************
-
- /** Sets the target host. */
- public function setRemoteHost($hostname) {
- $this->setString('remoteHost', $hostname);
- }
-
- /** Sets the target port */
- public function setPort($port) {
- $this->setPositiveInteger('port', $port);
- }
-
- /** Sets the timeout. */
- public function setTimeout($timeout) {
- $this->setPositiveInteger('timeout', $timeout);
- }
-
- /** Returns the target host. */
- public function getRemoteHost() {
- return $this->getRemoteHost();
- }
-
- /** Returns the target port. */
- public function getPort() {
- return $this->port;
- }
-
- /** Returns the timeout */
- public function getTimeout() {
- return $this->timeout;
- }
+class SocketAppender extends AbstractAppender
+{
+ /**
+ * Target host.
+ * @see http://php.net/manual/en/function.fsockopen.php
+ */
+ protected $remoteHost;
+
+ /** Target port */
+ protected $port = 4446;
+
+ /** Connection timeout in ms. */
+ protected $timeout;
+
+ // ******************************************
+ // *** Appender methods ***
+ // ******************************************
+
+ /** Override the default layout to use serialized. */
+ public function getDefaultLayout()
+ {
+ return new SerializedLayout();
+ }
+
+ public function activateOptions()
+ {
+ if (empty($this->remoteHost)) {
+ $this->warn("Required parameter [remoteHost] not set. Closing appender.");
+ $this->closed = true;
+
+ return;
+ }
+
+ if (empty($this->timeout)) {
+ $this->timeout = ini_get("default_socket_timeout");
+ }
+
+ $this->closed = false;
+ }
+
+ public function append(LoggingEvent $event)
+ {
+ $socket = fsockopen($this->remoteHost, $this->port, $errno, $errstr, $this->timeout);
+ if ($socket === false) {
+ $this->warn("Could not open socket to {$this->remoteHost}:{$this->port}. Closing appender.");
+ $this->closed = true;
+
+ return;
+ }
+
+ if (false === fwrite($socket, $this->layout->format($event))) {
+ $this->warn("Error writing to socket. Closing appender.");
+ $this->closed = true;
+ }
+ fclose($socket);
+ }
+
+ // ******************************************
+ // *** Accessor methods ***
+ // ******************************************
+
+ /** Sets the target host. */
+ public function setRemoteHost($hostname)
+ {
+ $this->setString('remoteHost', $hostname);
+ }
+
+ /** Sets the target port */
+ public function setPort($port)
+ {
+ $this->setPositiveInteger('port', $port);
+ }
+
+ /** Sets the timeout. */
+ public function setTimeout($timeout)
+ {
+ $this->setPositiveInteger('timeout', $timeout);
+ }
+
+ /** Returns the target host. */
+ public function getRemoteHost()
+ {
+ return $this->getRemoteHost();
+ }
+
+ /** Returns the target port. */
+ public function getPort()
+ {
+ return $this->port;
+ }
+
+ /** Returns the timeout */
+ public function getTimeout()
+ {
+ return $this->timeout;
+ }
}
http://git-wip-us.apache.org/repos/asf/logging-log4php/blob/35dfd5d3/src/Appenders/SyslogAppender.php
----------------------------------------------------------------------
diff --git a/src/Appenders/SyslogAppender.php b/src/Appenders/SyslogAppender.php
index ae7351b..b6fc0c0 100644
--- a/src/Appenders/SyslogAppender.php
+++ b/src/Appenders/SyslogAppender.php
@@ -70,235 +70,253 @@ use Apache\Log4php\LoggingEvent;
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @link http://logging.apache.org/log4php/docs/appenders/syslog.html Appender documentation
*/
-class SyslogAppender extends AbstractAppender {
-
- /**
- * The ident string is added to each message. Typically the name of your application.
- *
- * @var string
- */
- protected $ident = "Apache log4php";
-
- /**
- * The syslog priority to use when overriding priority. This setting is
- * required if {@link overridePriority} is set to true.
- *
- * @var string
- */
- protected $priority;
-
- /**
- * The option used when opening the syslog connection.
- *
- * @var string
- */
- protected $option = 'PID|CONS';
-
- /**
- * The facility value indicates the source of the message.
- *
- * @var string
- */
- protected $facility = 'USER';
-
- /**
- * If set to true, the message priority will always use the value defined
- * in {@link $priority}, otherwise the priority will be determined by the
- * message's log level.
- *
- * @var string
- */
- protected $overridePriority = false;
-
- /**
- * Holds the int value of the {@link $priority}.
- * @var int
- */
- private $intPriority;
-
- /**
- * Holds the int value of the {@link $facility}.
- * @var int
- */
- private $intFacility;
-
- /**
- * Holds the int value of the {@link $option}.
- * @var int
- */
- private $intOption;
-
- /**
- * Sets the {@link $ident}.
- *
- * @param string $ident
- */
- public function setIdent($ident) {
- $this->ident = $ident;
- }
-
- /**
- * Sets the {@link $priority}.
- *
- * @param string $priority
- */
- public function setPriority($priority) {
- $this->priority = $priority;
- }
-
- /**
- * Sets the {@link $facility}.
- *
- * @param string $facility
- */
- public function setFacility($facility) {
- $this->facility = $facility;
- }
-
- /**
- * Sets the {@link $overridePriority}.
- *
- * @param string $overridePriority
- */
- public function setOverridePriority($overridePriority) {
- $this->overridePriority = $overridePriority;
- }
-
- /**
- * Sets the 'option' parameter.
- *
- * @param string $option
- */
- public function setOption($option) {
- $this->option = $option;
- }
-
- /**
- * Returns the 'ident' parameter.
- *
- * @return string $ident
- */
- public function getIdent() {
- return $this->ident;
- }
-
- /**
- * Returns the 'priority' parameter.
- *
- * @return string
- */
- public function getPriority() {
- return $this->priority;
- }
-
- /**
- * Returns the 'facility' parameter.
- *
- * @return string
- */
- public function getFacility() {
- return $this->facility;
- }
-
- /**
- * Returns the 'overridePriority' parameter.
- *
- * @return string
- */
- public function getOverridePriority() {
- return $this->overridePriority;
- }
-
- /**
- * Returns the 'option' parameter.
- *
- * @return string
- */
- public function getOption() {
- return $this->option;
- }
-
-
- public function activateOptions() {
- $this->intPriority = $this->parsePriority();
- $this->intOption = $this->parseOption();
- $this->intFacility = $this->parseFacility();
-
- $this->closed = false;
- }
-
- public function close() {
- if($this->closed != true) {
- closelog();
- $this->closed = true;
- }
- }
-
- /**
- * Appends the event to syslog.
- *
- * Log is opened and closed each time because if it is not closed, it
- * can cause the Apache httpd server to log to whatever ident/facility
- * was used in openlog().
- *
- * @see http://www.php.net/manual/en/function.syslog.php#97843
- */
- public function append(LoggingEvent $event) {
- $priority = $this->getSyslogPriority($event->getLevel());
- $message = $this->layout->format($event);
-
- openlog($this->ident, $this->intOption, $this->intFacility);
- syslog($priority, $message);
- closelog();
- }
-
- /** Determines which syslog priority to use based on the given level. */
- private function getSyslogPriority(Level $level) {
- if($this->overridePriority) {
- return $this->intPriority;
- }
- return $level->getSyslogEquivalent();
- }
-
- /** Parses a syslog option string and returns the correspodning int value. */
- private function parseOption() {
- $value = 0;
- $options = explode('|', $this->option);
-
- foreach($options as $option) {
- if (!empty($option)) {
- $constant = "LOG_" . trim($option);
- if (defined($constant)) {
- $value |= constant($constant);
- } else {
- trigger_error("log4php: Invalid syslog option provided: $option. Whole option string: {$this->option}.", E_USER_WARNING);
- }
- }
- }
- return $value;
- }
-
- /** Parses the facility string and returns the corresponding int value. */
- private function parseFacility() {
- if (!empty($this->facility)) {
- $constant = "LOG_" . trim($this->facility);
- if (defined($constant)) {
- return constant($constant);
- } else {
- trigger_error("log4php: Invalid syslog facility provided: {$this->facility}.", E_USER_WARNING);
- }
- }
- }
-
- /** Parses the priority string and returns the corresponding int value. */
- private function parsePriority() {
- if (!empty($this->priority)) {
- $constant = "LOG_" . trim($this->priority);
- if (defined($constant)) {
- return constant($constant);
- } else {
- trigger_error("log4php: Invalid syslog priority provided: {$this->priority}.", E_USER_WARNING);
- }
- }
- }
+class SyslogAppender extends AbstractAppender
+{
+ /**
+ * The ident string is added to each message. Typically the name of your application.
+ *
+ * @var string
+ */
+ protected $ident = "Apache log4php";
+
+ /**
+ * The syslog priority to use when overriding priority. This setting is
+ * required if {@link overridePriority} is set to true.
+ *
+ * @var string
+ */
+ protected $priority;
+
+ /**
+ * The option used when opening the syslog connection.
+ *
+ * @var string
+ */
+ protected $option = 'PID|CONS';
+
+ /**
+ * The facility value indicates the source of the message.
+ *
+ * @var string
+ */
+ protected $facility = 'USER';
+
+ /**
+ * If set to true, the message priority will always use the value defined
+ * in {@link $priority}, otherwise the priority will be determined by the
+ * message's log level.
+ *
+ * @var string
+ */
+ protected $overridePriority = false;
+
+ /**
+ * Holds the int value of the {@link $priority}.
+ * @var int
+ */
+ private $intPriority;
+
+ /**
+ * Holds the int value of the {@link $facility}.
+ * @var int
+ */
+ private $intFacility;
+
+ /**
+ * Holds the int value of the {@link $option}.
+ * @var int
+ */
+ private $intOption;
+
+ /**
+ * Sets the {@link $ident}.
+ *
+ * @param string $ident
+ */
+ public function setIdent($ident)
+ {
+ $this->ident = $ident;
+ }
+
+ /**
+ * Sets the {@link $priority}.
+ *
+ * @param string $priority
+ */
+ public function setPriority($priority)
+ {
+ $this->priority = $priority;
+ }
+
+ /**
+ * Sets the {@link $facility}.
+ *
+ * @param string $facility
+ */
+ public function setFacility($facility)
+ {
+ $this->facility = $facility;
+ }
+
+ /**
+ * Sets the {@link $overridePriority}.
+ *
+ * @param string $overridePriority
+ */
+ public function setOverridePriority($overridePriority)
+ {
+ $this->overridePriority = $overridePriority;
+ }
+
+ /**
+ * Sets the 'option' parameter.
+ *
+ * @param string $option
+ */
+ public function setOption($option)
+ {
+ $this->option = $option;
+ }
+
+ /**
+ * Returns the 'ident' parameter.
+ *
+ * @return string $ident
+ */
+ public function getIdent()
+ {
+ return $this->ident;
+ }
+
+ /**
+ * Returns the 'priority' parameter.
+ *
+ * @return string
+ */
+ public function getPriority()
+ {
+ return $this->priority;
+ }
+
+ /**
+ * Returns the 'facility' parameter.
+ *
+ * @return string
+ */
+ public function getFacility()
+ {
+ return $this->facility;
+ }
+
+ /**
+ * Returns the 'overridePriority' parameter.
+ *
+ * @return string
+ */
+ public function getOverridePriority()
+ {
+ return $this->overridePriority;
+ }
+
+ /**
+ * Returns the 'option' parameter.
+ *
+ * @return string
+ */
+ public function getOption()
+ {
+ return $this->option;
+ }
+
+ public function activateOptions()
+ {
+ $this->intPriority = $this->parsePriority();
+ $this->intOption = $this->parseOption();
+ $this->intFacility = $this->parseFacility();
+
+ $this->closed = false;
+ }
+
+ public function close()
+ {
+ if ($this->closed != true) {
+ closelog();
+ $this->closed = true;
+ }
+ }
+
+ /**
+ * Appends the event to syslog.
+ *
+ * Log is opened and closed each time because if it is not closed, it
+ * can cause the Apache httpd server to log to whatever ident/facility
+ * was used in openlog().
+ *
+ * @see http://www.php.net/manual/en/function.syslog.php#97843
+ */
+ public function append(LoggingEvent $event)
+ {
+ $priority = $this->getSyslogPriority($event->getLevel());
+ $message = $this->layout->format($event);
+
+ openlog($this->ident, $this->intOption, $this->intFacility);
+ syslog($priority, $message);
+ closelog();
+ }
+
+ /** Determines which syslog priority to use based on the given level. */
+ private function getSyslogPriority(Level $level)
+ {
+ if ($this->overridePriority) {
+ return $this->intPriority;
+ }
+
+ return $level->getSyslogEquivalent();
+ }
+
+ /** Parses a syslog option string and returns the correspodning int value. */
+ private function parseOption()
+ {
+ $value = 0;
+ $options = explode('|', $this->option);
+
+ foreach ($options as $option) {
+ if (!empty($option)) {
+ $constant = "LOG_" . trim($option);
+ if (defined($constant)) {
+ $value |= constant($constant);
+ } else {
+ trigger_error("log4php: Invalid syslog option provided: $option. Whole option string: {$this->option}.", E_USER_WARNING);
+ }
+ }
+ }
+
+ return $value;
+ }
+
+ /** Parses the facility string and returns the corresponding int value. */
+ private function parseFacility()
+ {
+ if (!empty($this->facility)) {
+ $constant = "LOG_" . trim($this->facility);
+ if (defined($constant)) {
+ return constant($constant);
+ } else {
+ trigger_error("log4php: Invalid syslog facility provided: {$this->facility}.", E_USER_WARNING);
+ }
+ }
+ }
+
+ /** Parses the priority string and returns the corresponding int value. */
+ private function parsePriority()
+ {
+ if (!empty($this->priority)) {
+ $constant = "LOG_" . trim($this->priority);
+ if (defined($constant)) {
+ return constant($constant);
+ } else {
+ trigger_error("log4php: Invalid syslog priority provided: {$this->priority}.", E_USER_WARNING);
+ }
+ }
+ }
}
http://git-wip-us.apache.org/repos/asf/logging-log4php/blob/35dfd5d3/src/Autoloader.php
----------------------------------------------------------------------
diff --git a/src/Autoloader.php b/src/Autoloader.php
index fe6b8d3..d4fc666 100644
--- a/src/Autoloader.php
+++ b/src/Autoloader.php
@@ -23,31 +23,31 @@ namespace Apache\Log4php;
*/
class Autoloader
{
- const BASE_NAMESPACE = 'Apache\\Log4php\\';
+ const BASE_NAMESPACE = 'Apache\\Log4php\\';
- public function autoload($class)
- {
- // Base directory for the namespace prefix
- $baseDir = __DIR__ . '/../src/';
+ public function autoload($class)
+ {
+ // Base directory for the namespace prefix
+ $baseDir = __DIR__ . '/../src/';
- // Skip classes which are not in base namespace
- $len = strlen(self::BASE_NAMESPACE);
- if (strncmp(self::BASE_NAMESPACE, $class, $len) !== 0) {
- return;
- }
+ // Skip classes which are not in base namespace
+ $len = strlen(self::BASE_NAMESPACE);
+ if (strncmp(self::BASE_NAMESPACE, $class, $len) !== 0) {
+ return;
+ }
- // Locate the class in base dir, based on namespace
- $classPath = str_replace('\\', '/', substr($class, $len));
- $file = $baseDir . $classPath . '.php';
+ // Locate the class in base dir, based on namespace
+ $classPath = str_replace('\\', '/', substr($class, $len));
+ $file = $baseDir . $classPath . '.php';
- // If the file exists, require it
- if (file_exists($file)) {
- require $file;
- }
- }
+ // If the file exists, require it
+ if (file_exists($file)) {
+ require $file;
+ }
+ }
- public function register()
- {
- spl_autoload_register(array($this, 'autoload'));
- }
+ public function register()
+ {
+ spl_autoload_register(array($this, 'autoload'));
+ }
}
http://git-wip-us.apache.org/repos/asf/logging-log4php/blob/35dfd5d3/src/Configurable.php
----------------------------------------------------------------------
diff --git a/src/Configurable.php b/src/Configurable.php
index 352aa23..258e6ec 100644
--- a/src/Configurable.php
+++ b/src/Configurable.php
@@ -29,90 +29,98 @@ use Exception;
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @since 2.2
*/
-abstract class Configurable {
+abstract class Configurable
+{
+ /** Setter function for boolean type. */
+ protected function setBoolean($property, $value)
+ {
+ try {
+ $this->$property = OptionConverter::toBooleanEx($value);
+ } catch (Exception $ex) {
+ $value = var_export($value, true);
+ $this->warn("Invalid value given for '$property' property: [$value]. Expected a boolean value. Property not changed.");
+ }
+ }
- /** Setter function for boolean type. */
- protected function setBoolean($property, $value) {
- try {
- $this->$property = OptionConverter::toBooleanEx($value);
- } catch (Exception $ex) {
- $value = var_export($value, true);
- $this->warn("Invalid value given for '$property' property: [$value]. Expected a boolean value. Property not changed.");
- }
- }
+ /** Setter function for integer type. */
+ protected function setInteger($property, $value)
+ {
+ try {
+ $this->$property = OptionConverter::toIntegerEx($value);
+ } catch (Exception $ex) {
+ $value = var_export($value, true);
+ $this->warn("Invalid value given for '$property' property: [$value]. Expected an integer. Property not changed.");
+ }
+ }
- /** Setter function for integer type. */
- protected function setInteger($property, $value) {
- try {
- $this->$property = OptionConverter::toIntegerEx($value);
- } catch (Exception $ex) {
- $value = var_export($value, true);
- $this->warn("Invalid value given for '$property' property: [$value]. Expected an integer. Property not changed.");
- }
- }
+ /** Setter function for Level values. */
+ protected function setLevel($property, $value)
+ {
+ try {
+ $this->$property = OptionConverter::toLevelEx($value);
+ } catch (Exception $ex) {
+ $value = var_export($value, true);
+ $this->warn("Invalid value given for '$property' property: [$value]. Expected a level value. Property not changed.");
+ }
+ }
- /** Setter function for Level values. */
- protected function setLevel($property, $value) {
- try {
- $this->$property = OptionConverter::toLevelEx($value);
- } catch (Exception $ex) {
- $value = var_export($value, true);
- $this->warn("Invalid value given for '$property' property: [$value]. Expected a level value. Property not changed.");
- }
- }
+ /** Setter function for integer type. */
+ protected function setPositiveInteger($property, $value)
+ {
+ try {
+ $this->$property = OptionConverter::toPositiveIntegerEx($value);
+ } catch (Exception $ex) {
+ $value = var_export($value, true);
+ $this->warn("Invalid value given for '$property' property: [$value]. Expected a positive integer. Property not changed.");
+ }
+ }
- /** Setter function for integer type. */
- protected function setPositiveInteger($property, $value) {
- try {
- $this->$property = OptionConverter::toPositiveIntegerEx($value);
- } catch (Exception $ex) {
- $value = var_export($value, true);
- $this->warn("Invalid value given for '$property' property: [$value]. Expected a positive integer. Property not changed.");
- }
- }
+ /** Setter for file size. */
+ protected function setFileSize($property, $value)
+ {
+ try {
+ $this->$property = OptionConverter::toFileSizeEx($value);
+ } catch (Exception $ex) {
+ $value = var_export($value, true);
+ $this->warn("Invalid value given for '$property' property: [$value]. Expected a file size value. Property not changed.");
+ }
+ }
- /** Setter for file size. */
- protected function setFileSize($property, $value) {
- try {
- $this->$property = OptionConverter::toFileSizeEx($value);
- } catch (Exception $ex) {
- $value = var_export($value, true);
- $this->warn("Invalid value given for '$property' property: [$value]. Expected a file size value. Property not changed.");
- }
- }
+ /** Setter function for numeric type. */
+ protected function setNumeric($property, $value)
+ {
+ try {
+ $this->$property = OptionConverter::toNumericEx($value);
+ } catch (Exception $ex) {
+ $value = var_export($value, true);
+ $this->warn("Invalid value given for '$property' property: [$value]. Expected a number. Property not changed.");
+ }
+ }
- /** Setter function for numeric type. */
- protected function setNumeric($property, $value) {
- try {
- $this->$property = OptionConverter::toNumericEx($value);
- } catch (Exception $ex) {
- $value = var_export($value, true);
- $this->warn("Invalid value given for '$property' property: [$value]. Expected a number. Property not changed.");
- }
- }
+ /** Setter function for string type. */
+ protected function setString($property, $value, $nullable = false)
+ {
+ if ($value === null) {
+ if ($nullable) {
+ $this->$property= null;
+ } else {
+ $this->warn("Null value given for '$property' property. Expected a string. Property not changed.");
+ }
+ } else {
+ try {
+ $value = OptionConverter::toStringEx($value);
+ $this->$property = OptionConverter::substConstants($value);
+ } catch (Exception $ex) {
+ $value = var_export($value, true);
+ $this->warn("Invalid value given for '$property' property: [$value]. Expected a string. Property not changed.");
+ }
+ }
+ }
- /** Setter function for string type. */
- protected function setString($property, $value, $nullable = false) {
- if ($value === null) {
- if($nullable) {
- $this->$property= null;
- } else {
- $this->warn("Null value given for '$property' property. Expected a string. Property not changed.");
- }
- } else {
- try {
- $value = OptionConverter::toStringEx($value);
- $this->$property = OptionConverter::substConstants($value);
- } catch (Exception $ex) {
- $value = var_export($value, true);
- $this->warn("Invalid value given for '$property' property: [$value]. Expected a string. Property not changed.");
- }
- }
- }
-
- /** Triggers a warning. */
- protected function warn($message) {
- $class = get_class($this);
- trigger_error("log4php: $class: $message", E_USER_WARNING);
- }
+ /** Triggers a warning. */
+ protected function warn($message)
+ {
+ $class = get_class($this);
+ trigger_error("log4php: $class: $message", E_USER_WARNING);
+ }
}
http://git-wip-us.apache.org/repos/asf/logging-log4php/blob/35dfd5d3/src/Configuration/ConfiguratorInterface.php
----------------------------------------------------------------------
diff --git a/src/Configuration/ConfiguratorInterface.php b/src/Configuration/ConfiguratorInterface.php
index a4a8e21..f58fc41 100644
--- a/src/Configuration/ConfiguratorInterface.php
+++ b/src/Configuration/ConfiguratorInterface.php
@@ -27,15 +27,15 @@ use Apache\Log4php\Hierarchy;
*/
interface ConfiguratorInterface
{
- /**
- * Configures log4php based on the given configuration.
- *
- * All configurators implementations must implement this interface.
- *
- * @param Hierarchy $hierarchy The hierarchy on which to perform
- * the configuration.
- * @param mixed $input Either path to the config file or the
- * configuration as an array.
- */
- public function configure(Hierarchy $hierarchy, $input = null);
-}
\ No newline at end of file
+ /**
+ * Configures log4php based on the given configuration.
+ *
+ * All configurators implementations must implement this interface.
+ *
+ * @param Hierarchy $hierarchy The hierarchy on which to perform
+ * the configuration.
+ * @param mixed $input Either path to the config file or the
+ * configuration as an array.
+ */
+ public function configure(Hierarchy $hierarchy, $input = null);
+}