Skip to content
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

Amended PR: Add listen new config param "minimum_level" error log. #46

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
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 .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ matrix:

before_install:
- composer self-update
- composer clear-cache
Copy link

Choose a reason for hiding this comment

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

Why was this change made? Seems completely unnecessary.

Copy link

@Yozhef Yozhef Dec 15, 2020

Choose a reason for hiding this comment

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

The head is here:
Change:

        return new RollbarHandler(Rollbar::logger(), LogLevel::ERROR);
To:
        return new RollbarHandler(Rollbar::logger(), $this->minimumLevel);

Because there are errors above the error equation, but in the config, lib has params minimum erorr level log.

- composer require -n --prefer-dist "symfony/symfony:${SYMFONY_VERSION}"

script:
Expand Down
40 changes: 24 additions & 16 deletions Factories/RollbarHandlerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
*/
class RollbarHandlerFactory
{
/**
* @var string|null
*/
private $minimumLevel;

/**
* RollbarHandlerFactory constructor.
*
Expand All @@ -30,25 +35,28 @@ public function __construct(ContainerInterface $container)

if (!empty($config['person_fn']) && is_callable($config['person_fn'])) {
$config['person'] = null;
} else {
if (empty($config['person'])) {
$config['person_fn'] = function () use ($container) {
try {
$token = $container->get('security.token_storage')->getToken();
} elseif (empty($config['person'])) {
$config['person_fn'] = function () use ($container) {

if ($token) {
$user = $token->getUser();
$serializer = $container->get('serializer');
$person = \json_decode($serializer->serialize($user, 'json'), true);
return $person;
}
} catch (\Exception $exception) {
// Ignore
try {
$token = $container->get('security.token_storage')->getToken();

if ($token) {
$user = $token->getUser();
$serializer = $container->get('serializer');

return \json_decode($serializer->serialize($user, 'json'), true);
}
};
}
} catch (\Exception $exception) {
// Ignore
}
};
}

$this->minimumLevel = isset($config['minimum_level'])
? $config['minimum_level']
: \Rollbar\Defaults::get()->minimumLevel();

Rollbar::init($config, false, false, false);
}

Expand All @@ -59,6 +67,6 @@ public function __construct(ContainerInterface $container)
*/
public function createRollbarHandler()
{
return new RollbarHandler(Rollbar::logger(), LogLevel::ERROR);
return new RollbarHandler(Rollbar::logger(), $this->minimumLevel);
}
}
56 changes: 56 additions & 0 deletions Tests/Factories/RollbarHandlerFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Rollbar\Symfony\RollbarBundle\Tests\Factories;

use Rollbar\Config;
use Rollbar\Defaults;
use Psr\Log\LogLevel;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Rollbar\Symfony\RollbarBundle\DependencyInjection\RollbarExtension;
use Rollbar\Symfony\RollbarBundle\Factories\RollbarHandlerFactory;
use Rollbar\Rollbar;
use Rollbar\Payload\Level;

/**
* Class RollbarHandlerFactoryTest
*
* @package Rollbar\Symfony\RollbarBundle\Tests\Factories;
*/
class RollbarHandlerFactoryTest extends KernelTestCase
{
public static function setUpBeforeClass()
{
self::bootKernel();
}

public function testMinimumLevel()
{
$factory = new RollbarHandlerFactory(self::$container);

$getAccessTokenMethod = new \ReflectionMethod('\Rollbar\RollbarLogger', 'getAccessToken');
$getAccessTokenMethod->setAccessible(true);
$accessToken = $getAccessTokenMethod->invoke(Rollbar::logger());

$mockLogger = $this->getMockBuilder('\Rollbar\RollbarLogger')
->setConstructorArgs(array(array(
'access_token' => $accessToken,
'minimum_level' => LogLevel::ERROR
)))
->getMock();

$rollbarClass = new \ReflectionClass('\Rollbar\Rollbar');
$setLoggerMethod = $rollbarClass->getMethod('setLogger');
$setLoggerMethod->setAccessible(true);
$setLoggerMethod->invoke(null, $mockLogger);

$logger = self::$container->get('test_alias.logger');

$mockLogger->expects($this->once())
->method('log')
->with($this->equalTo(Level::ERROR));

$logger->log(Level::ERROR, "Test info from the factory test");

$logger->log(Level::INFO, "Test debug from the factory test");
}
}
10 changes: 4 additions & 6 deletions Tests/Fixtures/app/config/config_test.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
imports:
- { resource: config.yml }
- { resource: parameters.yml }

rollbar:
access_token: ~
- { resource: packages/rollbar.yml }

monolog:
handlers:
Expand All @@ -12,7 +10,7 @@ monolog:
id: Rollbar\Monolog\Handler\RollbarHandler

services:
_defaults:
public: true

test.Rollbar\Symfony\RollbarBundle\Payload\Generator: '@Rollbar\Symfony\RollbarBundle\Payload\Generator'
test_alias.logger:
alias: 'logger'
public: true
5 changes: 5 additions & 0 deletions Tests/Fixtures/app/config/packages/rollbar.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
rollbar:
enabled: true
access_token: ~ # pulled from phpunit.xml
environment: 'rollbar-php-symfony-test-app'
minimum_level: 'info'
Copy link

Choose a reason for hiding this comment

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

please add an empty line.

Copy link
Author

Choose a reason for hiding this comment

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

will do

Copy link

Choose a reason for hiding this comment

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

Is this the reason this PR was never merged?

8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@
"require": {
"php": ">=5.6",
"rollbar/rollbar": "^1",
"symfony/dependency-injection": "^3.4 | ^4.0",
"symfony/config": "^3.4 | ^4.0",
"symfony/http-kernel": "^3.4 | ^4.0",
"symfony/dependency-injection": "^3.4 | ^4",
"symfony/config": "^3.4 | ^4",
"symfony/http-kernel": "^3.4 | ^4",
"symfony/monolog-bundle": "*",
"symfony/serializer": "*"
},
"require-dev": {
"phpunit/phpunit": "^5.7",
"symfony/framework-bundle": "^3.4 | ^4.0",
"symfony/framework-bundle": "^3.4 | ^4",
"squizlabs/php_codesniffer": "^2.7",
"matthiasnoback/symfony-dependency-injection-test": "^1.1"
},
Expand Down