Skip to content

Commit

Permalink
Initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
dajve committed Oct 14, 2017
1 parent 0c12cef commit 309fb0e
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 65 deletions.
2 changes: 2 additions & 0 deletions DependencyInjection/AlliesOroBugsnagExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ public function load(array $configs, ContainerBuilder $container)

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');

$container->prependExtensionConfig($this->getAlias(), array_intersect_key($config, array_flip(['settings'])));
}
}
18 changes: 17 additions & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Monolog\Logger;

use Oro\Bundle\ConfigBundle\DependencyInjection\SettingsBuilder;

class Configuration implements ConfigurationInterface
{
Expand All @@ -14,7 +17,20 @@ public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('allies_oro_bugsnag');


SettingsBuilder::append(
$rootNode,
[
'reporting_level' => ['value' => [
Logger::EMERGENCY,
Logger::ALERT,
Logger::CRITICAL,
Logger::ERROR,
Logger::WARNING,
]],
]
);

return $treeBuilder;
}
}
139 changes: 85 additions & 54 deletions Handler/BugsnagHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,75 +6,120 @@
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Logger;
use Bugsnag\Client as BugsnagClient;
use Bugsnag\Report as BugsnagReport;
use Oro\Bundle\ConfigBundle\Config\ConfigManager;

class BugsnagHandler extends AbstractProcessingHandler
{

/******************************************************************************
* PROPERTIES
******************************************************************************/

/**
* monolog error codes mapped on to bugSnag severities.
* @var string[]
* @var ContainerInterface
*/
protected $severityMapping = array(
Logger::DEBUG => 'info',
Logger::INFO => 'info',
Logger::NOTICE => 'info',
Logger::WARNING => 'warning',
Logger::ERROR => 'error',
Logger::CRITICAL => 'error',
Logger::ALERT => 'error',
Logger::EMERGENCY => 'error'
);

protected $container;

/**
* @var \Bugsnag\Client
* @var BugsnagClient
*/
protected $client;

public function setContainer(ContainerInterface $container)
{
$this->container = $container;

return $this;
}

public function getClient()
/**
* @var ConfigManager
*/
protected $configManager;

/******************************************************************************
* STATICS
******************************************************************************/

/**
* @param integer $errorCode
* @return string
*/
public static function getSeverity($errorCode)
{
if (is_null($this->client)) {
if (!$this->container) {
throw new \RuntimeException("Container must be defined");
}

$this->client = $this->container->get('bugsnag');
switch ($errorCode) {
case Logger::EMERGENCY :
case Logger::ALERT :
case Logger::CRITICAL :
case Logger::ERROR :
return 'error';
break;

case Logger::WARNING :
return 'warning';
break;

case Logger::NOTICE :
case Logger::INFO :
case Logger::DEBUG :
return 'info';
break;

default :
throw new \InvalidArgumentException(sprintf(
"Unknown errorCode %s passed",
(is_object($errorCode)) ? get_class($errorCode) : $errorCode
));
break;
}
}

/******************************************************************************
* MAGIC
******************************************************************************/

/**
* @param BugsnagClient $client
* @param ConfigManager $configManager
* @param integer $level
* @param boolean $bubble
*/
public function __construct(
BugsnagClient $client,
ConfigManager $configManager,
$level = Logger::ERROR,
$bubble = true
) {
parent::__construct($level, $bubble);

return $this->client;
$this->client = $client;
$this->configManager = $configManager;
}

/******************************************************************************
* ACTIONS
******************************************************************************/

/**
* Writes the record down to the log of the implementing handler
*
* @param array $record
* @param array $record
* @return void
*/
protected function write(array $record)
{
$severity = $this->getSeverity($record['level']);
{
if (!in_array($record['level'], $this->configManager->get('allies_oro_bugsnag.reporting_level'))) {
return;
}
$severity = self::getSeverity($record['level']);

if (isset($record['context']['exception'])) {
$this->getClient()->notifyException(
$this->client->notifyException(
$record['context']['exception'],
function (\Bugsnag\Report $report) use ($record, $severity) {
function (BugsnagReport $report) use ($record, $severity) {
$report->setSeverity($severity);
if (isset($record['extra'])) {
$report->setMetaData($record['extra']);
}
}
);
} else {
$this->getClient()->notifyError(
(string) $record['message'],
(string) $record['formatted'],
function (\Bugsnag\Report $report) use ($record, $severity) {
$this->client->notifyError(
(string)$record['message'],
(string)$record['formatted'],
function (BugsnagReport $report) use ($record, $severity) {
$report->setSeverity($severity);
if (isset($record['extra'])) {
$report->setMetaData($record['extra']);
Expand All @@ -83,18 +128,4 @@ function (\Bugsnag\Report $report) use ($record, $severity) {
);
}
}

/**
* Returns the Bugsnag severiry from a monolog error code.
* @param int $errorCode - one of the Logger:: constants.
* @return string
*/
protected function getSeverity($errorCode)
{
if (isset($this->severityMapping[$errorCode])) {
return $this->severityMapping[$errorCode];
} else {
return $this->severityMapping[Logger::ERROR];
}
}
}
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,5 @@ Bugsnag for OroCRM

A simple wrapper to push exceptions and errors logged through Monolog to the (Bugsnag)[https://www.bugsnag.com/] service.

Utilises

* `bugsnag/bugsnag-symfony` : [Bugsnag Symfony Bundle](https://github.com/bugsnag/bugsnag-symfony)
* `mead-steve/mono-snag` : [MonoSnag Bundle](https://github.com/meadsteve/MonoSnag)

Utilises [Bugsnag Symfony Bundle](https://github.com/bugsnag/bugsnag-symfony) `bugsnag/bugsnag-symfony`
Credit to [MonoSnag Bundle](https://github.com/meadsteve/MonoSnag) `mead-steve/mono-snag` for references
33 changes: 33 additions & 0 deletions Resources/config/oro/system_configuration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
system_configuration:
groups:
allies_oro_bugsnag:
title: allies.oro_bugsnag.system_configuration.groups.allies_oro_bugsnag.title
fields:
allies_oro_bugsnag.reporting_level:
data_type: string
type: choice
priority: 10
options:
label: allies.oro_bugsnag.system_configuration.fields.reporting_level.label
required: false
multiple: true
choices:
600: Emergency
550: Alert
500: Critical
400: Error
300: Warning
250: Notice
200: Info
100: Debug
tree:
system_configuration:
platform:
children:
general_setup:
children:
application_settings:
children:
allies_oro_bugsnag:
children:
- allies_oro_bugsnag.reporting_level
32 changes: 30 additions & 2 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,33 @@ parameters:
services:
allies_orobugsnag.handler:
class: %allies_orobugsnag.handler.class%
calls:
- [setContainer, [@service_container]]
arguments:
- @allies_orobugsnag.client
- @oro_config.manager

allies_orobugsnag.client:
parent: bugsnag
factory: ['@allies_orobugsnag.factory', make]

allies_orobugsnag.factory:
class: %bugsnag.factory%
arguments:
- @bugsnag.resolver
- null
- null
- '%bugsnag.api_key%'
- '%bugsnag.endpoint%'
- '%bugsnag.callbacks%'
- '%bugsnag.user%'
- '%bugsnag.app_type%'
- '%bugsnag.app_version%'
- '%bugsnag.batch_sending%'
- '%bugsnag.hostname%'
- '%bugsnag.send_code%'
- '%bugsnag.strip_path%'
- '%bugsnag.project_root%'
- '%kernel.root_dir%'
- '%kernel.environment%'
- '%bugsnag.release_stage%'
- '%bugsnag.notify_release_stages%'
- '%bugsnag.filters%'
6 changes: 6 additions & 0 deletions Resources/translations/messages.en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
allies_oro_bugsnag:
system_configuration:
groups:
allies_oro_bugsnag.title: Bugsnag
fields:
reporting_level.label: Reporting Level
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
"require": {
"php": ">=5.6",
"oro/platform": "2.*",
"bugsnag/bugsnag-symfony": "1.*",
"mead-steve/mono-snag": "^3.0.0"
"bugsnag/bugsnag-symfony": "1.*"
},
"autoload": {
"psr-0": { "Allies\\Bundle\\LogViewerBundle": "src/" }
"psr-0": { "Allies\\Bundle\\OroBugsnagBundle": "src/" }
},
"target-dir": "src/Allies/Bundle/OroBugsnagBundle",
"minimum-stability": "dev",
Expand Down

0 comments on commit 309fb0e

Please sign in to comment.