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

73 lines
1.4 KiB
PHP

<?php
namespace core\log;
/**
* Manage server logs
*/
class Logger
{
/**
* Array of streams used by this logger
*
* @var \core\log\Stream[]
*/
protected array $streams;
/**
* Constructor of Logger
*
* @param \core\path\Path $path Path to the folder where logs manaded by this object are stored
*/
public function __construct()
{
$this->streams = array();
}
/**
* Log a message
*
* @param \string $message Message to log
* @param \array[\core\log\Tag] $tags Tags associated to the log
*
* @return \int Number of logs written
*/
public static function log(string $message, \core\log\TagsType $tags) : int
{
$counter = 0;
foreach ($tags as $tag)
{
if (!($tag instanceof \core\log\Tag))
{
throw new \core\log\LogException(\core\substitute(
\_('{tag} is not a correct tag'),
array('tag' => $tag::class),
));
}
$stream = self::build_stream($tag);
if (!\is_null($stream))
{
$stream->push(new \core\log\Event($message), $tags);
$counter += 1;
}
}
return $counter;
}
/**
* Build a stream
*
* @param \core\log\Tag $tag Tag of the stream to manage
*
* @throws \core\log\StreamException
*
* @return \core\log\Stream | null
*/
protected static function build_stream(\core\log\Tag $tag) : \core\log\Stream | null
{
return \core\log\StreamFactory::build(tag: $tag); # NOTE: can throw \core\log\StreamException too
}
}
?>