<commit_before><?php

/*
 * This file is part of the 'org.octris.core' package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace org\octris\core\logger\writer {
    /**
     * Logger for graylog backend. Inspired by official GELF library.
     *
     * @octdoc      c:writer/graylog
     * @copyright   copyright (c) 2011-2014 by <NAME>
     * @author      <NAME> <<EMAIL>>
     */
    class graylog implements \org\octris\core\logger\writer_if
    /**/
    {
        /**
         * Mapping of logger levels to graylog levels.
         *
         * @octdoc  p:graylog/$graylog_levels
         * @type    array
         */
        private static $graylog_levels = array(
            \org\octris\core\logger::T_EMERGENCY => 0,
            \org\octris\core\logger::T_ALERT     => 1,
            \org\octris\core\logger::T_CRITICAL  => 2,
            \org\octris\core\logger::T_ERROR     => 3,
            \org\octris\core\logger::T_WARNING   => 4,
            \org\octris\core\logger::T_NOTICE    => 5,
            \org\octris\core\logger::T_INFO      => 6,
            \org\octris\core\logger::T_DEBUG     => 7
        );
        /**/

        /**
         * Graylog format version.
         *
         * @octdoc  p:graylog/$version
         */
        private static $version = '1.0';
        /**/

        /**
         * Constants to more easy configure chunk sizes.
         *
         * @octdoc  d:graylog/T_WAN, T_LAN
         */
        const T_WAN = 1420;
        const T_LAN = 8154;
        /**/

        /**
         * IP address of graylog server.
         *
         * @octdoc  p:graylog/$host
         * @type    string
         */
        protected $host;
        /**/

        /**
         * Port number of graylog server.
         *
         * @octdoc  p:graylog/$port
         * @type    int
         */
        protected $port;
        /**/

        /**
         * Maximum chunk size of packets to send to graylog server.
         *
         * @octdoc  p:graylog/$chunk_size
         * @type    int
         */
        protected $chunk_size;
        /**/

        /**
         * Constructor.
         *
         * @octdoc  m:graylog/__construct
         * @param   string      $hostname       Hostname of graylog server.
         * @param   int         $port           Optional port number the graylog server is expected to listen on.
         * @param   int         $chunk_size     Optional maximum chunk size.
         */
        public function __construct($hostname, $port = 12201, $chunk_size = self::T_WAN)
        /**/
        {
            $this->host       = gethostbyname($hostname);
            $this->port       = $port;
            $this->chunk_size = $chunk_size;
        }

        /**
         * Create GELF compatible message from logger message.
         *
         * @octdoc  m:graylog/prepareMessage
         * @param   array       $message        Message to convert.
         */
        protected function prepareMessage(array $message)
        /**/
        {
            $gelf = array(
                'version'       => self::$version,
                'host'          => $message['host'],
                'short_message' => $message['message'],
                'full_message'  => (is_null($message['exception'])
                                    ? ''
                                    : $message['exception']->getTraceAsString()),
                'timestamp'     => $message['timestamp'],
                'level'         => self::$graylog_levels[$message['level']],
                'facility'      => $message['facility'],
                'file'          => $message['file'],
                'line'          => $message['line'],
                '_code'         => $message['code']
            );

            array_walk($message['data'], function($v, $k) use (&$gelf) {
                if ($k != 'id' && $k != '_id') {
                    $gelf[(substr($k, 0, 1) != '_' ? '_' : '') . $k] = $v;
                }
            });
<commit_msg>bugfix: typesafe comparison because data might be an index-based array<commit_after><?php

/*
 * This file is part of the 'org.octris.core' package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace org\octris\core\logger\writer {
    /**
     * Logger for graylog backend. Inspired by official GELF library.
     *
     * @octdoc      c:writer/graylog
     * @copyright   copyright (c) 2011-2014 by <NAME>
     * @author      <NAME> <<EMAIL>>
     */
    class graylog implements \org\octris\core\logger\writer_if
    /**/
    {
        /**
         * Mapping of logger levels to graylog levels.
         *
         * @octdoc  p:graylog/$graylog_levels
         * @type    array
         */
        private static $graylog_levels = array(
            \org\octris\core\logger::T_EMERGENCY => 0,
            \org\octris\core\logger::T_ALERT     => 1,
            \org\octris\core\logger::T_CRITICAL  => 2,
            \org\octris\core\logger::T_ERROR     => 3,
            \org\octris\core\logger::T_WARNING   => 4,
            \org\octris\core\logger::T_NOTICE    => 5,
            \org\octris\core\logger::T_INFO      => 6,
            \org\octris\core\logger::T_DEBUG     => 7
        );
        /**/

        /**
         * Graylog format version.
         *
         * @octdoc  p:graylog/$version
         */
        private static $version = '1.0';
        /**/

        /**
         * Constants to more easy configure chunk sizes.
         *
         * @octdoc  d:graylog/T_WAN, T_LAN
         */
        const T_WAN = 1420;
        const T_LAN = 8154;
        /**/

        /**
         * IP address of graylog server.
         *
         * @octdoc  p:graylog/$host
         * @type    string
         */
        protected $host;
        /**/

        /**
         * Port number of graylog server.
         *
         * @octdoc  p:graylog/$port
         * @type    int
         */
        protected $port;
        /**/

        /**
         * Maximum chunk size of packets to send to graylog server.
         *
         * @octdoc  p:graylog/$chunk_size
         * @type    int
         */
        protected $chunk_size;
        /**/

        /**
         * Constructor.
         *
         * @octdoc  m:graylog/__construct
         * @param   string      $hostname       Hostname of graylog server.
         * @param   int         $port           Optional port number the graylog server is expected to listen on.
         * @param   int         $chunk_size     Optional maximum chunk size.
         */
        public function __construct($hostname, $port = 12201, $chunk_size = self::T_WAN)
        /**/
        {
            $this->host       = gethostbyname($hostname);
            $this->port       = $port;
            $this->chunk_size = $chunk_size;
        }

        /**
         * Create GELF compatible message from logger message.
         *
         * @octdoc  m:graylog/prepareMessage
         * @param   array       $message        Message to convert.
         */
        protected function prepareMessage(array $message)
        /**/
        {
            $gelf = array(
                'version'       => self::$version,
                'host'          => $message['host'],
                'short_message' => $message['message'],
                'full_message'  => (is_null($message['exception'])
                                    ? ''
                                    : $message['exception']->getTraceAsString()),
                'timestamp'     => $message['timestamp'],
                'level'         => self::$graylog_levels[$message['level']],
                'facility'      => $message['facility'],
                'file'          => $message['file'],
                'line'          => $message['line'],
                '_code'         => $message['code']
            );

            array_walk($message['data'], function($v, $k) use (&$gelf) {
                if ($k !== 'id' && $k !== '_id') {
                    $gelf[(substr($k, 0, 1) != '_' ? '_' : '') . $k] = $v;
                }
            });

{'fraction_non_alphanumeric': 0.13230136654976418, 'fraction_numerical': 0.007981618091667675, 'mean_word_length': 1.2940360610263524, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '913311', 'n_tokens_mistral': 2426, 'n_tokens_neox': 2344, 'n_words': 513}