logbook/class/core/log/Logger.class.php

85 lines
1.7 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();
}
/**
* Add a stream to manage
*
* @param \core\log\Tag $tag Tag of the stream to manage
*
* @throws \core\log\StreamException
*
* @return \core\log\Stream
*/
protected function add_stream(\core\log\Tag $tag) : \core\log\Stream
{
if (!$this->stream_exist(tag: $tag))
{
$this->streams[$tag] = \core\log\StreamFactory::build(tag: $tag); # NOTE: can throw \core\log\StreamException too
return $this->streams[$tag];
}
\throw new \core\log\StreamException(_('the stream already exist, cannot add it', 1));
}
/**
* Check if stream associated to the tag already exist
*
* @param ?\core\log\Tag $tag Tag associated to a stream
*
* @param ?\core\log\Stream $stream Stream to search for
*
* @throws \core\log\LogException
*
* @return bool
*/
protected function stream_exist(tag: ?\core\log\Tag $tag = null, stream: ?\core\log\Stream $stream = null) : bool
{
if (\is_null($stream))
{
if (\is_null($tag))
{
\throw new \core\log\LogException(_("tag and stream arguments cannot be null at the same time"), 1);
}
if (\in_array($tag, \array_keys($this->streams)))
{
return True;
}
return False;
}
if (\is_null($tag))
{
if (\in_array($stream, $this->streams))
{
return True
}
return False
}
# this will never be reached
}
}
?>