forked from LiveCarta/PayPal-PHP-SDK
Enabled Factory to Inject Logger
This commit is contained in:
@@ -33,13 +33,6 @@ class OAuthTokenCredential extends PayPalResourceModel
|
||||
*/
|
||||
private static $expiryBufferTime = 120;
|
||||
|
||||
/**
|
||||
* Private Variable
|
||||
*
|
||||
* @var \PayPal\Core\PayPalLoggingManager $logger
|
||||
*/
|
||||
private $logger;
|
||||
|
||||
/**
|
||||
* Client ID as obtained from the developer portal
|
||||
*
|
||||
@@ -93,7 +86,6 @@ class OAuthTokenCredential extends PayPalResourceModel
|
||||
$this->clientId = $clientId;
|
||||
$this->clientSecret = $clientSecret;
|
||||
$this->cipher = new Cipher($this->clientSecret);
|
||||
$this->logger = PayPalLoggingManager::getInstance(__CLASS__);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -276,9 +268,7 @@ class OAuthTokenCredential extends PayPalResourceModel
|
||||
if ($response == null || !isset($response["access_token"]) || !isset($response["expires_in"])) {
|
||||
$this->accessToken = null;
|
||||
$this->tokenExpiresIn = null;
|
||||
$this->logger->warning(
|
||||
"Could not generate new Access token. Invalid response from server: "
|
||||
);
|
||||
PayPalLoggingManager::getInstance(__CLASS__)->warning("Could not generate new Access token. Invalid response from server: ");
|
||||
throw new PayPalConnectionException(null, "Could not generate new Access token. Invalid response from server: ");
|
||||
} else {
|
||||
$this->accessToken = $response["access_token"];
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace PayPal\Core;
|
||||
|
||||
/**
|
||||
* Logging Levels.
|
||||
* Class containing all the constants for Logging Levels.
|
||||
*/
|
||||
class PayPalLoggingLevel
|
||||
{
|
||||
|
||||
// DEBUG Logging Level
|
||||
const DEBUG = 4;
|
||||
|
||||
// FINE Logging Level
|
||||
const FINE = 3;
|
||||
|
||||
// INFO Logging Level
|
||||
const INFO = 2;
|
||||
|
||||
// WARN Logging Level
|
||||
const WARN = 1;
|
||||
|
||||
// ERROR Logging Level
|
||||
const ERROR = 0;
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace PayPal\Core;
|
||||
use PayPal\Log\PayPalLogger;
|
||||
|
||||
use PayPal\Log\PayPalLogFactory;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
@@ -11,32 +12,10 @@ use Psr\Log\LoggerInterface;
|
||||
*/
|
||||
class PayPalLoggingManager
|
||||
{
|
||||
|
||||
/**
|
||||
* Default Logging Level
|
||||
* @var array of logging manager instances with class name as key
|
||||
*/
|
||||
const DEFAULT_LOGGING_LEVEL = 0;
|
||||
|
||||
/**
|
||||
* Logger Name
|
||||
* @var string
|
||||
*/
|
||||
private $loggerName;
|
||||
|
||||
/**
|
||||
* Log Enabled
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $isLoggingEnabled;
|
||||
|
||||
/**
|
||||
* Configured Logging Level
|
||||
*
|
||||
* @var int|mixed
|
||||
*/
|
||||
private $loggingLevel;
|
||||
|
||||
private static $instances = array();
|
||||
|
||||
/**
|
||||
* The logger to be used for all messages
|
||||
@@ -45,6 +24,13 @@ class PayPalLoggingManager
|
||||
*/
|
||||
private $logger;
|
||||
|
||||
/**
|
||||
* Logger Name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $loggerName;
|
||||
|
||||
/**
|
||||
* Returns the singleton object
|
||||
*
|
||||
@@ -53,45 +39,32 @@ class PayPalLoggingManager
|
||||
*/
|
||||
public static function getInstance($loggerName = __CLASS__)
|
||||
{
|
||||
$instance = new self();
|
||||
$instance->setLoggerName($loggerName);
|
||||
if (array_key_exists($loggerName, PayPalLoggingManager::$instances)) {
|
||||
return PayPalLoggingManager::$instances[$loggerName];
|
||||
}
|
||||
$instance = new self($loggerName);
|
||||
PayPalLoggingManager::$instances[$loggerName] = $instance;
|
||||
return $instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets Logger Name. Generally defaulted to Logging Class
|
||||
*
|
||||
* @param string $loggerName
|
||||
*/
|
||||
public function setLoggerName($loggerName = __CLASS__)
|
||||
{
|
||||
$this->loggerName = $loggerName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Default Constructor
|
||||
*
|
||||
* @param string $loggerName Generally represents the class name.
|
||||
*/
|
||||
public function __construct()
|
||||
private function __construct($loggerName)
|
||||
{
|
||||
$config = PayPalConfigManager::getInstance()->getConfigHashmap();
|
||||
$this->isLoggingEnabled = (array_key_exists('log.LogEnabled', $config) && $config['log.LogEnabled'] == '1');
|
||||
if ($this->isLoggingEnabled) {
|
||||
$loggingLevel = strtoupper($config['log.LogLevel']);
|
||||
$this->setupLogger($config);
|
||||
$this->loggingLevel =
|
||||
(isset($loggingLevel) && defined(__NAMESPACE__ . "\\PayPalLoggingLevel::$loggingLevel")) ?
|
||||
constant(__NAMESPACE__ . "\\PayPalLoggingLevel::$loggingLevel") :
|
||||
PayPalLoggingManager::DEFAULT_LOGGING_LEVEL;
|
||||
if (!empty($config)) {
|
||||
// Checks if custom factory defined, and is it an implementation of @PayPalLogFactory
|
||||
$factory = array_key_exists('log.AdapterFactory', $config) && in_array('PayPal\Log\PayPalLogFactory', class_implements($config['log.AdapterFactory'])) ? $config['log.AdapterFactory'] : '\PayPal\Log\PayPalDefaultLogFactory';
|
||||
/** @var PayPalLogFactory $factoryInstance */
|
||||
$factoryInstance = new $factory();
|
||||
$this->logger = $factoryInstance->getLogger($loggerName);
|
||||
$this->loggerName = $loggerName;
|
||||
}
|
||||
}
|
||||
|
||||
private function setupLogger($config = array()) {
|
||||
// Checks if custom adapter defined, and is it an implementation of @LoggerInterface
|
||||
$loggingAdapter = array_key_exists('log.Adapter', $config) && in_array('\Psr\Log\LoggerInterface', class_implements($config['log.Adapter']))? $config['log.Adapter'] : '\PayPal\Log\PayPalLogger';
|
||||
$this->logger = new $loggingAdapter();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Log Error
|
||||
*
|
||||
@@ -99,9 +72,7 @@ class PayPalLoggingManager
|
||||
*/
|
||||
public function error($message)
|
||||
{
|
||||
if ($this->isLoggingEnabled && $this->loggingLevel >= PayPalLoggingLevel::ERROR) {
|
||||
$this->logger->error($message);
|
||||
}
|
||||
$this->logger->error($message);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -111,10 +82,7 @@ class PayPalLoggingManager
|
||||
*/
|
||||
public function warning($message)
|
||||
{
|
||||
if ($this->isLoggingEnabled && $this->loggingLevel >= PayPalLoggingLevel::WARN) {
|
||||
$this->logger->warning($message);
|
||||
}
|
||||
|
||||
$this->logger->warning($message);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -124,9 +92,7 @@ class PayPalLoggingManager
|
||||
*/
|
||||
public function info($message)
|
||||
{
|
||||
if ($this->isLoggingEnabled && $this->loggingLevel >= PayPalLoggingLevel::INFO) {
|
||||
$this->logger->info($message);
|
||||
}
|
||||
$this->logger->info($message);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -140,20 +106,16 @@ class PayPalLoggingManager
|
||||
}
|
||||
|
||||
/**
|
||||
* Log Fine
|
||||
* Log Debug
|
||||
*
|
||||
* @param string $message
|
||||
*/
|
||||
public function debug($message)
|
||||
{
|
||||
if ($this->isLoggingEnabled) {
|
||||
$config = PayPalConfigManager::getInstance()->getConfigHashmap();
|
||||
// Check if logging in live
|
||||
if (array_key_exists('mode', $config) && $config['mode'] == 'live' && $this->loggingLevel >= PayPalLoggingLevel::DEBUG) {
|
||||
$this->logger->error("Not allowed to keep 'Debug' level for Live Environments. Reduced to 'INFO'");
|
||||
} elseif (PayPalLoggingLevel::DEBUG <= $this->loggingLevel) {
|
||||
$this->logger->debug($message);
|
||||
}
|
||||
$config = PayPalConfigManager::getInstance()->getConfigHashmap();
|
||||
// Disable debug in live mode.
|
||||
if (array_key_exists('mode', $config) && $config['mode'] != 'live') {
|
||||
$this->logger->debug($message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
26
lib/PayPal/Log/PayPalDefaultLogFactory.php
Normal file
26
lib/PayPal/Log/PayPalDefaultLogFactory.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace PayPal\Log;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* Class PayPalDefaultLogFactory
|
||||
*
|
||||
* This factory is the default implementation of Log factory.
|
||||
*
|
||||
* @package PayPal\Log
|
||||
*/
|
||||
class PayPalDefaultLogFactory implements PayPalLogFactory
|
||||
{
|
||||
/**
|
||||
* Returns logger instance implementing LoggerInterface.
|
||||
*
|
||||
* @param string $className
|
||||
* @return LoggerInterface instance of logger object implementing LoggerInterface
|
||||
*/
|
||||
public function getLogger($className)
|
||||
{
|
||||
return new PayPalLogger($className);
|
||||
}
|
||||
}
|
||||
17
lib/PayPal/Log/PayPalLogFactory.php
Normal file
17
lib/PayPal/Log/PayPalLogFactory.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace PayPal\Log;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
interface PayPalLogFactory
|
||||
{
|
||||
/**
|
||||
* Returns logger instance implementing LoggerInterface.
|
||||
*
|
||||
* @param string $className
|
||||
* @return LoggerInterface instance of logger object implementing LoggerInterface
|
||||
*/
|
||||
public function getLogger($className);
|
||||
|
||||
}
|
||||
@@ -3,21 +3,31 @@
|
||||
namespace PayPal\Log;
|
||||
|
||||
use PayPal\Core\PayPalConfigManager;
|
||||
use PayPal\Core\PayPalLoggingManager;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Psr\Log\AbstractLogger;
|
||||
use Psr\Log\LogLevel;
|
||||
|
||||
|
||||
class PayPalLogger implements LoggerInterface {
|
||||
class PayPalLogger extends AbstractLogger
|
||||
{
|
||||
|
||||
/**
|
||||
* Default Logging Level
|
||||
* @var array Indexed list of all log levels.
|
||||
*/
|
||||
const DEFAULT_LOGGING_LEVEL = 0;
|
||||
private $loggingLevels = array(
|
||||
LogLevel::EMERGENCY,
|
||||
LogLevel::ALERT,
|
||||
LogLevel::CRITICAL,
|
||||
LogLevel::ERROR,
|
||||
LogLevel::WARNING,
|
||||
LogLevel::NOTICE,
|
||||
LogLevel::INFO,
|
||||
LogLevel::DEBUG
|
||||
);
|
||||
|
||||
/**
|
||||
* Configured Logging Level
|
||||
*
|
||||
* @var int|mixed
|
||||
* @var LogLevel $loggingLevel
|
||||
*/
|
||||
private $loggingLevel;
|
||||
|
||||
@@ -28,65 +38,48 @@ class PayPalLogger implements LoggerInterface {
|
||||
*/
|
||||
private $loggerFile;
|
||||
|
||||
public function initialize() {
|
||||
$config = PayPalConfigManager::getInstance()->getConfigHashmap();
|
||||
/**
|
||||
* Log Enabled
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $isLoggingEnabled;
|
||||
|
||||
/**
|
||||
* Logger Name. Generally corresponds to class name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $loggerName;
|
||||
|
||||
public function __construct($className)
|
||||
{
|
||||
$this->loggerName = $className;
|
||||
$this->initialize();
|
||||
}
|
||||
|
||||
public function initialize()
|
||||
{
|
||||
$config = PayPalConfigManager::getInstance()->getConfigHashmap();
|
||||
if (!empty($config)) {
|
||||
$this->loggerFile = ($config['log.FileName']) ? $config['log.FileName'] : ini_get('error_log');
|
||||
$loggingLevel = strtoupper($config['log.LogLevel']);
|
||||
$this->loggingLevel =
|
||||
(isset($loggingLevel) && defined(__NAMESPACE__ . "\\PayPalLoggingLevel::$loggingLevel")) ?
|
||||
constant(__NAMESPACE__ . "\\PayPalLoggingLevel::$loggingLevel") :
|
||||
PayPalLoggingManager::DEFAULT_LOGGING_LEVEL;
|
||||
$this->isLoggingEnabled = (array_key_exists('log.LogEnabled', $config) && $config['log.LogEnabled'] == '1');
|
||||
if ($this->isLoggingEnabled) {
|
||||
$this->loggerFile = ($config['log.FileName']) ? $config['log.FileName'] : ini_get('error_log');
|
||||
$loggingLevel = strtoupper($config['log.LogLevel']);
|
||||
$this->loggingLevel = (isset($loggingLevel) && defined("\\Psr\\Log\\LogLevel::$loggingLevel")) ?
|
||||
constant("\\Psr\\Log\\LogLevel::$loggingLevel") :
|
||||
LogLevel::INFO;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function emergency($message, array $context = array())
|
||||
{
|
||||
$this->error($message, $context);
|
||||
}
|
||||
|
||||
public function alert($message, array $context = array())
|
||||
{
|
||||
$this->error($message, $context);
|
||||
}
|
||||
|
||||
public function critical($message, array $context = array())
|
||||
{
|
||||
$this->error($message, $context);
|
||||
}
|
||||
|
||||
public function error($message, array $context = array())
|
||||
{
|
||||
$this->initialize();
|
||||
error_log("[" . date('d-m-Y h:i:s') . "] ERROR: $message\n", 3, $this->loggerFile);
|
||||
}
|
||||
|
||||
public function warning($message, array $context = array())
|
||||
{
|
||||
$this->initialize();
|
||||
error_log("[" . date('d-m-Y h:i:s') . "] WARNING: $message\n", 3, $this->loggerFile);
|
||||
}
|
||||
|
||||
public function notice($message, array $context = array())
|
||||
{
|
||||
$this->initialize();
|
||||
error_log("[" . date('d-m-Y h:i:s') . "] NOTICE: $message\n", 3, $this->loggerFile);
|
||||
}
|
||||
|
||||
public function info($message, array $context = array())
|
||||
{
|
||||
$this->initialize();
|
||||
error_log("[" . date('d-m-Y h:i:s') . "] INFO: $message\n", 3, $this->loggerFile);
|
||||
}
|
||||
|
||||
public function debug($message, array $context = array())
|
||||
{
|
||||
$this->initialize();
|
||||
error_log("[" . date('d-m-Y h:i:s') . "] DEBUG: $message\n", 3, $this->loggerFile);
|
||||
}
|
||||
|
||||
public function log($level, $message, array $context = array())
|
||||
{
|
||||
$this->debug($message, $context);
|
||||
if($this->isLoggingEnabled) {
|
||||
// Checks if the message is at level below configured logging level
|
||||
if (array_search($level, $this->loggingLevels) <= array_search($this->loggingLevel, $this->loggingLevels)) {
|
||||
error_log("[" . date('d-m-Y h:i:s') . "] " . $this->loggerName . " : " . strtoupper($level) . ": $message\n", 3, $this->loggerFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user