93 lines
1.8 KiB
PHP
93 lines
1.8 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() : void
|
|
{
|
|
$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_exists(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_exists(?\core\log\Tag $tag = null, ?\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
|
|
}
|
|
if (\in_array($stream, $this->streams))
|
|
{
|
|
if ($this->streams[$tag] === $stream)
|
|
{
|
|
return True;
|
|
}
|
|
return False
|
|
}
|
|
return False
|
|
}
|
|
}
|
|
|
|
?>
|