<commit_before><?php

namespace Productsup\Flexilog\Handler;

use Redis;

/**
 * Publish to a Redis channel
 */
class RedisHandler extends AbstractHandler
{
    private $redis = null;
    private $redisConfig = array();
    private $fingersCrossed = false;

    /**
     * {@inheritDoc}
     */
    public function __construct($minimalLevel, $verbose, $additionalParameters = array())
    {
        if (!class_exists('Redis')) {
            throw new \Exception('Class Redis is not found');
        }
        if (!isset($additionalParameters['redisConfig'])) {
            throw new \Exception('Redis configuration has not been provided.');
        }
        $redisConfig = $additionalParameters['redisConfig'];
        if (!isset($redisConfig['channel'])) {
            throw new \Exception('Redis Channel to Publish to has not been provided');
        }
        if (isset($additionalParameters['fingersCrossed'])) {
            $this->fingersCrossed = $additionalParameters['fingersCrossed'];
        }
        $this->redis = new Redis();
        $this->redisConfig = $redisConfig;
        parent::__construct($minimalLevel, $verbose);
    }

    /**
     * Publishes the log to Redis
     *
     * @param string $channelName the Redis channelname to publish to
     * @param string $lineValue   the log message to publish
     */
    public function publishLine($channelName, $lineValue)
    {
        if (!$this->redis->connect($this->redisConfig['host'], $this->redisConfig['port'])) {
            if ($this->fingersCrossed) {
                return;
            }

            throw new \Exception('Could not connect to the Redis server.');
        }

        if (isset($this->redisConfig['password'])) {
            $this->redis->auth($this->redisConfig['password']);
        }

        $this->redis->publish($channelName, $lineValue);
        $this->redis->close();
    }

    /**
     * {@inheritDoc}
     */
    public function write($level, $message, $splitFullMessage, array $context = array())
    {
        $line = sprintf('%s: %s'.PHP_EOL, strtoupper($level), $message);

        $line = array(
            'date'    => date('Y-m-d'),
            'time'    => date('H:i:s'),
            'type'    => $level,
            'message' => $message,
            'process' => getenv('PRODUCTSUP_PID'),
            'host'    => gethostname(),
        );

        $lineValue = json_encode($line).PHP_EOL;
        $this->publishLine($this->redisConfig['channel'], $lineValue);
    }
}<commit_msg>Fixes Github issue #5<commit_after><?php

namespace Productsup\Flexilog\Handler;

use Redis;

/**
 * Publish to a Redis channel
 */
class RedisHandler extends AbstractHandler
{
    private $redis = null;
    private $redisConfig = array();
    private $fingersCrossed = false;

    /**
     * {@inheritDoc}
     */
    public function __construct($minimalLevel, $verbose, $additionalParameters = array())
    {
        if (!class_exists('Redis')) {
            throw new \Exception('Class Redis is not found');
        }
        if (!isset($additionalParameters['redisConfig'])) {
            throw new \Exception('Redis configuration has not been provided.');
        }
        $redisConfig = $additionalParameters['redisConfig'];
        if (!isset($redisConfig['channel'])) {
            throw new \Exception('Redis Channel to Publish to has not been provided');
        }
        if (isset($additionalParameters['fingersCrossed'])) {
            $this->fingersCrossed = $additionalParameters['fingersCrossed'];
        }
        $this->redis = new Redis();
        $this->redisConfig = $redisConfig;
        parent::__construct($minimalLevel, $verbose);
    }

    /**
     * Publishes the log to Redis
     *
     * @param string $channelName the Redis channelname to publish to
     * @param string $lineValue   the log message to publish
     */
    public function publishLine($channelName, $lineValue)
    {
        if (!$this->redis->connect($this->redisConfig['host'], $this->redisConfig['port'])) {
            if ($this->fingersCrossed) {
                return;
            }

            throw new \Exception('Could not connect to the Redis server.');
        }

        if (isset($this->redisConfig['password'])) {
            $this->redis->auth($this->redisConfig['password']);
        }

        $this->redis->publish($channelName, $lineValue);
        $this->redis->close();
    }

    /**
     * {@inheritDoc}
     */
    public function write($level, $message, $splitFullMessage, array $context = array())
    {
        $line = sprintf('%s: %s'.PHP_EOL, strtoupper($level), $message);

        $line = array(
            'date'    => date('Y-m-d'),
            'time'    => date('H:i:s'),
            'type'    => $level,
            'message' => $message,
            'host'    => gethostname(),
        );

        $lineValue = json_encode($line).PHP_EOL;
        $this->publishLine($this->redisConfig['channel'], $lineValue);
    }
}
{'fraction_non_alphanumeric': 0.15049425055477103, 'fraction_numerical': 0.00020173492031470649, 'mean_word_length': 2.3432231962238705, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2824426', 'n_tokens_mistral': 1433, 'n_tokens_neox': 1403, 'n_words': 309}