1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-04 20:27:31 +02:00

use mb_* when needed

This commit is contained in:
vershinin_so
2019-07-08 18:04:33 +03:00
parent ecd871b624
commit f8de7cf628
19 changed files with 42 additions and 64 deletions

View File

@@ -11,8 +11,6 @@
namespace Monolog\Formatter;
use Monolog\Utils;
/**
* formats the record to be used in the FlowdockHandler
*
@@ -84,12 +82,24 @@ class FlowdockFormatter implements FormatterInterface
return $formatted;
}
public function getShortMessage(string $message): string
public function getShortMessage($message)
{
static $hasMbString;
if (null === $hasMbString) {
$hasMbString = function_exists('mb_strlen');
}
$maxLength = 45;
if (Utils::strlen($message) > $maxLength) {
$message = Utils::substr($message, 0, $maxLength - 4) . ' ...';
if ($hasMbString) {
if (mb_strlen($message, 'UTF-8') > $maxLength) {
$message = mb_substr($message, 0, $maxLength - 4, 'UTF-8') . ' ...';
}
} else {
if (strlen($message) > $maxLength) {
$message = substr($message, 0, $maxLength - 4) . ' ...';
}
}
return $message;

View File

@@ -94,7 +94,7 @@ class GelfMessageFormatter extends NormalizerFormatter
->setLevel($this->logLevels[$record['level']]);
// message length + system name length + 200 for padding / metadata
$len = 200 + Utils::strlen((string) $record['message']) + Utils::strlen($this->systemName);
$len = 200 + strlen((string) $record['message']) + strlen($this->systemName);
if ($len > $this->maxLength) {
$message->setShortMessage(Utils::substr($record['message'], 0, $this->maxLength));
@@ -114,7 +114,7 @@ class GelfMessageFormatter extends NormalizerFormatter
foreach ($record['extra'] as $key => $val) {
$val = is_scalar($val) || null === $val ? $val : $this->toJson($val);
$len = Utils::strlen($this->extraPrefix . $key . $val);
$len = strlen($this->extraPrefix . $key . $val);
if ($len > $this->maxLength) {
$message->setAdditional($this->extraPrefix . $key, Utils::substr($val, 0, $this->maxLength));
@@ -125,7 +125,7 @@ class GelfMessageFormatter extends NormalizerFormatter
foreach ($record['context'] as $key => $val) {
$val = is_scalar($val) || null === $val ? $val : $this->toJson($val);
$len = Utils::strlen($this->contextPrefix . $key . $val);
$len = strlen($this->contextPrefix . $key . $val);
if ($len > $this->maxLength) {
$message->setAdditional($this->contextPrefix . $key, Utils::substr($val, 0, $this->maxLength));

View File

@@ -12,7 +12,6 @@
namespace Monolog\Formatter;
use Monolog\Logger;
use Monolog\Utils;
/**
* Serializes a log message according to Wildfire's header requirements
@@ -91,7 +90,7 @@ class WildfireFormatter extends NormalizerFormatter
// The message itself is a serialization of the above JSON object + it's length
return sprintf(
'%d|%s|',
Utils::strlen($json),
strlen($json),
$json
);
}

View File

@@ -176,7 +176,7 @@ class BrowserConsoleHandler extends AbstractProcessingHandler
$args[] = '"font-weight: normal"';
$pos = $match[0][1];
$format = Utils::substr($format, 0, $pos) . '%c' . $match[1][0] . '%c' . Utils::substr($format, $pos + Utils::strlen($match[0][0]));
$format = Utils::substr($format, 0, $pos) . '%c' . $match[1][0] . '%c' . Utils::substr($format, $pos + strlen($match[0][0]));
}
array_unshift($args, static::quote($format));

View File

@@ -14,7 +14,6 @@ namespace Monolog\Handler;
use Monolog\Formatter\ChromePHPFormatter;
use Monolog\Formatter\FormatterInterface;
use Monolog\Logger;
use Monolog\Utils;
/**
* Handler sending logs to the ChromePHP extension (http://www.chromephp.com/)
@@ -147,7 +146,7 @@ class ChromePHPHandler extends AbstractProcessingHandler
$json = @json_encode(self::$json);
$data = base64_encode(utf8_encode($json));
if (Utils::strlen($data) > 240 * 1024) {
if (strlen($data) > 240 * 1024) {
self::$overflowed = true;
$record = [

View File

@@ -12,7 +12,6 @@
namespace Monolog\Handler;
use Monolog\Logger;
use Monolog\Utils;
/**
* Logs to Cube.
@@ -135,7 +134,7 @@ class CubeHandler extends AbstractProcessingHandler
$this->connectUdp();
}
socket_send($this->udpConnection, $data, Utils::strlen($data), 0);
socket_send($this->udpConnection, $data, strlen($data), 0);
}
private function writeHttp(string $data): void
@@ -147,7 +146,7 @@ class CubeHandler extends AbstractProcessingHandler
curl_setopt($this->httpConnection, CURLOPT_POSTFIELDS, '['.$data.']');
curl_setopt($this->httpConnection, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: ' . Utils::strlen('['.$data.']'),
'Content-Length: ' . strlen('['.$data.']'),
]);
Curl\Util::execute($this->httpConnection, 5, false);

View File

@@ -12,7 +12,6 @@
namespace Monolog\Handler;
use Monolog\Logger;
use Monolog\Utils;
/**
* Simple handler wrapper that deduplicates log records across multiple requests
@@ -67,7 +66,7 @@ class DeduplicationHandler extends BufferHandler
{
parent::__construct($handler, 0, Logger::DEBUG, $bubble, false);
$this->deduplicationStore = $deduplicationStore === null ? sys_get_temp_dir() . '/monolog-dedup-' . Utils::substr(md5(__FILE__), 0, 20) .'.log' : $deduplicationStore;
$this->deduplicationStore = $deduplicationStore === null ? sys_get_temp_dir() . '/monolog-dedup-' . substr(md5(__FILE__), 0, 20) .'.log' : $deduplicationStore;
$this->deduplicationLevel = Logger::toMonologLevel($deduplicationLevel);
$this->time = $time;
}
@@ -150,7 +149,7 @@ class DeduplicationHandler extends BufferHandler
while (!feof($handle)) {
$log = fgets($handle);
if ($log && Utils::substr($log, 0, 10) >= $timestampValidity) {
if ($log && substr($log, 0, 10) >= $timestampValidity) {
$validLogs[] = $log;
}
}

View File

@@ -14,7 +14,6 @@ namespace Monolog\Handler;
use Monolog\Formatter\FormatterInterface;
use Monolog\Formatter\LineFormatter;
use Monolog\Logger;
use Monolog\Utils;
/**
* Sends logs to Fleep.io using Webhook integrations
@@ -97,7 +96,7 @@ class FleepHookHandler extends SocketHandler
$header = "POST " . static::FLEEP_HOOK_URI . $this->token . " HTTP/1.1\r\n";
$header .= "Host: " . static::FLEEP_HOST . "\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . Utils::strlen($content) . "\r\n";
$header .= "Content-Length: " . strlen($content) . "\r\n";
$header .= "\r\n";
return $header;

View File

@@ -14,7 +14,6 @@ namespace Monolog\Handler;
use Monolog\Logger;
use Monolog\Formatter\FlowdockFormatter;
use Monolog\Formatter\FormatterInterface;
use Monolog\Utils;
/**
* Sends notifications through the Flowdock push API
@@ -108,7 +107,7 @@ class FlowdockHandler extends SocketHandler
$header = "POST /v1/messages/team_inbox/" . $this->apiToken . " HTTP/1.1\r\n";
$header .= "Host: api.flowdock.com\r\n";
$header .= "Content-Type: application/json\r\n";
$header .= "Content-Length: " . Utils::strlen($content) . "\r\n";
$header .= "Content-Length: " . strlen($content) . "\r\n";
$header .= "\r\n";
return $header;

View File

@@ -13,7 +13,6 @@ namespace Monolog\Handler;
use Monolog\Formatter\FormatterInterface;
use Monolog\Formatter\HtmlFormatter;
use Monolog\Utils;
/**
* Base class for all mail handlers
@@ -71,7 +70,7 @@ abstract class MailHandler extends AbstractProcessingHandler
protected function isHtmlBody(string $body): bool
{
return Utils::substr($body, 0, 1) === '<';
return substr($body, 0, 1) === '<';
}
/**

View File

@@ -116,7 +116,7 @@ class PushoverHandler extends SocketHandler
private function buildContent(array $record): string
{
// Pushover has a limit of 512 characters on title and message combined.
$maxMessageLength = 512 - Utils::strlen($this->title);
$maxMessageLength = 512 - strlen($this->title);
$message = ($this->useFormattedMessage) ? $record['formatted'] : $record['message'];
$message = Utils::substr($message, 0, $maxMessageLength);
@@ -159,7 +159,7 @@ class PushoverHandler extends SocketHandler
$header = "POST /1/messages.json HTTP/1.1\r\n";
$header .= "Host: api.pushover.net\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . Utils::strlen($content) . "\r\n";
$header .= "Content-Length: " . strlen($content) . "\r\n";
$header .= "\r\n";
return $header;

View File

@@ -14,7 +14,6 @@ namespace Monolog\Handler\Slack;
use Monolog\Logger;
use Monolog\Formatter\NormalizerFormatter;
use Monolog\Formatter\FormatterInterface;
use Monolog\Utils;
/**
* Slack record utility helping to log to Slack webhooks or API.
@@ -308,7 +307,7 @@ class SlackRecord
private function generateAttachmentField(string $title, $value): array
{
$value = is_array($value)
? sprintf('```%s```', Utils::substr($this->stringify($value), 0, 1990))
? sprintf('```%s```', substr($this->stringify($value), 0, 1990))
: $value;
return array(

View File

@@ -14,7 +14,6 @@ namespace Monolog\Handler;
use Monolog\Formatter\FormatterInterface;
use Monolog\Logger;
use Monolog\Handler\Slack\SlackRecord;
use Monolog\Utils;
/**
* Sends notifications through Slack API
@@ -130,7 +129,7 @@ class SlackHandler extends SocketHandler
$header = "POST /api/chat.postMessage HTTP/1.1\r\n";
$header .= "Host: slack.com\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . Utils::strlen($content) . "\r\n";
$header .= "Content-Length: " . strlen($content) . "\r\n";
$header .= "\r\n";
return $header;

View File

@@ -12,7 +12,6 @@
namespace Monolog\Handler;
use Monolog\Logger;
use Monolog\Utils;
/**
* Stores to any socket - uses fsockopen() or pfsockopen().
@@ -326,7 +325,7 @@ class SocketHandler extends AbstractProcessingHandler
private function writeToSocket(string $data): void
{
$length = Utils::strlen($data);
$length = strlen($data);
$sent = 0;
$this->lastSentBytes = $sent;
while ($this->isConnected() && $sent < $length) {

View File

@@ -52,7 +52,7 @@ class SqsHandler extends AbstractProcessingHandler
}
$messageBody = $record['formatted'];
if (Utils::strlen($messageBody) >= static::MAX_MESSAGE_SIZE) {
if (strlen($messageBody) >= static::MAX_MESSAGE_SIZE) {
$messageBody = Utils::substr($messageBody, 0, static::HEAD_MESSAGE_SIZE);
}

View File

@@ -12,7 +12,6 @@
namespace Monolog\Handler;
use Monolog\Logger;
use Monolog\Utils;
/**
* Stores to any stream resource
@@ -147,8 +146,8 @@ class StreamHandler extends AbstractProcessingHandler
return dirname($stream);
}
if ('file://' === Utils::substr($stream, 0, 7)) {
return dirname(Utils::substr($stream, 7));
if ('file://' === substr($stream, 0, 7)) {
return dirname(substr($stream, 7));
}
return null;

View File

@@ -49,12 +49,12 @@ class UdpSocket
if (!is_resource($this->socket)) {
throw new \RuntimeException('The UdpSocket to '.$this->ip.':'.$this->port.' has been closed and can not be written to anymore');
}
socket_sendto($this->socket, $chunk, Utils::strlen($chunk), $flags = 0, $this->ip, $this->port);
socket_sendto($this->socket, $chunk, strlen($chunk), $flags = 0, $this->ip, $this->port);
}
protected function assembleMessage(string $line, string $header): string
{
$chunkSize = static::DATAGRAM_MAX_LENGTH - Utils::strlen($header);
$chunkSize = static::DATAGRAM_MAX_LENGTH - strlen($header);
return $header . Utils::substr($line, 0, $chunkSize);
}

View File

@@ -12,7 +12,6 @@
namespace Monolog\Processor;
use Monolog\ResettableInterface;
use Monolog\Utils;
/**
* Adds a unique identifier into records
@@ -46,11 +45,11 @@ class UidProcessor implements ProcessorInterface, ResettableInterface
public function reset()
{
$this->uid = $this->generateUid(Utils::strlen($this->uid));
$this->uid = $this->generateUid(strlen($this->uid));
}
private function generateUid(int $length): string
{
return Utils::substr(bin2hex(random_bytes((int) ceil($length / 2))), 0, $length);
return substr(bin2hex(random_bytes((int) ceil($length / 2))), 0, $length);
}
}

View File

@@ -23,30 +23,10 @@ final class Utils
return 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? get_parent_class($class).'@anonymous' : $class;
}
private static function hasMbString(): bool
{
static $hasMbString;
if (null === $hasMbString) {
$hasMbString = extension_loaded('mbstring');
}
return $hasMbString;
}
public static function strlen(string $string, ?string $encoding = null): int
{
if (self::hasMbString()) {
return $encoding ? mb_strlen($string, $encoding) : mb_strlen($string);
}
return strlen($string);
}
public static function substr(string $string, int $start, ?int $length = null)
{
if (self::hasMbString()) {
return mb_substr($string, $start, $length);
if (extension_loaded('mbstring')) {
return mb_strcut($string, $start, $length);
}
return substr($string, $start, $length);