Skip to content

Add support for rollbar-agent #109

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 4 commits into from
Mar 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/vendor
composer.lock
.DS_Store
.phpunit.result.cache
24 changes: 24 additions & 0 deletions src/AgentHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Rollbar\Laravel;

/**
* Adaptor for [rollbar-agent][1] in Laravel logging configuration.
*
* The configuration for logging in Laravel requires an instance of
* [`Monolog\Handler\HandlerInterface`][2], but the Rollbar library requires
* a string with value of "agent" to use the agent.
*
* This class carries the HandlerInterface requirement to satisfy the Laravel
* logging configuration and is recognized in the ServiceProvider, which
* converts it to the appropriate agent configuration just in time.
*
* This issue was raised by [Issue 85][3].
*
* [1]:https://github.com/rollbar/rollbar-agent
* [2]:https://github.com/Seldaek/monolog/blob/main/src/Monolog/Handler/HandlerInterface.php
* [3]:https://github.com/rollbar/rollbar-php-laravel/issues/85
*/
class AgentHandler extends MonologHandler
{
}
7 changes: 7 additions & 0 deletions src/RollbarServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ public function register()
$handleError = (bool) Arr::pull($config, 'handle_error');
$handleFatal = (bool) Arr::pull($config, 'handle_fatal');

// Convert a request for the Rollbar agent to handle the logs to
// the format expected by `Rollbar::init`.
// @see https://github.com/rollbar/rollbar-php-laravel/issues/85
$handler = Arr::get($config, 'handler');
if ($handler === AgentHandler::class) {
$config['handler'] = 'agent';
}
Rollbar::init($config, $handleException, $handleError, $handleFatal);

return Rollbar::logger();
Expand Down
18 changes: 18 additions & 0 deletions tests/RollbarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Rollbar\Laravel\RollbarServiceProvider;
use Rollbar\Laravel\MonologHandler;
use Rollbar\Laravel\AgentHandler;
use Rollbar\RollbarLogger;
use Monolog\Logger;
use Mockery;
Expand Down Expand Up @@ -32,6 +33,9 @@ public function testBinding()

$handler = $this->app->make(MonologHandler::class);
$this->assertInstanceOf(MonologHandler::class, $handler);

$handler = $this->app->make(AgentHandler::class);
$this->assertInstanceOf(AgentHandler::class, $handler);
}

public function testIsSingleton()
Expand Down Expand Up @@ -62,6 +66,20 @@ public function testCustomConfiguration()
$this->assertEquals(E_ERROR, $config['included_errno']);
}

public function testRollbarAgentConfigurationAdapter()
{
$this->app->config->set('logging.channels.rollbar.handler', AgentHandler::class);

$client = $this->app->make(RollbarLogger::class);
$config = $client->extend([]);

$this->assertEquals(
'agent',
$config['handler'],
'AgentHandler given as Laravel logging config handler should be given as "agent" to Rollbar::init'
);
}

// public function testAutomaticContext()
// {
// $this->app->session->put('foo', 'bar');
Expand Down