Skip to content

Commit

Permalink
Merge pull request #16445 from niden/T16430-logger-interpolation
Browse files Browse the repository at this point in the history
T16430 logger interpolation
  • Loading branch information
niden authored Sep 30, 2023
2 parents 378d490 + cd2044f commit 5b60ba8
Show file tree
Hide file tree
Showing 112 changed files with 735 additions and 808 deletions.
135 changes: 71 additions & 64 deletions CHANGELOG-5.0.md

Large diffs are not rendered by default.

42 changes: 32 additions & 10 deletions phalcon/Logger/Formatter/AbstractFormatter.zep
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,29 @@ abstract class AbstractFormatter extends AbstractStr implements FormatterInterfa
protected dateFormat = "c";

/**
* Return the default date format
*
* @var string
*/
protected interpolatorLeft = "%";

/**
* @var string
*/
protected interpolatorRight = "%";

/**
* @return string
*/
public function getDateFormat() -> string
{
return this->dateFormat;
return $this->dateFormat;
}

/**
* @param string $format
*/
public function setDateFormat(string $format) -> void
{
let this->dateFormat = $format;
}

/**
Expand All @@ -49,14 +65,20 @@ abstract class AbstractFormatter extends AbstractStr implements FormatterInterfa
}

/**
* Set the default date format
* @param Item $item
* @param string $message
*
* @param string $format
*
* @return void
* @return string
*/
public function setDateFormat(string format) -> void
{
let this->dateFormat = format;
protected function getInterpolatedMessage(
<Item> item,
string message
) -> string {
return $this->toInterpolate(
message,
item->getContext(),
this->interpolatorLeft,
this->interpolatorRight
);
}
}
19 changes: 11 additions & 8 deletions phalcon/Logger/Formatter/Json.zep
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@ class Json extends AbstractFormatter
* Json constructor.
*
* @param string $dateFormat
* @param string $interpolatorLeft
* @param string $interpolatorRight
*/
public function __construct(string dateFormat = "c")
{
let this->dateFormat = dateFormat;
public function __construct(
string dateFormat = "c",
string interpolatorLeft = "%",
string interpolatorRight = "%"
) {
let this->dateFormat = dateFormat;
let this->interpolatorLeft = interpolatorLeft;
let this->interpolatorRight = interpolatorRight;
}

/**
Expand All @@ -40,18 +47,14 @@ class Json extends AbstractFormatter
{
var message, options;

let message = $this->getInterpolatedMessage($item, $item->getMessage());
let options = JSON_HEX_TAG
+ JSON_HEX_APOS
+ JSON_HEX_AMP
+ JSON_HEX_QUOT
+ JSON_UNESCAPED_SLASHES
+ JSON_THROW_ON_ERROR;

let message = this->toInterpolate(
item->getMessage(),
item->getContext()
);

return json_encode(
[
"level" : item->getLevelName(),
Expand Down
20 changes: 13 additions & 7 deletions phalcon/Logger/Formatter/Line.zep
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,19 @@ class Line extends AbstractFormatter
*
* @param string $format
* @param string $dateFormat
* @param string $interpolatorLeft
* @param string $interpolatorRight
*/
public function __construct(
string format = "[%date%][%level%] %message%",
string dateFormat = "c"
string dateFormat = "c",
string interpolatorLeft = "%",
string interpolatorRight = "%"
) {
let this->format = format,
this->dateFormat = dateFormat;
let this->format = format;
let this->dateFormat = dateFormat;
let this->interpolatorLeft = interpolatorLeft;
let this->interpolatorRight = interpolatorRight;
}

/**
Expand All @@ -54,13 +60,13 @@ class Line extends AbstractFormatter
let message = strtr(
this->format,
[
"%date%" : this->getFormattedDate(item),
"%level%" : item->getLevelName(),
"%message%" : item->getMessage()
this->interpolatorLeft . "date" . this->interpolatorRight : this->getFormattedDate(item),
this->interpolatorLeft . "level" . this->interpolatorRight : item->getLevelName(),
this->interpolatorLeft . "message" . this->interpolatorRight : item->getMessage()
]
);

return this->toInterpolate(message, item->getContext());
return this->getInterpolatedMessage(item, message);
}

/**
Expand Down
41 changes: 25 additions & 16 deletions tests/_data/fixtures/Http/PhpStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class PhpStream
*/
protected $data = '';

/**
* Constructor
*/
public function __construct()
{
if (file_exists($this->getBufferFilename())) {
Expand All @@ -48,26 +51,21 @@ public function __construct()
$this->length = strlen($this->data);
}

protected function getBufferFilename(): string
{
return codecept_output_dir('tests/stream/php_input.txt');
}

public function stream_open($path, $mode, $options, &$opened_path)
public function stream_close()
{
return true;
}

public function stream_close()
public function stream_eof()
{
return ($this->index >= $this->length);
}

public function stream_stat()
public function stream_flush()
{
return [];
return true;
}

public function stream_flush()
public function stream_open($path, $mode, $options, &$opened_path)
{
return true;
}
Expand All @@ -89,11 +87,6 @@ public function stream_read($count)
return $data;
}

public function stream_eof()
{
return ($this->index >= $this->length);
}

public function stream_seek($offset, $whence)
{
if (null === $this->length) {
Expand Down Expand Up @@ -133,6 +126,17 @@ public function stream_seek($offset, $whence)
}
}

public function stream_stat()
{
return [];
}

public function stream_tell()
{
return $this->index;
}


public function stream_write($data)
{
return file_put_contents(
Expand All @@ -153,4 +157,9 @@ public function unlink()
$this->index = 0;
$this->length = 0;
}

protected function getBufferFilename(): string
{
return codecept_output_dir('tests/stream/php_input.txt');
}
}
Loading

0 comments on commit 5b60ba8

Please sign in to comment.