This repository has been archived by the owner on Jun 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
clean logs and merge config if not present
- Loading branch information
Sven Speckmaier
committed
Jul 11, 2019
1 parent
962a547
commit b43fb9d
Showing
6 changed files
with
237 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php namespace Ipunkt\LaravelJaeger\LogCleaner; | ||
|
||
/** | ||
* Class LogCleaner | ||
* @package Ipunkt\LaravelJaeger\LogCleaner | ||
*/ | ||
class LogCleaner { | ||
|
||
/** | ||
* @var int | ||
*/ | ||
protected $maxLength = 300; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $cutoffIndicator = '...'; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $logs = []; | ||
|
||
public function clean(): LogCleaner { | ||
$this->trimToMaxLength(); | ||
|
||
return $this; | ||
} | ||
|
||
protected function trimToMaxLength() { | ||
$this->trimToMaxLengthRecursive($this->logs); | ||
} | ||
|
||
protected function trimToMaxLengthRecursive(&$logs) { | ||
foreach ($logs as &$value) { | ||
if( is_array($value) ) { | ||
$this->trimToMaxLengthRecursive($value); | ||
continue; | ||
} | ||
|
||
if( !is_string($value) ) { | ||
continue; | ||
} | ||
|
||
$isLonger = strlen($value) > $this->maxLength; | ||
if( !$isLonger ) | ||
continue; | ||
|
||
$value = substr($value, 0, $this->maxLength); | ||
$value .= $this->cutoffIndicator; | ||
} | ||
} | ||
|
||
public function setMaxLength( int $maxLength ): LogCleaner { | ||
$this->maxLength = $maxLength; | ||
return $this; | ||
} | ||
|
||
public function setLogs(array $logs): LogCleaner { | ||
$this->logs = $logs; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param string $cutoffIndicator | ||
* @return LogCleaner | ||
*/ | ||
public function setCutoffIndicator( string $cutoffIndicator ): LogCleaner { | ||
$this->cutoffIndicator = $cutoffIndicator; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getLogs(): array { | ||
return $this->logs; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php namespace Ipunkt\LaravelJaegerTests\LogCleaner; | ||
|
||
use Ipunkt\LaravelJaeger\LogCleaner\LogCleaner; | ||
use Ipunkt\LaravelJaegerTests\TestCase; | ||
|
||
/** | ||
* Class LogCleanerTest | ||
* @package Ipunkt\LaravelJaegerTests\LogCleaner | ||
*/ | ||
class LogCleanerTest extends TestCase { | ||
|
||
/** | ||
* @var LogCleaner | ||
*/ | ||
protected $logCleaner; | ||
|
||
public function setUp():void { | ||
$this->logCleaner = new LogCleaner(); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function reducesStringSize() { | ||
$this->logCleaner | ||
->setMaxLength(10) | ||
->setCutoffIndicator('...') | ||
->setLogs([ | ||
'message' => '1234567890acbdefgh' | ||
])->clean(); | ||
|
||
$logs = $this->logCleaner->getLogs(); | ||
$this->assertEquals('1234567890...', $logs['message']); | ||
} | ||
/** | ||
* @test | ||
*/ | ||
public function reducesStringSizeInSubarrays() { | ||
$this->logCleaner | ||
->setMaxLength(10) | ||
->setCutoffIndicator('...') | ||
->setLogs([ | ||
'subarray' => [ | ||
'toolong' => '1234567890abcdefg' | ||
] | ||
])->clean(); | ||
|
||
$logs = $this->logCleaner->getLogs(); | ||
$this->assertEquals('1234567890...', $logs['subarray']['toolong']); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function honorsCutoffIndicator() { | ||
$this->logCleaner | ||
->setMaxLength(1) | ||
->setCutoffIndicator('+++') | ||
->setLogs([ | ||
'message' => '123' | ||
])->clean(); | ||
|
||
$logs = $this->logCleaner->getLogs(); | ||
$this->assertEquals('1+++', $logs['message']); | ||
} | ||
|
||
|
||
/** | ||
* @test | ||
*/ | ||
public function doesNotErrorOnObjectEntries() { | ||
$this->logCleaner | ||
->setMaxLength(1) | ||
->setCutoffIndicator('+++') | ||
->setLogs([ | ||
'object' => new \stdClass(), | ||
'subarray' => [ | ||
'anotherobject' => new \stdClass() | ||
], | ||
'message' => '123' | ||
])->clean(); | ||
|
||
$logs = $this->logCleaner->getLogs(); | ||
$this->assertEquals('1+++', $logs['message']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters