Skip to content

BC - Updates for Laravel 10 and the MonologHandler #142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
}
],
"require": {
"php": ">=7.2|^8.0",
"illuminate/support": "^6.0|^7.0|^8.0|^9.0|^10.0",
"php": "^8.1",
"illuminate/support": "^10.0",
"rollbar/rollbar": "^2.0 | ^3.1"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we may want to drop support for rollbar/rollbar v2 as it is not compatible with PHP 8.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

..and you're right it's not possible to support Laravel 9.x and below, as they only support monolog v2 and not v3.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we may want to drop support for rollbar/rollbar v2 as it is not compatible with PHP 8.

Removed older 2x

},
"require-dev": {
Expand Down
27 changes: 20 additions & 7 deletions src/MonologHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,41 @@
namespace Rollbar\Laravel;

use Monolog\Handler\RollbarHandler;
use Monolog\LogRecord;

class MonologHandler extends RollbarHandler
{
protected $app;

/**
* @param $app
* @return void
*/
public function setApp($app)
{
$this->app = $app;
}

protected function write(array $record): void
/**
* @param LogRecord $record
* @return void
*/
protected function write(LogRecord $record): void
{
$record['context'] = $this->addContext($record['context']);
parent::write($record);
parent::write(new LogRecord($record->datetime,
$record->channel,
$record->level,
$record->message,
(array_merge($record->toArray(), [ 'context' => $this->addContext($record->context) ])),
$record->extra,
$record->formatted));
}

/**
* Add Laravel specific information to the context.
*
* @param array $context
* @return array
*/
protected function addContext(array $context = [])
protected function addContext(array $context = []): array
{
$config = $this->rollbarLogger->extend([]);

Expand All @@ -47,7 +60,7 @@ protected function addContext(array $context = [])
} elseif (method_exists($data, 'getKey')) {
$person['id'] = $data->getKey();
}

if (isset($person['id'])) {
if (isset($data->username)) {
$person['username'] = $data->username;
Expand Down