Skip to content

Commit

Permalink
Merge branch 'master' into multilog
Browse files Browse the repository at this point in the history
  • Loading branch information
johannessteu authored Nov 8, 2017
2 parents 0fd567c + 8c6bd97 commit b090a51
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 37 deletions.
30 changes: 24 additions & 6 deletions Classes/Error/GraylogExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use Neos\Flow\Annotations as Flow;
use Neos\Flow\Error\ProductionExceptionHandler;
use Neos\Flow\ObjectManagement\DependencyInjection\DependencyProxy;
use Yeebase\Graylog\GraylogService;

/**
Expand All @@ -28,10 +29,10 @@ class GraylogExceptionHandler extends ProductionExceptionHandler
*/
protected function echoExceptionWeb($exception)
{
if ($this->graylogService === null) {
$this->graylogService = new GraylogService();
if (isset($this->renderingOptions['logException']) && $this->renderingOptions['logException']) {
$this->getGraylogService()->logException($exception);
}
$this->graylogService->logException($exception);

parent::echoExceptionWeb($exception);
}

Expand All @@ -41,10 +42,27 @@ protected function echoExceptionWeb($exception)
*/
protected function echoExceptionCli($exception)
{
if ($this->graylogService === null) {
$this->graylogService = new GraylogService();
if (isset($this->renderingOptions['logException']) && $this->renderingOptions['logException']) {
$this->getGraylogService()->logException($exception);
}
$this->graylogService->logException($exception);

parent::echoExceptionCli($exception);
}

/**
* Returns an instance of the injected GraylogService (including a fallback to a manually instantiated instance
* if Dependency Injection is not (yet) available)
*
* @return GraylogService
*/
private function getGraylogService()
{
if ($this->graylogService instanceof GraylogService) {
return $this->graylogService;
} elseif ($this->graylogService instanceof DependencyProxy) {
return $this->graylogService->_activateDependency();
} else {
return new GraylogService();
}
}
}
64 changes: 47 additions & 17 deletions Classes/Log/Backend/GraylogBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Log\Backend\AbstractBackend;
use Neos\Flow\Log\SystemLoggerInterface;
use Neos\Flow\ObjectManagement\DependencyInjection\DependencyProxy;
use Yeebase\Graylog\GraylogService;

/**
Expand Down Expand Up @@ -43,12 +44,13 @@ class GraylogBackend extends AbstractBackend
/**
* This method will send a message to our graylog service
*
* @param string $message
* @param int $severity
* @param null $additionalData
* @param null $packageKey
* @param null $className
* @param null $methodName
* @param string $message The message to log
* @param integer $severity One of the LOG_* constants
* @param mixed $additionalData A variable containing more information about the event to be logged
* @param string $packageKey Key of the package triggering the log (determined automatically if not specified)
* @param string $className Name of the class triggering the log (determined automatically if not specified)
* @param string $methodName Name of the method triggering the log (determined automatically if not specified)
* @return void
*/
public function append($message, $severity = LOG_INFO, $additionalData = null, $packageKey = null, $className = null, $methodName = null)
{
Expand All @@ -61,21 +63,26 @@ public function append($message, $severity = LOG_INFO, $additionalData = null, $

$output = $severityLabel . ': ' . $message;

$messageContext = [];
!is_null($packageKey) ? $messageContext['packageKey'] = $packageKey : '';
!is_null($className) ? $messageContext['className'] = $className : '';
!is_null($methodName) ? $messageContext['methodName'] = $methodName : '';
!is_null($additionalData) ? $messageContext['additionalData'] = $additionalData : '';
!is_null($ipAddress) ? $messageContext['ipAddress'] = $ipAddress : '';
!is_null($severityLabel) ? $messageContext['severityLabel'] = $severityLabel : '';

$this->graylogService->logMessage($output, $messageContext, $severity);

if ($this->alsoLogWithSystemLogger) {
$messageContext = [
'packageKey' => !is_null($packageKey) ? $packageKey : '',
'className' => !is_null($className) ? $className : '',
'methodName' => !is_null($methodName) ? $methodName : '',
'additionalData' => !is_null($additionalData) ? $additionalData : '',
'ipAddress' => !is_null($ipAddress) ? $ipAddress : '',
'severityLabel' => !is_null($severityLabel) ? $severityLabel : '',
];
$this->getGraylogService()->logMessage($output, $messageContext, $severity);
if ($this->alsoLogWithSystemLogger) {
$this->systemLogger->log($output, $severity, $additionalData, $packageKey, $className, $methodName);
}
}

/**
* Called when this backend is added to a logger
*
* @return void
*/
public function open()
{
$this->severityLabels = [
Expand All @@ -90,8 +97,31 @@ public function open()
];
}

/**
* Called when this backend is removed from a logger
*
* @return void
*/
public function close()
{
// nothing to do here
}

/**
* Returns an instance of the injected GraylogService (including a fallback to a manually instantiated instance
* if Dependency Injection is not (yet) available)
*
* @return GraylogService
*/
private function getGraylogService()
{
if ($this->graylogService instanceof GraylogService) {
return $this->graylogService;
} elseif ($this->graylogService instanceof DependencyProxy) {
return $this->graylogService->_activateDependency();
} else {
return new GraylogService();
}
}

/**
Expand Down
6 changes: 0 additions & 6 deletions Classes/Log/GraylogLoggerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,8 @@
* *
* */

use Neos\Flow\Annotations as Flow;
use Neos\Flow\Log\LoggerInterface;

/**
* Class ContentApiLogger
*
* @package Yeebase\t3n\ContentApi\Log
*/
interface GraylogLoggerInterface extends LoggerInterface
{

Expand Down
52 changes: 44 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
Yeebase.Graylog
================
# Yeebase.Graylog

The Yeebase.Graylog Flow package logs your exceptions as well as single messages to a central Graylog server. This
package also provides a simple backend to log message of Flows Logger classes to a Graylog server.

It depends on the official GELF php package https://github.com/bzikarsky/gelf-php

Installation & configuration
------------
## Installation & configuration

Just add "yeebase/graylog" as dependency to your composer.json and run a "composer update" in your project's root folder.
Just add "yeebase/graylog" as dependency to your composer.json and run a "composer update" in your project's root folder
or simply execute:
```
composer require yeebase/graylog
```
from your project's root.

Configure your Graylog Server:
```yaml
Expand All @@ -18,9 +21,9 @@ Yeebase:
host: '127.0.0.1'
port: 12201
chunksize: 'wan'
skipStatusCodes: [403, 404]
```
### Log exceptions
Activate the exception handler and configure the connection to your graylog server in your Settings.yaml:
Expand All @@ -32,17 +35,50 @@ Neos:
className: 'Yeebase\Graylog\Error\GraylogExceptionHandler'
```
If you wish to log normal log messages to your Graylog server just use the provided GraylogLoggerInterface:
*Note:* For `Development` context, the `Neos.Flow` package overrides this setting. Make sure to add this configuration
in the right context Settings.yaml.

#### Filter exceptions

To skip certain exceptions from being logged you can either use the `skipStatusCodes` setting:

```yaml
Yeebase:
Graylog:
# don't log any exceptions that would result in a HTTP status 403 (access denied) / 404 (not found)
skipStatusCodes: [403, 404]
```

Since version 2.1 you can alternatively use the `renderingGroups` Flow setting, i.e. to exclude certain Exception
*classes* from being logged:

```yaml
Neos:
Flow:
error:
exceptionHandler:
className: 'Yeebase\Graylog\Error\GraylogExceptionHandler'
renderingGroups:
'accessDeniedExceptions':
matchingExceptionClassNames: ['Neos\Flow\Security\Exception\AccessDeniedException']
options:
logException: false
```

### Manual logging


If you wish to log normal log messages to your Graylog server just use the provided `GraylogLoggerInterface`:

```php
use Neos\Flow\Annotations as Flow;
use Yeebase\Graylog\Log\GraylogLoggerInterface;
class SomeClass
{
/**
* @Flow\Inject
* @var Yeebase\Graylog\Log\GraylogLoggerInterface
* @var GraylogLoggerInterface
*/
protected $graylogLogger;
Expand Down

0 comments on commit b090a51

Please sign in to comment.