From c2b310fd73bf0ce151e1998ebf4eb6dee649725e Mon Sep 17 00:00:00 2001 From: Franck Allimant Date: Sat, 25 Jan 2014 00:30:09 +0100 Subject: [PATCH] Created TlogDestinationRotatingFile and set it the default destination --- .../Thelia/Log/AbstractTlogDestination.php | 14 ++- .../Log/Destination/TlogDestinationFile.php | 39 ++----- .../TlogDestinationRotatingFile.php | 101 ++++++++++++++++++ core/lib/Thelia/Log/Tlog.php | 2 +- 4 files changed, 118 insertions(+), 38 deletions(-) create mode 100644 core/lib/Thelia/Log/Destination/TlogDestinationRotatingFile.php diff --git a/core/lib/Thelia/Log/AbstractTlogDestination.php b/core/lib/Thelia/Log/AbstractTlogDestination.php index 5dcb76f71..718679d76 100644 --- a/core/lib/Thelia/Log/AbstractTlogDestination.php +++ b/core/lib/Thelia/Log/AbstractTlogDestination.php @@ -26,31 +26,29 @@ namespace Thelia\Log; abstract class AbstractTlogDestination { //Tableau de TlogDestinationConfig paramétrant la destination - protected $_configs; + protected $_configs = array(); //Tableau des lignes de logs stockés avant utilisation par ecrire() - protected $_logs; + protected $_logs = array(); public function __construct() { - $this->_configs = array(); - $this->_logs = array(); - // Initialiser les variables de configuration - $this->_configs = $this->getConfigs(); + $this->_configs = $this->getConfigs(); // Appliquer la configuration $this->configure(); } //Affecte une valeur à une configuration de la destination - public function setConfig($name, $value) + public function setConfig($name, $value, $apply_changes = true) { foreach ($this->_configs as $config) { if ($config->getName() == $name) { $config->setValue($value); + // Appliquer les changements - $this->configure(); + if ($apply_changes) $this->configure(); return true; } diff --git a/core/lib/Thelia/Log/Destination/TlogDestinationFile.php b/core/lib/Thelia/Log/Destination/TlogDestinationFile.php index 08b7aab66..2f21583a3 100644 --- a/core/lib/Thelia/Log/Destination/TlogDestinationFile.php +++ b/core/lib/Thelia/Log/Destination/TlogDestinationFile.php @@ -37,9 +37,6 @@ class TlogDestinationFile extends AbstractTlogDestination const VAR_MODE = "tlog_destinationfile_mode"; const VALEUR_MODE_DEFAULT = "A"; - const VAR_MAX_FILE_SIZE_KB = "tlog_destinationfile_max_file_size"; - const MAX_FILE_SIZE_KB_DEFAULT = 1024; // 1 Mb - protected $path_defaut = false; protected $fh = false; @@ -49,10 +46,18 @@ class TlogDestinationFile extends AbstractTlogDestination parent::__construct(); } + protected function getFilePath() { + return $this->getConfig(self::VAR_PATH_FILE); + } + + protected function getOpenMode() { + return strtolower($this->getConfig(self::VAR_MODE, self::VALEUR_MODE_DEFAULT)) == 'a' ? 'a' : 'w'; + } + public function configure() { - $file_path = $this->getConfig(self::VAR_PATH_FILE); - $mode = strtolower($this->getConfig(self::VAR_MODE, self::VALEUR_MODE_DEFAULT)) == 'a' ? 'a' : 'w'; + $file_path = $this->getFilePath(); + $mode = $this->getOpenMode(); if (! empty($file_path)) { if (! is_file($file_path)) { @@ -67,23 +72,6 @@ class TlogDestinationFile extends AbstractTlogDestination if ($this->fh) @fclose($this->fh); - if (filesize($file_path) > 1024 * $this->getConfig(self::VAR_MAX_FILE_SIZE_KB, self::MAX_FILE_SIZE_KB_DEFAULT)) { - - $idx = 1; - - do { - $file_path_bk = "$file_path.$idx"; - - $idx++; - - } while (file_exists($file_path_bk)); - - @rename($file_path, $file_path_bk); - - @touch($file_path); - @chmod($file_path, 0666); - } - $this->fh = fopen($file_path, $mode); } } @@ -114,13 +102,6 @@ class TlogDestinationFile extends AbstractTlogDestination 'Enter E to empty this file for each request, or A to always append logs. Consider resetting the file from time to time', self::VALEUR_MODE_DEFAULT, TlogDestinationConfig::TYPE_TEXTFIELD - ), - new TlogDestinationConfig( - self::VAR_MAX_FILE_SIZE_KB, - 'Maximum log file size, in Kb', - 'When this size if exeeded, a backup copy of the file is made, and a new log file is opened. As the file size check is performed only at the beginning of a request, the file size may be bigger thant this limit. Note: 1 Mb = 1024 Kb', - self::MAX_FILE_SIZE_KB_DEFAULT, - TlogDestinationConfig::TYPE_TEXTFIELD ) ); } diff --git a/core/lib/Thelia/Log/Destination/TlogDestinationRotatingFile.php b/core/lib/Thelia/Log/Destination/TlogDestinationRotatingFile.php new file mode 100644 index 000000000..dfd225347 --- /dev/null +++ b/core/lib/Thelia/Log/Destination/TlogDestinationRotatingFile.php @@ -0,0 +1,101 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Log\Destination; + +use Thelia\Log\AbstractTlogDestination; +use Thelia\Log\TlogDestinationConfig; +use Thelia\Core\Translation\Translator; + +class TlogDestinationRotatingFile extends TlogDestinationFile +{ + // Nom des variables de configuration + // ---------------------------------- + + const VAR_MAX_FILE_SIZE_KB = "tlog_destinationfile_max_file_size"; + const MAX_FILE_SIZE_KB_DEFAULT = 1024; // 1 Mb + + public function __construct($maxFileSize = self::MAX_FILE_SIZE_KB_DEFAULT) + { + $this->path_defaut = THELIA_ROOT . "log" . DS . self::TLOG_DEFAULT_NAME; + + $this->setConfig(self::VAR_MAX_FILE_SIZE_KB, $maxFileSize, false); + + parent::__construct(); + } + + public function configure() + { + parent::configure(); + + $file_path = $this->getFilePath(); + $mode = $this->getOpenMode(); + + if ($this->fh) @fclose($this->fh); + + if (filesize($file_path) > 1024 * $this->getConfig(self::VAR_MAX_FILE_SIZE_KB, self::MAX_FILE_SIZE_KB_DEFAULT)) { + + $idx = 1; + + do { + $file_path_bk = "$file_path.$idx"; + + $idx++; + + } while (file_exists($file_path_bk)); + + @rename($file_path, $file_path_bk); + + @touch($file_path); + @chmod($file_path, 0666); + } + + $this->fh = fopen($file_path, $mode); + } + + public function getTitle() + { + return Translator::getInstance()->trans('Rotated Text File'); + } + + public function getDescription() + { + return Translator::getInstance()->trans('Store logs into text file, up to a certian size, then a new file is created'); + } + + public function getConfigs() + { + $arr = parent::getConfigs(); + + $arr[] = + new TlogDestinationConfig( + self::VAR_MAX_FILE_SIZE_KB, + 'Maximum log file size, in Kb', + 'When this size if exeeded, a backup copy of the file is made, and a new log file is opened. As the file size check is performed only at the beginning of a request, the file size may be bigger thant this limit. Note: 1 Mb = 1024 Kb', + self::MAX_FILE_SIZE_KB_DEFAULT, + TlogDestinationConfig::TYPE_TEXTFIELD + ); + + return $arr; + } +} \ No newline at end of file diff --git a/core/lib/Thelia/Log/Tlog.php b/core/lib/Thelia/Log/Tlog.php index 426ea5c7c..5e3d0537a 100644 --- a/core/lib/Thelia/Log/Tlog.php +++ b/core/lib/Thelia/Log/Tlog.php @@ -69,7 +69,7 @@ class Tlog Implements LoggerInterface // default values const DEFAULT_LEVEL = self::DEBUG; - const DEFAUT_DESTINATIONS = "Thelia\Log\Destination\TlogDestinationFile"; + const DEFAUT_DESTINATIONS = "Thelia\Log\Destination\TlogDestinationRotatingFile"; const DEFAUT_PREFIXE = "#INDEX: #LEVEL [#FILE:#FUNCTION()] {#LINE} #DATE #HOUR: "; const DEFAUT_FILES = "*"; const DEFAUT_IP = "";