forked from LiveCarta/PayPal-PHP-SDK
Merge pull request #543 from paypal/logger-interface
First pass on implementing PSR logger interface
This commit is contained in:
@@ -14,7 +14,8 @@
|
||||
"require": {
|
||||
"php": ">=5.3.0",
|
||||
"ext-curl": "*",
|
||||
"ext-json": "*"
|
||||
"ext-json": "*",
|
||||
"psr/log": "1.0.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "3.7.*"
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -2,6 +2,9 @@
|
||||
|
||||
namespace PayPal\Core;
|
||||
|
||||
use PayPal\Log\PayPalLogFactory;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* Simple Logging Manager.
|
||||
* This does an error_log for now
|
||||
@@ -9,39 +12,25 @@ namespace PayPal\Core;
|
||||
*/
|
||||
class PayPalLoggingManager
|
||||
{
|
||||
/**
|
||||
* @var array of logging manager instances with class name as key
|
||||
*/
|
||||
private static $instances = array();
|
||||
|
||||
/**
|
||||
* Default Logging Level
|
||||
* The logger to be used for all messages
|
||||
*
|
||||
* @var LoggerInterface
|
||||
*/
|
||||
const DEFAULT_LOGGING_LEVEL = 0;
|
||||
private $logger;
|
||||
|
||||
/**
|
||||
* Logger Name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $loggerName;
|
||||
|
||||
/**
|
||||
* Log Enabled
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $isLoggingEnabled;
|
||||
|
||||
/**
|
||||
* Configured Logging Level
|
||||
*
|
||||
* @var int|mixed
|
||||
*/
|
||||
private $loggingLevel;
|
||||
|
||||
/**
|
||||
* Configured Logging File
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $loggerFile;
|
||||
|
||||
/**
|
||||
* Returns the singleton object
|
||||
*
|
||||
@@ -50,66 +39,29 @@ 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) {
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Default Logger
|
||||
*
|
||||
* @param string $message
|
||||
* @param int $level
|
||||
*/
|
||||
private function log($message, $level = PayPalLoggingLevel::INFO)
|
||||
{
|
||||
if ($this->isLoggingEnabled) {
|
||||
$config = PayPalConfigManager::getInstance()->getConfigHashmap();
|
||||
// Check if logging in live
|
||||
if (array_key_exists('mode', $config) && $config['mode'] == 'live') {
|
||||
// Live should not have logging level above INFO.
|
||||
if ($this->loggingLevel >= PayPalLoggingLevel::INFO) {
|
||||
// If it is at Debug Level, throw an warning in the log.
|
||||
if ($this->loggingLevel == PayPalLoggingLevel::DEBUG) {
|
||||
error_log("[" . date('d-m-Y h:i:s') . "] " . $this->loggerName . ": ERROR\t: Not allowed to keep 'Debug' level for Live Environments. Reduced to 'INFO'\n", 3, $this->loggerFile);
|
||||
}
|
||||
// Reducing it to info level
|
||||
$this->loggingLevel = PayPalLoggingLevel::INFO;
|
||||
}
|
||||
}
|
||||
|
||||
if ($level <= $this->loggingLevel) {
|
||||
error_log("[" . date('d-m-Y h:i:s') . "] " . $this->loggerName . ": $message\n", 3, $this->loggerFile);
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,7 +72,7 @@ class PayPalLoggingManager
|
||||
*/
|
||||
public function error($message)
|
||||
{
|
||||
$this->log("ERROR\t: " . $message, PayPalLoggingLevel::ERROR);
|
||||
$this->logger->error($message);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -130,7 +82,7 @@ class PayPalLoggingManager
|
||||
*/
|
||||
public function warning($message)
|
||||
{
|
||||
$this->log("WARNING\t: " . $message, PayPalLoggingLevel::WARN);
|
||||
$this->logger->warning($message);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -140,7 +92,7 @@ class PayPalLoggingManager
|
||||
*/
|
||||
public function info($message)
|
||||
{
|
||||
$this->log("INFO\t: " . $message, PayPalLoggingLevel::INFO);
|
||||
$this->logger->info($message);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -150,17 +102,21 @@ class PayPalLoggingManager
|
||||
*/
|
||||
public function fine($message)
|
||||
{
|
||||
$this->log("FINE\t: " . $message, PayPalLoggingLevel::FINE);
|
||||
$this->info($message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Log Fine
|
||||
* Log Debug
|
||||
*
|
||||
* @param string $message
|
||||
*/
|
||||
public function debug($message)
|
||||
{
|
||||
$this->log("DEBUG\t: " . $message, PayPalLoggingLevel::DEBUG);
|
||||
$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);
|
||||
|
||||
}
|
||||
85
lib/PayPal/Log/PayPalLogger.php
Normal file
85
lib/PayPal/Log/PayPalLogger.php
Normal file
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace PayPal\Log;
|
||||
|
||||
use PayPal\Core\PayPalConfigManager;
|
||||
use Psr\Log\AbstractLogger;
|
||||
use Psr\Log\LogLevel;
|
||||
|
||||
|
||||
class PayPalLogger extends AbstractLogger
|
||||
{
|
||||
|
||||
/**
|
||||
* @var array Indexed list of all log levels.
|
||||
*/
|
||||
private $loggingLevels = array(
|
||||
LogLevel::EMERGENCY,
|
||||
LogLevel::ALERT,
|
||||
LogLevel::CRITICAL,
|
||||
LogLevel::ERROR,
|
||||
LogLevel::WARNING,
|
||||
LogLevel::NOTICE,
|
||||
LogLevel::INFO,
|
||||
LogLevel::DEBUG
|
||||
);
|
||||
|
||||
/**
|
||||
* Configured Logging Level
|
||||
*
|
||||
* @var LogLevel $loggingLevel
|
||||
*/
|
||||
private $loggingLevel;
|
||||
|
||||
/**
|
||||
* Configured Logging File
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $loggerFile;
|
||||
|
||||
/**
|
||||
* 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->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 log($level, $message, array $context = array())
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -86,10 +86,11 @@ function getApiContext($clientId, $clientSecret)
|
||||
'mode' => 'sandbox',
|
||||
'log.LogEnabled' => true,
|
||||
'log.FileName' => '../PayPal.log',
|
||||
'log.LogLevel' => 'DEBUG', // PLEASE USE `FINE` LEVEL FOR LOGGING IN LIVE ENVIRONMENTS
|
||||
'log.LogLevel' => 'DEBUG', // PLEASE USE `INFO` LEVEL FOR LOGGING IN LIVE ENVIRONMENTS
|
||||
'cache.enabled' => true,
|
||||
// 'http.CURLOPT_CONNECTTIMEOUT' => 30
|
||||
// 'http.headers.PayPal-Partner-Attribution-Id' => '123123123'
|
||||
//'log.AdapterFactory' => '\PayPal\Log\DefaultLogFactory' // Factory class implementing \PayPal\Log\PayPalLogFactory
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
@@ -25,22 +25,27 @@ mode = sandbox
|
||||
|
||||
;Logging Information
|
||||
[Log]
|
||||
; For custom logging implementation, you can set the
|
||||
; logging factory provider class here.
|
||||
; The class should be implementing \PayPal\Log\PayPalLogFactory.
|
||||
; If this is not set, it will default to \PayPal\Log\PayPalDefaultLogFactory.
|
||||
;log.AdapterFactory=\PayPal\Log\PayPalDefaultLogFactory
|
||||
|
||||
; Settings for PayPalDefaultLogFactory
|
||||
log.LogEnabled=true
|
||||
|
||||
; When using a relative path, the log file is created
|
||||
; relative to the .php file that is the entry point
|
||||
; for this request. You can also provide an absolute
|
||||
; path here
|
||||
; Settings for PayPalDefaultLogFactory
|
||||
log.FileName=../PayPal.log
|
||||
|
||||
; Logging level can be one of
|
||||
; Sandbox Environments: DEBUG, INFO, WARN, ERROR
|
||||
; Live Environments: INFO, WARN, ERROR
|
||||
; Logging level can be one of any provided at \Psr\Log\LogLevel
|
||||
; Logging is most verbose in the 'DEBUG' level and
|
||||
; decreases as you proceed towards ERROR
|
||||
; DEBUG level is disabled for live, to not log sensitive information.
|
||||
; If the level is set to DEBUG, it will be reduced to FINE automatically,
|
||||
; with a warning message
|
||||
; If the level is set to DEBUG, it will be reduced to INFO automatically
|
||||
log.LogLevel=INFO
|
||||
|
||||
;Caching Configuration
|
||||
|
||||
Reference in New Issue
Block a user