Skip to content

Commit

Permalink
Merge pull request #55 from cloud-solutions/zend-sentry-update
Browse files Browse the repository at this point in the history
Zend sentry update
  • Loading branch information
markushausammann authored Oct 16, 2018
2 parents 7052179 + 34ca66e commit 5c1b13e
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 207 deletions.
12 changes: 0 additions & 12 deletions .travis.yml-prep

This file was deleted.

93 changes: 52 additions & 41 deletions Module.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

/**
* cloud solutions ZendSentry
* Bright Answer ZendSentry
*
* This source file is part of the cloud solutions ZendSentry package
* This source file is part of the Bright Answer ZendSentry package
*
* @package ZendSentry\Module
* @license New BSD License {@link /docs/LICENSE}
* @copyright Copyright (c) 2013, cloud solutions
* @license MIT License {@link /docs/LICENSE}
* @copyright Copyright (c) 2018, Bright Answer
*/

namespace ZendSentry;
Expand All @@ -30,7 +30,7 @@ class Module
/**
* Translates Zend Framework log levels to Raven log levels.
*/
private $logLevels = array(
private $logLevels = [
7 => Raven::DEBUG,
6 => Raven::INFO,
5 => Raven::INFO,
Expand All @@ -39,7 +39,7 @@ class Module
2 => Raven::FATAL,
1 => Raven::FATAL,
0 => Raven::FATAL,
);
];

/**
* @var Raven $ravenClient
Expand All @@ -64,7 +64,7 @@ class Module
/**
* @param MvcEvent $event
*/
public function onBootstrap(MvcEvent $event)
public function onBootstrap(MvcEvent $event): void
{
// Setup RavenClient (provided by Sentry) and Sentry (provided by this module)
$this->config = $event->getApplication()->getServiceManager()->get('Config');
Expand All @@ -73,10 +73,10 @@ public function onBootstrap(MvcEvent $event)
return;
}

if (isset($this->config['zend-sentry']['raven-config']) && is_array($this->config['zend-sentry']['raven-config'])) {
if (isset($this->config['zend-sentry']['raven-config']) && \is_array($this->config['zend-sentry']['raven-config'])) {
$ravenConfig = $this->config['zend-sentry']['raven-config'];
} else {
$ravenConfig = array();
$ravenConfig = [];
}

$sentryApiKey = $this->config['zend-sentry']['sentry-api-key'];
Expand All @@ -103,7 +103,7 @@ public function onBootstrap(MvcEvent $event)

// If ZendSentry is configured to log errors, register it as error handler
if ($this->config['zend-sentry']['handle-errors']) {
$errorReportingLevel = (isset($this->config['zend-sentry']['error-reporting'])) ? $this->config['zend-sentry']['error-reporting'] : -1;
$errorReportingLevel = $this->config['zend-sentry']['error-reporting'] ?? -1;
$this->zendSentry->registerErrorHandler($this->config['zend-sentry']['call-existing-error-handler'], $errorReportingLevel);
}

Expand All @@ -121,11 +121,15 @@ public function onBootstrap(MvcEvent $event)
/**
* @return array
*/
public function getAutoloaderConfig()
public function getAutoloaderConfig(): array
{
return array('Zend\Loader\StandardAutoloader' => array('namespaces' => array(
__NAMESPACE__ => __DIR__.'/src/'.__NAMESPACE__,
)));
return [
'Zend\Loader\StandardAutoloader' => [
'namespaces' => [
__NAMESPACE__ => __DIR__.'/src/'.__NAMESPACE__,
]
]
];
}

/**
Expand All @@ -134,8 +138,7 @@ public function getAutoloaderConfig()
public function getConfig()
{
return include __DIR__.'/config/module.config.php';
}/** @noinspection PhpUnusedParameterInspection */
/** @noinspection PhpUnusedParameterInspection */
}

/**
* Gives us the possibility to write logs to Sentry from anywhere in the application
Expand All @@ -144,7 +147,7 @@ public function getConfig()
*
* @param MvcEvent $event
*/
protected function setupBasicLogging(MvcEvent $event)
protected function setupBasicLogging(MvcEvent $event): void
{
// Get the shared event manager and attach a logging listener for the log event on application level
$sharedManager = $this->eventManager->getSharedManager();
Expand All @@ -153,18 +156,18 @@ protected function setupBasicLogging(MvcEvent $event)

$sharedManager->attach('*', 'log', function($event) use ($raven, $logLevels) {
/** @var $event MvcEvent */
if (is_object($event->getTarget())) {
$target = get_class($event->getTarget());
if (\is_object($event->getTarget())) {
$target = \get_class($event->getTarget());
} else {
$target = (string) $event->getTarget();
}
$message = $event->getParam('message', 'No message provided');
$priority = (int) $event->getParam('priority', Logger::INFO);
$message = sprintf('%s: %s', $target, $message);
$tags = $event->getParam('tags', array());
$extra = $event->getParam('extra', array());
$eventID = $raven->captureMessage($message, array(), array('tags' => $tags, 'level' => $logLevels[$priority], 'extra' => $extra));

$tags = $event->getParam('tags', []);
$extra = $event->getParam('extra', []);
$eventID = $raven->captureMessage($message, [], ['tags' => $tags, 'level' => $logLevels[$priority], 'extra' => $extra]
);
return $eventID;
}, 2);
}
Expand All @@ -175,7 +178,7 @@ protected function setupBasicLogging(MvcEvent $event)
*
* @param MvcEvent $event
*/
protected function setupExceptionLogging(MvcEvent $event)
protected function setupExceptionLogging(MvcEvent $event): void
{
// Register Sentry as exception handler for exception that bubble up to the top
$this->zendSentry->registerExceptionHandler($this->config['zend-sentry']['call-existing-exception-handler']);
Expand All @@ -186,22 +189,23 @@ protected function setupExceptionLogging(MvcEvent $event)
$exceptionStrategy = $event->getApplication()->getServiceManager()->get('HttpExceptionStrategy');
$exceptionStrategy->detach($this->eventManager);
}

// Check if script is running in console
$exceptionStrategy = (PHP_SAPI == 'cli') ? (new SentryConsoleStrategy()) : (new SentryHttpStrategy());
$exceptionStrategy = (PHP_SAPI == 'cli') ? new SentryConsoleStrategy() : new SentryHttpStrategy();
$exceptionStrategy->attach($this->eventManager);
$exceptionStrategy->setDisplayExceptions($this->config['zend-sentry']['display-exceptions']);
$exceptionStrategy->setDefaultExceptionMessage($this->config['zend-sentry'][(PHP_SAPI == 'cli') ? 'default-exception-console-message' : 'default-exception-message']);

if ($exceptionStrategy instanceof SentryHttpStrategy && isset($this->config['view_manager']['exception_template'])) {
$exceptionStrategy->setExceptionTemplate($this->config['view_manager']['exception_template']);
}
$ravenClient = $this->ravenClient;

// Attach an exception listener for the ZendSentry exception strategy, can be triggered from anywhere else too
$this->eventManager->getSharedManager()->attach('*', 'logException', function($event) use ($ravenClient) {
/** @var $event MvcEvent */
$exception = $event->getParam('exception');
$tags = $event->getParam('tags', array());
$eventID = $ravenClient->captureException($exception, array('tags' => $tags));
return $eventID;
$tags = $event->getParam('tags', []);
return $ravenClient->captureException($exception, ['tags' => $tags]);
});
}

Expand All @@ -210,30 +214,37 @@ protected function setupExceptionLogging(MvcEvent $event)
*
* @param MvcEvent $event
*/
protected function setupJavascriptLogging(MvcEvent $event)
protected function setupJavascriptLogging(MvcEvent $event): void
{
$viewHelper = $event->getApplication()->getServiceManager()->get('viewhelpermanager')->get('headscript');
/** @noinspection PhpUndefinedMethodInspection */
$viewHelper->offsetSetFile(0, '//cdn.ravenjs.com/3.17.0/raven.min.js');
$viewHelper = $event->getApplication()->getServiceManager()->get('ViewHelperManager')->get('headscript');
$useRavenjsCDN = $this->config['zend-sentry']['use-ravenjs-cdn'];
if (!isset($useRavenjsCDN) || $useRavenjsCDN) {
/** @noinspection PhpUndefinedMethodInspection */
$viewHelper->offsetSetFile(0, '//cdn.ravenjs.com/3.26.2/raven.min.js');
}
$publicApiKey = $this->convertKeyToPublic($this->config['zend-sentry']['sentry-api-key']);
$ravenjsConfig = json_encode($this->config['zend-sentry']['ravenjs-config']);
/** @noinspection PhpUndefinedMethodInspection */
$viewHelper->offsetSetScript(1, sprintf("Raven.config('%s').install()", $publicApiKey));
$viewHelper->offsetSetScript(1, sprintf("if (typeof Raven !== 'undefined') Raven.config('%s', %s).install()", $publicApiKey, $ravenjsConfig));
}

/**
* @param string $key
* @return string $publicKey
*/
private function convertKeyToPublic($key)
private function convertKeyToPublic($key): string
{
// Find private part
// If new DSN is configured, no converting is needed
if (substr_count($key, ':') == 1) {
return $key;
}
// If legacy DSN with private part is configured...
// ...find private part
$start = strpos($key, ':', 6);
$end = strpos($key, '@');
$privatePart = substr($key, $start, $end - $start);

// Replace it with an empty string
$publicKey = str_replace($privatePart, '', $key);

return $publicKey;
// ... replace it with an empty string
return str_replace($privatePart, '', $key);
}
}
48 changes: 26 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
# NB! We're deprecating the 2.* version branch for ZF 2.


A Zend Framework module that lets you log exceptions, errors or whatever you wish to the Sentry service.
A Zend Framework 3 module that lets you log exceptions, errors or whatever you wish to the Sentry.io service.

Scrutizier analysis: [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/cloud-solutions/zend-sentry/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/cloud-solutions/zend-sentry/?branch=master) [![Build Status](https://scrutinizer-ci.com/g/cloud-solutions/zend-sentry/badges/build.png?b=master)](https://scrutinizer-ci.com/g/cloud-solutions/zend-sentry/build-status/master)

ZendSentry is released under the New BSD License.
ZendSentry is released under the MIT License.

The current version of ZendSentry for ZF2 is `2.4.0`. It supports Zend Framework >= 2.5.3. For older versions see the legacy branch and tags in the `1.*` series. For ZF3 compatible versions, please install releases in the `3.*` branch.
The current version of ZendSentry for ZF3 is `3.5.0`. It supports Zend Framework >= 3.0. For other versions see tags in the 1.* series as well as 2.* series. **NB!** We are not supporting the old branches anymore.

# Important Changes
# Recent Changes
- 3.5.0: Add support for new Sentry DSN, deprecate old DSN for later removal
- 3.4.0: Add possibility to switch off usage of raven-js CDN
- 3.3.0: Add possibility to pass config options to ravenjs
- 3.2.0 and 2.4.0: Upgrade dependencies to `sentry/sentry` 1.7.0 and `ravenjs` 3.17.0
- 3.0.1: ViewHelper fix
- 3.0.0: First ZF2 release with latest sentry SDK dependencies and ZF3 compatibility fixes
- 2.2.1: Fix: Only detach HttpExceptionStrategy if it exists
- 2.0.0: New major version for ZF >=2.5.3
- 1.5.2: Configurable error messages
- 1.4.0: Raven configuration can now be overwritten through ZendSentry configuration if needed
- 1.2.0: supports tags, every logging action returns the Sentry event_id, Raven is registered as Service
- 0.3.1: dedicated CLI ExceptionStrategy (credits to Mateusz Mirosławski)

# Introduction

Expand All @@ -28,10 +18,11 @@ The current version of ZendSentry for ZF2 is `2.4.0`. It supports Zend Framework
exceptions and errors. Sentry creates nice reports in real time and aggregates your logged data for you.

## What's ZendSentry
It is a module that builds the bridge between your Zend Framework 2 application and the Sentry service. It's extremely
It is a module that builds the bridge between your Zend Framework 3 application and the Sentry.io service. It's extremely
easy to setup and does a lot of things out-of-the-box.

Current features:
Features and capabilities:

* log uncatched PHP exceptions to Sentry automagically
* log PHP errors to Sentry automagically
* log uncatched Javascript errors to Sentry automagically
Expand All @@ -41,7 +32,8 @@ Current features:
* log actions return the Sentry event_id
* Raven is registered as a Service
* override Raven config defaults
* set ravenjs options via config
* pass config options to ravenjs
* configure error messages

# Installation

Expand All @@ -50,7 +42,7 @@ In your project's `composer.json` use:

{
"require": {
"cloud-solutions/zend-sentry": "2.4.0"
"cloud-solutions/zend-sentry": "3.5.0"
}

Run `php composer.phar update` to download it into your vendor folder and setup autoloading.
Expand Down Expand Up @@ -150,11 +142,11 @@ You might want to do something like this e.g. in your `AbstractActionController:
$ravenClient->tags_context(
[
'locale' => $this->translator()->getLocale(),
...
]
);
}


# Configuration options

Just for the record, a copy of the actual global configuration options:
Expand Down Expand Up @@ -221,10 +213,22 @@ Just for the record, a copy of the actual global configuration options:
'handle-javascript-errors' => true,

/**
* Set raven config options here.
* Should ZendSentry load raven-js via CDN?
* If you set this to false you'll need to make sure to load raven-js some other way.
*/
'use-ravenjs-cdn' => true,

/**
* Set raven config options for the getsentry/sentry-php package here.
* Raven has sensible defaults set in Raven_Client, if you need to override them, this is where you can do it.
*/
'raven-config' => array(),

/**
* Set ravenjs config options for the getsentry/raven-js package here.
* This will be json encoded and passed to raven-js when doing Raven.install().
*/
'ravenjs-config' => array(),

# Try it
A few ideas how to try the different features from a Controller or View:
Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "cloud-solutions/zend-sentry",
"description": "A Zend Framework module that lets you log to the Sentry service.",
"description": "A Zend Framework 3 module that lets you log to the Sentry.io service.",
"keywords": ["log", "logging", "sentry", "raven", "zend-framework"],
"homepage": "https://github.com/cloud-solutions/zend-sentry",
"type": "library",
"version": "2.4.0",
"version": "3.5.0",
"license": "MIT",
"authors": [
{
Expand All @@ -13,11 +13,11 @@
}
],
"require": {
"php": "^5.5 || ^7.0",
"sentry/sentry": "1.7.0"
"php": "^5.6 || ^7.0",
"sentry/sentry": "^1.9.2"
},
"conflict": {
"zendframework/zendframework": "<2.5.3"
"zendframework/zendframework": "<3.0.0"
},
"autoload": {
"psr-0": {
Expand Down
4 changes: 2 additions & 2 deletions config/module.config.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php
return array(
);
return [
];
14 changes: 13 additions & 1 deletion config/zend-sentry.global.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,22 @@ $settings = array(
'handle-javascript-errors' => true,

/**
* Set raven config options here.
* Should ZendSentry load raven-js via CDN?
* If you set this to false you'll need to make sure to load raven-js some other way.
*/
'use-ravenjs-cdn' => true,

/**
* Set raven config options for the getsentry/sentry-php package here.
* Raven has sensible defaults set in Raven_Client, if you need to override them, this is where you can do it.
*/
'raven-config' => array(),

/**
* Set ravenjs config options for the getsentry/raven-js package here.
* This will be json encoded and passed to raven-js when doing Raven.install().
*/
'ravenjs-config' => array(),
);

/**
Expand Down
Loading

0 comments on commit 5c1b13e

Please sign in to comment.