Skip to content

Commit

Permalink
Merge pull request #8 from CottaCush/features/multiple-log-targets
Browse files Browse the repository at this point in the history
Implement multiple log targets
  • Loading branch information
yemexx1 committed Sep 11, 2017
2 parents ab190c2 + 31bd75d commit a0fff74
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 12 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,8 @@
- Added Transformable interface
- Added Response Data Serializer for consistent response from transformers
- Added Date, File and Text Utils
- Added Custom Logger
- Added Custom Logger

# [2.3.0](https://github.com/CottaCush/phalcon-utils/releases/tag/2.3.0) (2017-09-08)
- Add Multiple Logging targets
- Allow Multiple log targets for request logger middleware
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"ext-phalcon": "^3.0",
"swiftmailer/swiftmailer": "@stable",
"phalcon/incubator": "^3.0",
"aws/aws-sdk-php": "^3.3"
"aws/aws-sdk-php": "^3.3",
"psr/log": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "~4.5||~6.0",
Expand Down
15 changes: 15 additions & 0 deletions src/Exceptions/InvalidLoggerConfigException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace PhalconUtils\src\Exceptions;

use Phalcon\Exception;

/**
* Class InvalidLoggerConfigException
* @author Adeyemi Olaoye <yemi@cottacush.com>
* @package PhalconUtils\src\Exceptions
*/
class InvalidLoggerConfigException extends Exception
{

}
25 changes: 19 additions & 6 deletions src/Middleware/RequestLoggerMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PhalconUtils\Middleware;

use Phalcon\Logger\Adapter\File;
use Phalcon\Mvc\Micro;
use PhalconUtils\Constants\Services;
use PhalconUtils\Util\Logger;
Expand All @@ -13,6 +14,22 @@
*/
class RequestLoggerMiddleware extends BaseMiddleware
{
/** @var Logger */
protected $logger;

public function __construct($logTargets = [])
{
$config = $this->getDI()->get(Services::CONFIG);

if (!$logTargets) {
$fileTarget = new File($config->application->logsDir . 'requests.log');
$logTargets = [$fileTarget];
}

$this->logger = new Logger($logTargets);

}

public function beforeExecuteRoute()
{
if ($this->isExcludedPath($this->getDI()->get(Services::CONFIG)->requestLogger->excluded_paths)) {
Expand All @@ -22,14 +39,10 @@ public function beforeExecuteRoute()
/** @var \Phalcon\Http\Request $request */
$request = $this->getDI()->get(Services::REQUEST);

$config = $this->getDI()->get(Services::CONFIG);

$logger = new Logger($config->application->logsDir . "requests.log");

$logger->log('Request URL:' . $request->getURI(), \Phalcon\Logger::INFO);
$this->logger->info('Request URL:' . $request->getURI());
if ($request->isPost() || $request->isPut()) {
$rawBody = $request->getRawBody();
$logger->log('Request Body: ' . $rawBody, \Phalcon\Logger::INFO);
$this->logger->info('Request Body: ' . $rawBody);
}
}

Expand Down
68 changes: 64 additions & 4 deletions src/Util/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,41 @@

namespace PhalconUtils\Util;

use Phalcon\Logger\Adapter\File;
use Phalcon\Http\Client\Response as HttpResponse;
use Phalcon\Logger as PhalconLogLevel;
use Phalcon\Logger\AdapterInterface;
use PhalconUtils\Http\BaseGateway;
use PhalconUtils\src\Exceptions\InvalidLoggerConfigException;
use Psr\Log\AbstractLogger;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use SoapClient;

/**
* Class Logger
* @author Adeyemi Olaoye <yemi@cottacush.com>
* @package PhalconUtils\Util
*/
class Logger extends File
class Logger extends AbstractLogger implements LoggerInterface
{
protected $debugEnabled;

public function __construct($name, $options = null)
/** @var AdapterInterface[] | LoggerInterface[] */
protected $logTargets;

public function __construct(array $logTargets, $options = [])
{
$this->debugEnabled = ArrayUtils::getValue($options, 'debug', false);
parent::__construct($name, $options);
$this->logTargets = array_filter($logTargets);

foreach ($this->logTargets as $logTarget) {
if (!($logTarget instanceof LoggerInterface || $logTarget instanceof AdapterInterface)) {
throw new InvalidLoggerConfigException(
'Log target must be instance of ' . LoggerInterface::class . ' or ' .
AdapterInterface::class . '; encountered ' . get_class($logTarget)
);
}
}
}

public function debug($message, array $context = null)
Expand Down Expand Up @@ -61,4 +78,47 @@ public function debugHttpServiceResponse(BaseGateway $service, HttpResponse $res
{
$this->debug('Service Response ' . get_class($service) . ':' . var_export($response, true));
}

/**
* Logs with an arbitrary level.
*
* @param mixed $level
* @param string $message
* @param array $context
*
* @return void
*/
public function log($level, $message, array $context = [])
{
/** @var LoggerInterface|AdapterInterface $logTarget */
foreach ($this->logTargets as $logTarget) {
if (($logTarget instanceof AdapterInterface)) {
$logTarget->log($this->getPhalconLoggerLevel($level), $message, $context);
} else {
$logTarget->log($level, $message, $context);
}
}
}

/**
* Get Phalcon Log Level from PSR Log level
* @author Adeyemi Olaoye <yemi@cottacush.com>
* @param $level
* @return null
*/
private function getPhalconLoggerLevel($level)
{
$levelMap = [
LogLevel::INFO => PhalconLogLevel::INFO,
LogLevel::DEBUG => PhalconLogLevel::DEBUG,
LogLevel::ALERT => PhalconLogLevel::ALERT,
LogLevel::CRITICAL => PhalconLogLevel::CRITICAL,
LogLevel::EMERGENCY => PhalconLogLevel::EMERGENCY,
LogLevel::ERROR => PhalconLogLevel::ERROR,
LogLevel::NOTICE => PhalconLogLevel::NOTICE,
LogLevel::WARNING => PhalconLogLevel::WARNING
];

return ArrayUtils::getValue($levelMap, $level, PhalconLogLevel::INFO);
}
}

0 comments on commit a0fff74

Please sign in to comment.