Skip to content

Commit

Permalink
Extend from logger because legacy ...
Browse files Browse the repository at this point in the history
  • Loading branch information
jjsaunier committed Mar 21, 2017
1 parent cff3d31 commit c27112e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 98 deletions.
4 changes: 1 addition & 3 deletions src/Logger/LoggerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,7 @@ public function addProcess(callable $processor)
public function create($channelName)
{
if(!isset($this->loggers[$channelName])){
$this->loggers[$channelName] = new ExceptionLoggerDecorator(
new Logger($channelName, $this->handlers, $this->processors)
);
$this->loggers[$channelName] = new PyriteLogger($channelName, $this->handlers, $this->processors);
}

return $this->loggers[$channelName];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,9 @@
namespace Pyrite\Logger;

use Monolog\Logger;
use Psr\Log\LoggerInterface;

class ExceptionLoggerDecorator implements LoggerInterface
class PyriteLogger extends Logger
{
/**
* @var Logger
*/
private $logger;

/**
* ExceptionLoggerDecorator constructor.
*
* @param Logger $logger
*/
public function __construct(Logger $logger)
{
$this->logger = $logger;
}

/**
* @return Logger
*/
public function getDecoratedLogger()
{
return $this->logger;
}

/**
* @param \Exception $error
*
Expand Down Expand Up @@ -100,6 +76,8 @@ private function renderException(\Exception $e)
/**
* @param string $message
* @param array $context
*
* @return bool
*/
public function emergency($message, array $context = [])
{
Expand All @@ -108,16 +86,20 @@ public function emergency($message, array $context = [])

foreach($exceptions as $exception){
$errorContext = ['exception' => $exception];
$this->logger->emergency($exception['message'], array_merge($context, $errorContext));
parent::emergency($exception['message'], array_merge($context, $errorContext));
}
}else{
$this->logger->emergency($message, $context);

return true;
}

return parent::emergency($message, $context);
}

/**
* @param string $message
* @param array $context
*
* @return bool
*/
public function alert($message, array $context = [])
{
Expand All @@ -126,16 +108,18 @@ public function alert($message, array $context = [])

foreach($exceptions as $exception){
$errorContext = ['exception' => $exception];
$this->logger->alert($exception['message'], array_merge($context, $errorContext));
parent::alert($exception['message'], array_merge($context, $errorContext));
}
}else{
$this->logger->alert($message, $context);
}

return parent::alert($message, $context);
}

/**
* @param string $message
* @param array $context
*
* @return bool
*/
public function critical($message, array $context = [])
{
Expand All @@ -144,16 +128,20 @@ public function critical($message, array $context = [])

foreach($exceptions as $exception){
$errorContext = ['exception' => $exception];
$this->logger->critical($exception['message'], array_merge($context, $errorContext));
parent::error($exception['message'], array_merge($context, $errorContext));
}
}else{
$this->logger->critical($message, $context);

return true;
}

return parent::critical($message, $context);
}

/**
* @param string $message
* @param array $context
*
* @return bool
*/
public function error($message, array $context = [])
{
Expand All @@ -162,16 +150,20 @@ public function error($message, array $context = [])

foreach($exceptions as $exception){
$errorContext = ['exception' => $exception];
$this->logger->error($exception['message'], array_merge($context, $errorContext));
parent::error($exception['message'], array_merge($context, $errorContext));
}
}else{
$this->logger->error($message, $context);

return true;
}

return parent::error($message, $context);
}

/**
* @param string $message
* @param array $context
*
* @return bool
*/
public function warning($message, array $context = [])
{
Expand All @@ -180,48 +172,12 @@ public function warning($message, array $context = [])

foreach($exceptions as $exception){
$errorContext = ['exception' => $exception];
$this->logger->warning($exception['message'], array_merge($context, $errorContext));
parent::warning($exception['message'], array_merge($context, $errorContext));
}
}else{
$this->logger->warning($message, $context);
}
}

/**
* @param string $message
* @param array $context
*/
public function notice($message, array $context = [])
{
$this->logger->notice($message, $context);
}

/**
* @param string $message
* @param array $context
*/
public function info($message, array $context = [])
{
$this->logger->info($message, $context);
}

/**
* @param string $message
* @param array $context
*/
public function debug($message, array $context = [])
{
$this->logger->debug($message, $context);
}
return true;
}

/**
* @param mixed $level
* @param string $message
* @param array $context
*/
public function log($level, $message, array $context = [])
{
$this->logger->log($level, $message, $context);
return parent::warning($message, $context);
}

}
21 changes: 3 additions & 18 deletions src/Utils/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@ public static function attachSentry(LoggerFactory $factory, $callbackUrl, $debug
$sentryHandler = new RavenHandler($ravenClient, \Monolog\Logger::ERROR);
$sentryHandler->setFormatter(new LineFormatter('%message% %context% %extra%\n'));
$factory->addHandler($sentryHandler);

$decoratedLogger = $factory->getLogger('app');

if($decoratedLogger instanceof \Monolog\Logger){
$decoratedLogger->pushHandler($sentryHandler);
}
$factory->getLogger('app')->pushHandler($sentryHandler);
}

/**
Expand All @@ -48,12 +43,7 @@ public static function attachSlack(LoggerFactory $factory, $token, $channel, $us

$slackHandler = new SlackHandler($token, $channel, $username, true, null, \Monolog\Logger::ERROR, true, false, true);
$factory->addHandler($slackHandler);

$decoratedLogger = $factory->getLogger('app');

if($decoratedLogger instanceof \Monolog\Logger){
$decoratedLogger->pushHandler($slackHandler);
}
$factory->getLogger('app')->pushHandler($slackHandler);
}

/**
Expand All @@ -69,11 +59,6 @@ public static function attachNewRelic(LoggerFactory $factory, $appName, $debug)

$newRelicHandler = new NewRelicHandler(\Monolog\Logger::ERROR, true, $appName, true);
$factory->addHandler($newRelicHandler);

$decoratedLogger = $factory->getLogger('app');

if($decoratedLogger instanceof \Monolog\Logger){
$factory->getLogger('app')->pushHandler($newRelicHandler);
}
$factory->getLogger('app')->pushHandler($newRelicHandler);
}
}

0 comments on commit c27112e

Please sign in to comment.