Skip to content

Commit

Permalink
Monolog 3
Browse files Browse the repository at this point in the history
Monolog 3 support for Laravel 10 (requires Monolog 3).
  • Loading branch information
dp88 authored Oct 19, 2023
2 parents 8ec8962 + 619a7ea commit 7d2a35c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
}
],
"require": {
"monolog/monolog": "^2.0",
"illuminate/log": "^6.0|^7.0|^8.0|^9.0|^10.0"
"monolog/monolog": "^3.0",
"illuminate/log": "^10.0"
},
"require-dev": {
"phpstan/phpstan": "^0.11.2",
Expand Down
19 changes: 13 additions & 6 deletions src/MonologFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@
use DateTime;
use Monolog\Formatter\FormatterInterface;
use Monolog\Formatter\LineFormatter;
use Monolog\LogRecord;

class MonologFormatter extends LineFormatter implements FormatterInterface
{
/** @var string */
protected $dateFormat = 'c';
protected string $dateFormat = 'c';

/** @var string */
protected $transactionID;

/** @var bool */
protected $allowInlineLineBreaks = true;
protected bool $allowInlineLineBreaks = true;

/** @var bool */
protected $includeStacktraces = false;
protected bool $includeStacktraces = false;

public function __construct(
?string $format = null,
Expand All @@ -43,7 +44,7 @@ public function __construct(
/**
* Formats a log record.
*/
public function format(array $record): string
public function format(LogRecord $record): string
{
$record = $this->stsContext($record);
$record = $this->normalize($record);
Expand All @@ -63,7 +64,7 @@ public function format(array $record): string
}

/** Ensure we have out context info added the way we like it. */
protected function stsContext(array $record): array
protected function stsContext(LogRecord $record): LogRecord
{
$record['context']['transactionID'] = $this->transactionID;
$record['context']['environment'] = env('APP_ENV', 'unknown');
Expand Down Expand Up @@ -102,8 +103,14 @@ protected function exists(string $key, ?array $collection = []): bool
*
* @return mixed
*/
protected function normalize($data, $depth = 0)
protected function normalize(mixed $data, int $depth = 0): mixed
{
// Convert $data from Monolog 3 LogRecord to Monolog 1/2 style array.
// https://github.com/Seldaek/monolog/blob/main/UPGRADE.md#300
if ($data instanceof LogRecord) {
$data = $data->toArray();
}

if (is_array($data) &&
isset($data['message']) &&
substr($data['message'], 0, 9) === 'exception' &&
Expand Down

0 comments on commit 7d2a35c

Please sign in to comment.