logbook/class/core/log/clients/redis.class.php
2025-04-05 23:00:51 +02:00

88 lines
1.8 KiB
PHP

<?php
namespace core\log\clients;
/**
* Redis stream (only https://github.com/phpredis/phpredis for now)
*/
class Redis implements \core\log\Stream
{
/**
* Client object for redis
*
* @var \Redis
*/
protected \Redis $client;
/**
* @throws \core\config\ConfigException
*/
public function __construct(\core\log\Tag $tag) : void
{
$hardcoded_configuration = \core\config\class\core\log\client\RedisConfiguration(array(
'hostname' => '127.0.0.1', # localhost
));
$hostname = $hardcoded_configuration->get('hostname');
$port = null;
$auth_password = null;
$auth_username = null;
if (\core\config\GlobalConfiguration::exists('class', 'core', 'log', 'client', 'settings'))
{
$configuration = \core\config\GlobalConfiguration::read('class', 'core', 'log', 'client', 'settings');
if ($configuration->exists('hostname'))
{
$hostname = $configuration->get('host');
}
if ($configuration->exists('port'))
{
$port = $configuration->get('port');
}
if ($configuration->exists('auth', 'password'))
{
$auth_password = $configuration->get('auth', 'password');
}
if ($configuration->exists('auth', 'username'))
{
$auth_username = $configuration->get('auth', 'username');
}
}
$this->tag = $tag;
$config = array(
'host' => $hostname,
);
if (!\is_null($port))
{
$config['port'] = $port;
}
if (!\is_null($auth_username))
{
$config['auth'] = array($auth_username);
}
if (!\is_null($auth_password))
{
if (\in_array('auth', \array_keys($config)))
{
$config['auth'][] = $auth_password;
}
else
{
$config['auth'] = array($auth_password);
}
}
$this->client = new \Redis($config);
}
public function push(\core\log\Event $event) : \core\log\Event
{
$this->client->rpush($tag->generate_id(), $event->display());
return $event;
}
}
?>