Enabled Factory to Inject Logger

This commit is contained in:
Jay Patel
2016-04-11 15:45:49 -05:00
parent 557fb3718f
commit c0c307f267
8 changed files with 143 additions and 176 deletions

View 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);
}
}

View 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);
}

View File

@@ -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);
}
}
}
}