Skip to content

Improved missing typing and comments #147

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 2 commits into from
Mar 6, 2023
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
38 changes: 26 additions & 12 deletions src/MonologHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,53 @@

namespace Rollbar\Laravel;

use Illuminate\Contracts\Foundation\Application;
use Monolog\Handler\RollbarHandler;
use Monolog\LogRecord;

class MonologHandler extends RollbarHandler
{
protected $app;
protected Application $app;

/**
* @param $app
* Sets the Laravel application, so it can be used to get the current user and session data later.
*
* @param Application $app The Laravel application.
*
* @return void
*/
public function setApp($app)
public function setApp(Application $app): void
{
$this->app = $app;
}

/**
* @param LogRecord $record
* Custom write method to add the Laravel context to the log record for Rollbar.
*
* @param LogRecord $record The Monolog log record.
*
* @return void
*/
protected function write(LogRecord $record): void
{
parent::write(new LogRecord($record->datetime,
$record->channel,
$record->level,
$record->message,
$this->addContext($record->context),
$record->extra,
$record->formatted));
parent::write(
new LogRecord(
$record->datetime,
$record->channel,
$record->level,
$record->message,
$this->addContext($record->context),
$record->extra,
$record->formatted
)
);
}

/**
* @param array $context
* Adds the Laravel context to the log record for Rollbar. This includes person and session data.
*
* @param array $context The {@see LogRecord::context} array.
*
* @return array
*/
protected function addContext(array $context = []): array
Expand Down
21 changes: 11 additions & 10 deletions src/RollbarServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use Rollbar\RollbarLogger;
use Illuminate\Support\Arr;
use InvalidArgumentException;
use Rollbar\Laravel\MonologHandler;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\ServiceProvider;

Expand All @@ -13,14 +13,14 @@ class RollbarServiceProvider extends ServiceProvider
/**
* Register the service provider.
*/
public function register()
public function register(): void
{
// Don't register rollbar if it is not configured.
if ($this->stop() === true) {
return;
}

$this->app->singleton(RollbarLogger::class, function ($app) {
$this->app->singleton(RollbarLogger::class, function (Application $app) {

$defaults = [
'environment' => $app->environment(),
Expand Down Expand Up @@ -54,7 +54,7 @@ public function register()
return Rollbar::logger();
});

$this->app->singleton(MonologHandler::class, function ($app) {
$this->app->singleton(MonologHandler::class, function (Application $app) {

$level = static::config('level', 'debug');

Expand All @@ -66,7 +66,7 @@ public function register()
}

/**
* Check if we should prevent the service from registering
* Check if we should prevent the service from registering.
*
* @return boolean
*/
Expand All @@ -82,15 +82,16 @@ public function stop() : bool
}

/**
* Return a rollbar logging config
* Return a rollbar logging config.
*
* @param string $key The config key to lookup.
* @param mixed $default The default value to return if the config is not found.
*
* @param array|string $key
* @param mixed $default
* @return mixed
*/
protected static function config($key = '', $default = null)
protected static function config(string $key = '', mixed $default = null): mixed
{
$envKey = 'ROLLBAR_'.strtoupper($key);
$envKey = 'ROLLBAR_' . strtoupper($key);

if ($envKey === 'ROLLBAR_ACCESS_TOKEN') {
$envKey = 'ROLLBAR_TOKEN';
Expand Down
3 changes: 2 additions & 1 deletion tests/RollbarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@

class RollbarTest extends \Orchestra\Testbench\TestCase
{
private string $access_token = 'B42nHP04s06ov18Dv8X7VI4nVUs6w04X';

protected function setUp(): void
{
$this->access_token = 'B42nHP04s06ov18Dv8X7VI4nVUs6w04X';
putenv('ROLLBAR_TOKEN=' . $this->access_token);

parent::setUp();
Expand Down