[GH-ISSUE #841] preg_split null value in SyslogUdpHandler.php #326

Closed
opened 2026-03-04 02:14:09 +03:00 by kerem · 1 comment
Owner

Originally created by @pjuanda on GitHub (Aug 30, 2016).
Original GitHub issue: https://github.com/Seldaek/monolog/issues/841

Hi,

I have an issue when handling log with SyslogUdpHandler.php. In its method splitIntoLines, it will split $message into 2 lines although $message is NULL.

    protected function write(array $record)
    {
        // var_dump($record['formatted']); result: NULL
        $lines = $this->splitMessageIntoLines($record['formatted']);
        // echo count($lines); result: 2

        $header = $this->makeCommonSyslogHeader($this->logLevels[$record['level']]);

        foreach ($lines as $line) {
            $this->socket->write($line, $header);
        }
    }

I have checked it out, this problem is caused by preg_split

    private function splitMessageIntoLines($message)
    {
        if (is_array($message)) {
            $message = implode("\n", $message);
        }

        return preg_split('/$\R?^/m', $message);
    }

To solve the problem, we can pass PREG_SPLIT_NO_EMPTY flag as its 4th parameter

        return preg_split('/$\R?^/m', $message, -1, PREG_SPLIT_NO_EMPTY);

Is this a bug ? or the $record['formatted'] value must not NULL in the first place ?

Note : I'm using custom Formatter that override some methods from NormalizeFormatter. Here is my custom formatter

<?php

namespace AppBundle\Monolog\Formatter;

use Exception;
use Monolog\Formatter\NormalizerFormatter;
use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class IncludeStackTraceFormatter extends NormalizerFormatter
{
    /**
     * {@inheritdoc}
     */
    public function format(array $record)
    {
        $exception = isset($record['context']['exception']) ? $record['context']['exception'] : null;

        if ($exception && !$exception instanceof NotFoundHttpException) {
            $additionalMessage = '';
            $errorMessages = "Message : \n". $record['message'];

            if (isset($record['extra']['url'])) {
                $additionalMessage .= "URL : ". $record['extra']['url']. "\n";
            }

            if (isset($record['extra']['ip'])) {
                $additionalMessage .= "IP Address : ". $record['extra']['ip']. "\n";
            }

            $normalizedException = $this->normalizeException($exception);
            $errorMessages .= $normalizedException;

            return $additionalMessage. $errorMessages;
        }

        return;
    }

    /**
     * {@inheritdoc}
     */
    protected function normalizeException(Exception $e)
    {
        $stackTraces = "\nStack Traces :\n";
        $flattenException = FlattenException::create($e);

        foreach ($flattenException->getTrace() as $trace) {
            $stackTraces .= sprintf(" at file %s line %d ", $trace['file'], $trace['line']). "\n";
        }

        return $stackTraces;
    }
}
Originally created by @pjuanda on GitHub (Aug 30, 2016). Original GitHub issue: https://github.com/Seldaek/monolog/issues/841 Hi, I have an issue when handling log with [SyslogUdpHandler.php](https://github.com/Seldaek/monolog/blob/master/src/Monolog/Handler/SyslogUdpHandler.php). In its method [splitIntoLines](https://github.com/Seldaek/monolog/blob/master/src/Monolog/Handler/SyslogUdpHandler.php#L56), it will split `$message` into 2 lines although `$message` is `NULL`. ``` php protected function write(array $record) { // var_dump($record['formatted']); result: NULL $lines = $this->splitMessageIntoLines($record['formatted']); // echo count($lines); result: 2 $header = $this->makeCommonSyslogHeader($this->logLevels[$record['level']]); foreach ($lines as $line) { $this->socket->write($line, $header); } } ``` I have checked it out, this problem is caused by [preg_split](https://github.com/Seldaek/monolog/blob/master/src/Monolog/Handler/SyslogUdpHandler.php#L62) ``` php private function splitMessageIntoLines($message) { if (is_array($message)) { $message = implode("\n", $message); } return preg_split('/$\R?^/m', $message); } ``` To solve the problem, we can pass `PREG_SPLIT_NO_EMPTY` flag as its 4<sup>th</sup> parameter ``` php return preg_split('/$\R?^/m', $message, -1, PREG_SPLIT_NO_EMPTY); ``` Is this a bug ? or the `$record['formatted']` value must not `NULL` in the first place ? Note : I'm using custom Formatter that override some methods from `NormalizeFormatter`. Here is my custom formatter ``` php <?php namespace AppBundle\Monolog\Formatter; use Exception; use Monolog\Formatter\NormalizerFormatter; use Symfony\Component\Debug\Exception\FlattenException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; class IncludeStackTraceFormatter extends NormalizerFormatter { /** * {@inheritdoc} */ public function format(array $record) { $exception = isset($record['context']['exception']) ? $record['context']['exception'] : null; if ($exception && !$exception instanceof NotFoundHttpException) { $additionalMessage = ''; $errorMessages = "Message : \n". $record['message']; if (isset($record['extra']['url'])) { $additionalMessage .= "URL : ". $record['extra']['url']. "\n"; } if (isset($record['extra']['ip'])) { $additionalMessage .= "IP Address : ". $record['extra']['ip']. "\n"; } $normalizedException = $this->normalizeException($exception); $errorMessages .= $normalizedException; return $additionalMessage. $errorMessages; } return; } /** * {@inheritdoc} */ protected function normalizeException(Exception $e) { $stackTraces = "\nStack Traces :\n"; $flattenException = FlattenException::create($e); foreach ($flattenException->getTrace() as $trace) { $stackTraces .= sprintf(" at file %s line %d ", $trace['file'], $trace['line']). "\n"; } return $stackTraces; } } ```
kerem closed this issue 2026-03-04 02:14:09 +03:00
Author
Owner

@Seldaek commented on GitHub (Sep 25, 2016):

Normally the formatted key should be non-null yeah, it's a bit strange that you use the formatter as a way to filter messages basically.. This is usually done with the FingersCrossedHandler + a custom activation strategy, but then again if it is null it shouldn't break so I'll apply your patch thanks.

<!-- gh-comment-id:249427850 --> @Seldaek commented on GitHub (Sep 25, 2016): Normally the formatted key should be non-null yeah, it's a bit strange that you use the formatter as a way to filter messages basically.. This is usually done with the FingersCrossedHandler + a custom activation strategy, but then again if it is null it shouldn't break so I'll apply your patch thanks.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/monolog#326
No description provided.