forked from LiveCarta/PayPal-PHP-SDK
First pass on implementing PSR logger interface
- Created a separate PayPalLogger as default implementation. - Enabled `log.Adapter` as a configuration to pass name of custom logger.
This commit is contained in:
@@ -14,7 +14,8 @@
|
|||||||
"require": {
|
"require": {
|
||||||
"php": ">=5.3.0",
|
"php": ">=5.3.0",
|
||||||
"ext-curl": "*",
|
"ext-curl": "*",
|
||||||
"ext-json": "*"
|
"ext-json": "*",
|
||||||
|
"psr/log": "1.0.0"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"phpunit/phpunit": "3.7.*"
|
"phpunit/phpunit": "3.7.*"
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace PayPal\Core;
|
namespace PayPal\Core;
|
||||||
|
use PayPal\Log\PayPalLogger;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple Logging Manager.
|
* Simple Logging Manager.
|
||||||
@@ -35,12 +37,13 @@ class PayPalLoggingManager
|
|||||||
*/
|
*/
|
||||||
private $loggingLevel;
|
private $loggingLevel;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configured Logging File
|
* The logger to be used for all messages
|
||||||
*
|
*
|
||||||
* @var string
|
* @var LoggerInterface
|
||||||
*/
|
*/
|
||||||
private $loggerFile;
|
private $logger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the singleton object
|
* Returns the singleton object
|
||||||
@@ -71,12 +74,10 @@ class PayPalLoggingManager
|
|||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$config = PayPalConfigManager::getInstance()->getConfigHashmap();
|
$config = PayPalConfigManager::getInstance()->getConfigHashmap();
|
||||||
|
|
||||||
$this->isLoggingEnabled = (array_key_exists('log.LogEnabled', $config) && $config['log.LogEnabled'] == '1');
|
$this->isLoggingEnabled = (array_key_exists('log.LogEnabled', $config) && $config['log.LogEnabled'] == '1');
|
||||||
|
|
||||||
if ($this->isLoggingEnabled) {
|
if ($this->isLoggingEnabled) {
|
||||||
$this->loggerFile = ($config['log.FileName']) ? $config['log.FileName'] : ini_get('error_log');
|
|
||||||
$loggingLevel = strtoupper($config['log.LogLevel']);
|
$loggingLevel = strtoupper($config['log.LogLevel']);
|
||||||
|
$this->setupLogger($config);
|
||||||
$this->loggingLevel =
|
$this->loggingLevel =
|
||||||
(isset($loggingLevel) && defined(__NAMESPACE__ . "\\PayPalLoggingLevel::$loggingLevel")) ?
|
(isset($loggingLevel) && defined(__NAMESPACE__ . "\\PayPalLoggingLevel::$loggingLevel")) ?
|
||||||
constant(__NAMESPACE__ . "\\PayPalLoggingLevel::$loggingLevel") :
|
constant(__NAMESPACE__ . "\\PayPalLoggingLevel::$loggingLevel") :
|
||||||
@@ -84,34 +85,12 @@ class PayPalLoggingManager
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private function setupLogger($config = array()) {
|
||||||
* Default Logger
|
// Checks if custom adapter defined, and is it an implementation of @LoggerInterface
|
||||||
*
|
$loggingAdapter = array_key_exists('log.Adapter', $config) && in_array(LoggerInterface::class, class_implements($config['log.Adapter']))? $config['log.Adapter'] : PayPalLogger::class;
|
||||||
* @param string $message
|
$this->logger = new $loggingAdapter();
|
||||||
* @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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Log Error
|
* Log Error
|
||||||
@@ -120,7 +99,9 @@ class PayPalLoggingManager
|
|||||||
*/
|
*/
|
||||||
public function error($message)
|
public function error($message)
|
||||||
{
|
{
|
||||||
$this->log("ERROR\t: " . $message, PayPalLoggingLevel::ERROR);
|
if ($this->isLoggingEnabled && $this->loggingLevel >= PayPalLoggingLevel::ERROR) {
|
||||||
|
$this->logger->error($message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -130,7 +111,10 @@ class PayPalLoggingManager
|
|||||||
*/
|
*/
|
||||||
public function warning($message)
|
public function warning($message)
|
||||||
{
|
{
|
||||||
$this->log("WARNING\t: " . $message, PayPalLoggingLevel::WARN);
|
if ($this->isLoggingEnabled && $this->loggingLevel >= PayPalLoggingLevel::WARN) {
|
||||||
|
$this->logger->warning($message);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -140,7 +124,9 @@ class PayPalLoggingManager
|
|||||||
*/
|
*/
|
||||||
public function info($message)
|
public function info($message)
|
||||||
{
|
{
|
||||||
$this->log("INFO\t: " . $message, PayPalLoggingLevel::INFO);
|
if ($this->isLoggingEnabled && $this->loggingLevel >= PayPalLoggingLevel::INFO) {
|
||||||
|
$this->logger->info($message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -150,7 +136,7 @@ class PayPalLoggingManager
|
|||||||
*/
|
*/
|
||||||
public function fine($message)
|
public function fine($message)
|
||||||
{
|
{
|
||||||
$this->log("FINE\t: " . $message, PayPalLoggingLevel::FINE);
|
$this->info($message);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -160,7 +146,15 @@ class PayPalLoggingManager
|
|||||||
*/
|
*/
|
||||||
public function debug($message)
|
public function debug($message)
|
||||||
{
|
{
|
||||||
$this->log("DEBUG\t: " . $message, PayPalLoggingLevel::DEBUG);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
92
lib/PayPal/Log/PayPalLogger.php
Normal file
92
lib/PayPal/Log/PayPalLogger.php
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace PayPal\Log;
|
||||||
|
|
||||||
|
use PayPal\Core\PayPalConfigManager;
|
||||||
|
use PayPal\Core\PayPalLoggingManager;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
|
||||||
|
|
||||||
|
class PayPalLogger implements LoggerInterface {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default Logging Level
|
||||||
|
*/
|
||||||
|
const DEFAULT_LOGGING_LEVEL = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configured Logging Level
|
||||||
|
*
|
||||||
|
* @var int|mixed
|
||||||
|
*/
|
||||||
|
private $loggingLevel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configured Logging File
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $loggerFile;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -90,6 +90,7 @@ function getApiContext($clientId, $clientSecret)
|
|||||||
'cache.enabled' => true,
|
'cache.enabled' => true,
|
||||||
// 'http.CURLOPT_CONNECTTIMEOUT' => 30
|
// 'http.CURLOPT_CONNECTTIMEOUT' => 30
|
||||||
// 'http.headers.PayPal-Partner-Attribution-Id' => '123123123'
|
// 'http.headers.PayPal-Partner-Attribution-Id' => '123123123'
|
||||||
|
// 'log.Adapter' => '\Your\Custom\Logger' // Class name of the logger to be used. Must be implementing Psr\Log\LoggerInterface
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user