From 69886a0741a2025719a9306490c61f76325f4540 Mon Sep 17 00:00:00 2001 From: Jay Patel Date: Wed, 6 Apr 2016 14:10:54 -0500 Subject: [PATCH] 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. --- composer.json | 3 +- lib/PayPal/Core/PayPalLoggingManager.php | 74 +++++++++---------- lib/PayPal/Log/PayPalLogger.php | 92 ++++++++++++++++++++++++ sample/bootstrap.php | 1 + 4 files changed, 129 insertions(+), 41 deletions(-) create mode 100644 lib/PayPal/Log/PayPalLogger.php diff --git a/composer.json b/composer.json index 878f4d0..19fa87a 100644 --- a/composer.json +++ b/composer.json @@ -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.*" diff --git a/lib/PayPal/Core/PayPalLoggingManager.php b/lib/PayPal/Core/PayPalLoggingManager.php index 6e2d9a4..c9febea 100644 --- a/lib/PayPal/Core/PayPalLoggingManager.php +++ b/lib/PayPal/Core/PayPalLoggingManager.php @@ -1,6 +1,8 @@ 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->setupLogger($config); $this->loggingLevel = (isset($loggingLevel) && defined(__NAMESPACE__ . "\\PayPalLoggingLevel::$loggingLevel")) ? - constant(__NAMESPACE__ . "\\PayPalLoggingLevel::$loggingLevel") : - PayPalLoggingManager::DEFAULT_LOGGING_LEVEL; + 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); - } - } + 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(LoggerInterface::class, class_implements($config['log.Adapter']))? $config['log.Adapter'] : PayPalLogger::class; + $this->logger = new $loggingAdapter(); } + /** * Log Error * @@ -120,7 +99,9 @@ class PayPalLoggingManager */ 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) { - $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) { - $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) { - $this->log("FINE\t: " . $message, PayPalLoggingLevel::FINE); + $this->info($message); } /** @@ -160,7 +146,15 @@ class PayPalLoggingManager */ 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); + } + } } } diff --git a/lib/PayPal/Log/PayPalLogger.php b/lib/PayPal/Log/PayPalLogger.php new file mode 100644 index 0000000..723d06c --- /dev/null +++ b/lib/PayPal/Log/PayPalLogger.php @@ -0,0 +1,92 @@ +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); + } +} \ No newline at end of file diff --git a/sample/bootstrap.php b/sample/bootstrap.php index bfdda9b..c85fb47 100644 --- a/sample/bootstrap.php +++ b/sample/bootstrap.php @@ -90,6 +90,7 @@ function getApiContext($clientId, $clientSecret) 'cache.enabled' => true, // 'http.CURLOPT_CONNECTTIMEOUT' => 30 // '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 ) );