logbook/class/core/log/clients/file.class.php
linarphy a8aa60ae83
Fix following first run
The code 'works' as is
2025-04-15 21:03:44 +02:00

64 lines
1.4 KiB
PHP

<?php
namespace core\log\clients;
/**
* Store stream in files
*/
class File implements \core\log\Stream
{
/**
* File where event are stored
*
* @var \resource
*/
protected resource $file;
/**
* @throws \core\config\ConfigException
* @throws \core\log\LogException
*/
public function __construct(\core\log\Tag $tag) : void
{
$hardcoded_configuration = \core\config\class\core\log\client\FileSettings(array(
'path' => '/var/log/logbook/',
));
$path = $hardcoded_configuration->get('path');
if (\core\config\GlobalConfiguration::exists('class', 'core', 'log', 'client', 'settings'))
{
$configuration = \core\config\GlobalConfiguration::read('class', 'core', 'log', 'client', 'settings');
if ($configuration->exists('path'))
{
$path = $configuration->get('path');
}
}
$path = new \core\Path($path); # WARNING: must be absolute path
$path->safe_append($tag->generate_id());
$this->file = \fopen($path->__tostring(), 'a');
if (!$this->file)
{
throw new \core\log\LogException(_('cannot open log file'), 1);
}
}
public funtion push(\core\log\Event $event) : \core\log\Event
{
if (!\flock($this->file, \LOCK_EX))
{
throw new \core\log\LogException(_(
'cannot guarantee that the file is not already being edited'
));
}
\fwrite($this->file, $event->display());
\flock($this->file, \LOCK_UN);
return $event;
}
}
?>