-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Customise monolog formatter to exclude line breaks
- Loading branch information
Showing
3 changed files
with
65 additions
and
0 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,23 @@ | ||
<?php | ||
|
||
namespace MityDigital\StatamicLogger\Support; | ||
|
||
use Illuminate\Log\Logger; | ||
|
||
class LoggerFormatter | ||
{ | ||
/** | ||
* Customize the given logger instance. | ||
*/ | ||
public function __invoke(Logger $logger): void | ||
{ | ||
foreach ($logger->getHandlers() as $handler) { | ||
// get the formatter, and disable inline breaks | ||
$formatter = $handler->getFormatter(); | ||
$formatter->allowInlineLineBreaks(false); | ||
|
||
// re-set the formatter | ||
$handler->setFormatter($formatter); | ||
} | ||
} | ||
} |
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,40 @@ | ||
<?php | ||
|
||
use Carbon\Carbon; | ||
use Illuminate\Support\Facades\Log; | ||
use MityDigital\StatamicLogger\Facades\StatamicLogger; | ||
use MityDigital\StatamicLogger\Support\StatamicLoggerReader; | ||
|
||
it('stores a log entry with line breaks in a single line', function () { | ||
|
||
// set today | ||
$today = Carbon::now()->format('Y-m-d'); | ||
|
||
// remove the log for today | ||
unlink(storage_path(StatamicLogger::getStoragePath().DIRECTORY_SEPARATOR.StatamicLogger::getStorageFilename().'-'.$today.'.log')); | ||
|
||
// log a simple string | ||
Log::channel('statamic-logger') | ||
->info( | ||
json_encode([ | ||
'lines' => 'no lines here', | ||
]) | ||
); | ||
|
||
// log a string with line breaks | ||
Log::channel('statamic-logger') | ||
->info( | ||
json_encode([ | ||
'lines' => "a string with\nline breaks", | ||
]) | ||
); | ||
|
||
// read the last line of the log | ||
$reader = app(StatamicLoggerReader::class); | ||
$data = $reader->paginate($today, 1, 10); | ||
|
||
expect($data) | ||
->toHaveCount(2) | ||
->and($data[0])->toContain('{"lines":"a string with\nline breaks"}') | ||
->and($data[1])->toContain('{"lines":"no lines here"}'); | ||
}); |