Skip to content

Commit

Permalink
[TASK] Write cleaner FileWriter JSON output
Browse files Browse the repository at this point in the history
Since PHP 5.4 it's possible to disable escaping for unicode characters
and slashes, which results in a much cleaner, leaner and most
importantly readable TYPO3 log output.

Releases: master, 9.5
Resolves: #88120
Related: #77274
Change-Id: I5b331e29e0f036d3d95b0f1045c269a28fec6abb
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/60431
Reviewed-by: Markus Klein <markus.klein@typo3.org>
Reviewed-by: Oliver Klee <typo3-coding@oliverklee.de>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
Reviewed-by: Tomas Norre Mikkelsen <tomasnorre@gmail.com>
Reviewed-by: Frank Naegler <frank.naegler@typo3.org>
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: Frank Naegler <frank.naegler@typo3.org>
  • Loading branch information
mkoitka authored and NeoBlack committed May 31, 2019
1 parent 92b0661 commit 06a2e04
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion typo3/sysext/core/Classes/Log/Writer/FileWriter.php
Expand Up @@ -154,7 +154,7 @@ public function writeLog(LogRecord $record)
if (isset($recordData['exception']) && $recordData['exception'] instanceof \Exception) {
$recordData['exception'] = (string)$recordData['exception'];
}
$data = '- ' . json_encode($recordData);
$data = '- ' . json_encode($recordData, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
}

$message = sprintf(
Expand Down
22 changes: 22 additions & 0 deletions typo3/sysext/core/Tests/Unit/Log/Writer/FileWriterTest.php
Expand Up @@ -152,6 +152,28 @@ public function logsToFile(LogRecord $record, $expectedResult): void
$this->assertEquals($expectedResult, $logFileContents);
}

/**
* @test
*/
public function logsToFileWithUnescapedCharacters(): void
{
$this->setUpVfsStream();

$recordWithData = GeneralUtility::makeInstance(
LogRecord::class,
$this->getUniqueId('test.core.log.fileWriter.recordWithData.'),
\TYPO3\CMS\Core\Log\LogLevel::INFO,
'test record with unicode and slash in data to encode',
['foo' => ['bar' => 'I paid 0.00€ for open source projects/code']]
);

$expectedResult = '{"foo":{"bar":"I paid 0.00€ for open source projects/code"}}';

$this->createWriter('encoded-data')->writeLog($recordWithData);
$logFileContents = trim(file_get_contents($this->getDefaultFileName('encoded-data')));
$this->assertContains($expectedResult, $logFileContents);
}

/**
* @test
* @param LogRecord $record Record Test Data
Expand Down

0 comments on commit 06a2e04

Please sign in to comment.