N°2518 Possibiliy to rotate log files based on days

See the 'log_filename_builder_impl' config parameter
This commit is contained in:
Pierre Goiffon
2019-10-03 17:54:43 +02:00
parent 2c8887398d
commit 21ff03e28f
3 changed files with 139 additions and 27 deletions

View File

@@ -385,6 +385,14 @@ class Config
'source_of_value' => '',
'show_in_conf_sample' => false,
),
'log_filename_builder_impl' => array(
'type' => 'string',
'description' => 'Name of the ILogFileNameBuilder to use',
'default' => 'DailyRotatingLogFileNameBuilder',
'value' => '',
'source_of_value' => '',
'show_in_conf_sample' => false,
),
'log_rest_service' => array(
'type' => 'bool',
'description' => 'Log the usage of the REST/JSON service',

View File

@@ -16,54 +16,159 @@
// You should have received a copy of the GNU Affero General Public License
// along with iTop. If not, see <http://www.gnu.org/licenses/>
/**
* @since 2.7.0 N°2518
*/
interface ILogFileNameBuilder
{
public function __construct($sFileFullPath);
public function GetLogFilePath();
}
class DefaultLogFileNameBuilder implements ILogFileNameBuilder
{
private $sLogFileFullPath;
public function __construct($sFileFullPath)
{
$this->sLogFileFullPath = $sFileFullPath;
}
public function GetLogFilePath()
{
return $this->sLogFileFullPath;
}
}
/**
* Adds a suffix to the filename
*
* @since 2.7.0 N°2518
*/
abstract class RotatingLogFileNameBuilder implements ILogFileNameBuilder
{
protected $sFilePath;
protected $sFileBaseName;
protected $sFileExtension;
public function __construct($sFileFullPath)
{
$aPathParts = pathinfo($sFileFullPath);
$this->sFilePath = $aPathParts['dirname'];
$this->sFileBaseName = $aPathParts['filename'];
$this->sFileExtension = $aPathParts['extension'];
}
public function GetLogFilePath()
{
$sFileSuffix = $this->GetFileSuffix();
return $this->sFilePath
.'/'
.$this->sFileBaseName
.'.'.$sFileSuffix
.'.'.$this->sFileExtension;
}
abstract protected function GetFileSuffix();
}
/**
* @since 2.7.0 N°2518
*/
class DailyRotatingLogFileNameBuilder extends RotatingLogFileNameBuilder
{
protected function GetFileSuffix()
{
return date('Y-m-d');
}
}
/**
* @since 2.7.0 N°2518
*/
class LogFileNameBuilderFactory
{
/**
* Uses the 'log_filename_builder_impl' config parameter
*
* @param string $sFileFullPath
*
* @return \ILogFileNameBuilder
* @throws \ConfigException
* @throws \CoreException
*/
public static function GetInstance($sFileFullPath)
{
$oConfig = utils::GetConfig();
$sFileNameBuilderImpl = $oConfig->Get('log_filename_builder_impl');
if (empty($sFileNameBuilderImpl))
{
$sFileNameBuilderImpl = 'DefaultLogFileNameBuilder';
}
return new $sFileNameBuilderImpl($sFileFullPath);
}
}
/**
* File logging
*
* @copyright Copyright (C) 2010-2017 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
* @since 2.7.0 allow to rotate file (N°2518)
*/
class FileLog
{
protected $m_sFile = ''; // log is disabled if this is empty
protected $oFileNameBuilder;
/**
* FileLog constructor.
*
* @param string $sFileName
*
* @since 2.7.0 allow to rotate file (N°2518)
* @throws \ConfigException
* @throws \CoreException
*/
public function __construct($sFileName = '')
{
$this->m_sFile = $sFileName;
$this->oFileNameBuilder = LogFileNameBuilderFactory::GetInstance($sFileName);
}
public function Error($sText)
{
$this->Write("Error | ".$sText);
$this->Write('Error | '.$sText);
}
public function Warning($sText)
{
$this->Write("Warning | ".$sText);
$this->Write('Warning | '.$sText);
}
public function Info($sText)
{
$this->Write("Info | ".$sText);
$this->Write('Info | '.$sText);
}
public function Ok($sText)
{
$this->Write("Ok | ".$sText);
$this->Write('Ok | '.$sText);
}
protected function Write($sText)
{
if (strlen($this->m_sFile) == 0) return;
$sLogFilePath = $this->oFileNameBuilder->GetLogFilePath();
$hLogFile = @fopen($this->m_sFile, 'a');
if (empty($sLogFilePath))
{
return;
}
$hLogFile = @fopen($sLogFilePath, 'a');
if ($hLogFile !== false)
{
flock($hLogFile, LOCK_EX);

View File

@@ -24,17 +24,22 @@
* @license http://opensource.org/licenses/AGPL-3.0
*/
require_once(APPROOT."/application/nicewebpage.class.inc.php");
require_once(APPROOT."setup/modulediscovery.class.inc.php");
require_once(APPROOT."setup/runtimeenv.class.inc.php");
require_once(APPROOT.'/application/nicewebpage.class.inc.php');
require_once(APPROOT.'setup/modulediscovery.class.inc.php');
require_once(APPROOT.'setup/runtimeenv.class.inc.php');
require_once(APPROOT.'core/log.class.inc.php');
define('INSTALL_LOG_FILE', APPROOT.'/log/setup.log');
SetupLog::Enable(APPROOT.'/log/setup.log');
/**
* @uses SetupLog
*/
class SetupPage extends NiceWebPage
{
public function __construct($sTitle)
{
parent::__construct($sTitle);
parent::__construct($sTitle);
$this->add_linked_script("../js/jquery.blockUI.js");
$this->add_linked_script("../setup/setup.js");
$this->add_style(
@@ -276,32 +281,26 @@ CSS
public static function log_error($sText)
{
self::log("Error - ".$sText);
SetupLog::Error($sText);
}
public static function log_warning($sText)
{
self::log("Warning - ".$sText);
SetupLog::Warning($sText);
}
public static function log_info($sText)
{
self::log("Info - ".$sText);
SetupLog::Info($sText);
}
public static function log_ok($sText)
{
self::log("Ok - ".$sText);
SetupLog::Ok($sText);
}
public static function log($sText)
{
$hLogFile = @fopen(INSTALL_LOG_FILE, 'a');
if ($hLogFile !== false)
{
$sDate = date('Y-m-d H:i:s');
fwrite($hLogFile, "$sDate - $sText\n");
fclose($hLogFile);
}
SetupLog::Ok($sText);
}
} // End of class
}