<commit_before><?php namespace Comodojo\Dispatcher\Components;

use \Monolog\Handler\HandlerInterface;
use \Monolog\Logger;
use \Monolog\Handler\StreamHandler;
use \Monolog\Handler\SyslogHandler;
use \Monolog\Handler\ErrorLogHandler;
use \Monolog\Handler\NullHandler;
use \Comodojo\Dispatcher\Components\Configuration;

/**
 * @package     Comodojo Dispatcher
 * @author      <NAME> <<EMAIL>>
 * @author      <NAME> <<EMAIL>>
 * @license     GPL-3.0+
 *
 * LICENSE:
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */


class LogManager {

    public $configuration;

    public $name;

    private static $levels = array(
        'INFO' => Logger::INFO,
        'NOTICE' => Logger::NOTICE,
        'WARNING' => Logger::WARNING,
        'ERROR' => Logger::ERROR,
        'CRITICAL' => Logger::CRITICAL,
        'ALERT' => Logger::ALERT,
        'EMERGENCY' => Logger::EMERGENCY,
        'DEBUG' => Logger::DEBUG
    );

    public function __construct(Configuration $configuration, $name = 'dispatcher') {

        $this->configuration = $configuration;
        $this->name = $name;

    }

    public function init() {

        $log = $this->configuration->get('log');

        $name = empty($log['name']) ? $this->name : $log['name'];

        $logger = new Logger($name);

        if (
            empty($log) ||
            ( isset($log['enabled']) && $log['enabled'] === false ) ||
            empty($log['providers'])
        ) {

            $logger->pushHandler( new NullHandler( self::getLevel() ) );

        } else {

            foreach ($log['providers'] as $provider => $parameters) {

                $handler = $this->getHandler($provider, $parameters);

                if ( $handler instanceof HandlerInterface ) $logger->pushHandler($handler);

            }

        }

        return $logger;

    }

    /**
     * Create the logger
     *
     * @param Configuration $configuration
     *
     * @return Logger
     */
    public static function create(Configuration $configuration) {

        $log = new LogManager($configuration);

        return $log->init();

    }

    protected function getHandler($provider, $parameters) {

        switch ( $parameters['type'] ) {

            case 'StreamHandler':
                $handler = $this->getStreamHandler($provider, $parameters);
                break;

            case 'SyslogHandler':
                $handler = $this->getSyslogHandler($provider, $parameters);
                break;<commit_msg>fix  param in static constructor<commit_after><?php namespace Comodojo\Dispatcher\Components;

use \Monolog\Handler\HandlerInterface;
use \Monolog\Logger;
use \Monolog\Handler\StreamHandler;
use \Monolog\Handler\SyslogHandler;
use \Monolog\Handler\ErrorLogHandler;
use \Monolog\Handler\NullHandler;
use \Comodojo\Dispatcher\Components\Configuration;

/**
 * @package     Comodojo Dispatcher
 * @author      <NAME> <<EMAIL>>
 * @author      <NAME> <<EMAIL>>
 * @license     GPL-3.0+
 *
 * LICENSE:
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */


class LogManager {

    public $configuration;

    public $name;

    private static $levels = array(
        'INFO' => Logger::INFO,
        'NOTICE' => Logger::NOTICE,
        'WARNING' => Logger::WARNING,
        'ERROR' => Logger::ERROR,
        'CRITICAL' => Logger::CRITICAL,
        'ALERT' => Logger::ALERT,
        'EMERGENCY' => Logger::EMERGENCY,
        'DEBUG' => Logger::DEBUG
    );

    public function __construct(Configuration $configuration, $name = 'dispatcher') {

        $this->configuration = $configuration;
        $this->name = $name;

    }

    public function init() {

        $log = $this->configuration->get('log');

        $name = empty($log['name']) ? $this->name : $log['name'];

        $logger = new Logger($name);

        if (
            empty($log) ||
            ( isset($log['enabled']) && $log['enabled'] === false ) ||
            empty($log['providers'])
        ) {

            $logger->pushHandler( new NullHandler( self::getLevel() ) );

        } else {

            foreach ($log['providers'] as $provider => $parameters) {

                $handler = $this->getHandler($provider, $parameters);

                if ( $handler instanceof HandlerInterface ) $logger->pushHandler($handler);

            }

        }

        return $logger;

    }

    /**
     * Create the logger
     *
     * @param Configuration $configuration
     *
     * @return Logger
     */
    public static function create(Configuration $configuration, $name = 'dispatcher') {

        $log = new LogManager($configuration, $name);

        return $log->init();

    }

    protected function getHandler($provider, $parameters) {

        switch ( $parameters['type'] ) {

            case 'StreamHandler':
                $handler = $this->getStreamHandler($provider, $parameters);
                break;

            case 'SyslogHandler':
                $handler = $this->getSyslogHandler($provider, $parameters);
                break;
{'fraction_non_alphanumeric': 0.12708333333333333, 'fraction_numerical': 0.0009615384615384616, 'mean_word_length': 2.5220090293453725, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5210585', 'n_tokens_mistral': 1718, 'n_tokens_neox': 1638, 'n_words': 488}