Skip to content

Commit 8a24975

Browse files
author
Bishop Bettini
authored
Merge pull request #109 from rollbar/bug/CH-81893/cannot-use-rollbar-agent
Add support for rollbar-agent
2 parents 8b7a989 + ff681d5 commit 8a24975

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/vendor
22
composer.lock
33
.DS_Store
4+
.phpunit.result.cache

src/AgentHandler.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Rollbar\Laravel;
4+
5+
/**
6+
* Adaptor for [rollbar-agent][1] in Laravel logging configuration.
7+
*
8+
* The configuration for logging in Laravel requires an instance of
9+
* [`Monolog\Handler\HandlerInterface`][2], but the Rollbar library requires
10+
* a string with value of "agent" to use the agent.
11+
*
12+
* This class carries the HandlerInterface requirement to satisfy the Laravel
13+
* logging configuration and is recognized in the ServiceProvider, which
14+
* converts it to the appropriate agent configuration just in time.
15+
*
16+
* This issue was raised by [Issue 85][3].
17+
*
18+
* [1]:https://github.com/rollbar/rollbar-agent
19+
* [2]:https://github.com/Seldaek/monolog/blob/main/src/Monolog/Handler/HandlerInterface.php
20+
* [3]:https://github.com/rollbar/rollbar-php-laravel/issues/85
21+
*/
22+
class AgentHandler extends MonologHandler
23+
{
24+
}

src/RollbarServiceProvider.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ public function register()
4242
$handleError = (bool) Arr::pull($config, 'handle_error');
4343
$handleFatal = (bool) Arr::pull($config, 'handle_fatal');
4444

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

4754
return Rollbar::logger();

tests/RollbarTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Rollbar\Laravel\RollbarServiceProvider;
66
use Rollbar\Laravel\MonologHandler;
7+
use Rollbar\Laravel\AgentHandler;
78
use Rollbar\RollbarLogger;
89
use Monolog\Logger;
910
use Mockery;
@@ -32,6 +33,9 @@ public function testBinding()
3233

3334
$handler = $this->app->make(MonologHandler::class);
3435
$this->assertInstanceOf(MonologHandler::class, $handler);
36+
37+
$handler = $this->app->make(AgentHandler::class);
38+
$this->assertInstanceOf(AgentHandler::class, $handler);
3539
}
3640

3741
public function testIsSingleton()
@@ -62,6 +66,20 @@ public function testCustomConfiguration()
6266
$this->assertEquals(E_ERROR, $config['included_errno']);
6367
}
6468

69+
public function testRollbarAgentConfigurationAdapter()
70+
{
71+
$this->app->config->set('logging.channels.rollbar.handler', AgentHandler::class);
72+
73+
$client = $this->app->make(RollbarLogger::class);
74+
$config = $client->extend([]);
75+
76+
$this->assertEquals(
77+
'agent',
78+
$config['handler'],
79+
'AgentHandler given as Laravel logging config handler should be given as "agent" to Rollbar::init'
80+
);
81+
}
82+
6583
// public function testAutomaticContext()
6684
// {
6785
// $this->app->session->put('foo', 'bar');

0 commit comments

Comments
 (0)