Created TlogDestinationRotatingFile and set it the default destination

This commit is contained in:
Franck Allimant
2014-01-25 00:30:09 +01:00
parent 3e3ecc8edf
commit c2b310fd73
4 changed files with 118 additions and 38 deletions

View File

@@ -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;
}

View File

@@ -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
)
);
}

View File

@@ -0,0 +1,101 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
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;
}
}

View File

@@ -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 = "";