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:
@@ -1,6 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace PayPal\Core;
|
||||
use PayPal\Log\PayPalLogger;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* Simple Logging Manager.
|
||||
@@ -35,12 +37,13 @@ class PayPalLoggingManager
|
||||
*/
|
||||
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
|
||||
@@ -71,48 +74,24 @@ class PayPalLoggingManager
|
||||
public function __construct()
|
||||
{
|
||||
$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->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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user