1 <?php
2
3 namespace PayPal\Log;
4
5 use PayPal\Core\PayPalConfigManager;
6 use Psr\Log\AbstractLogger;
7 use Psr\Log\LogLevel;
8
9 class PayPalLogger extends AbstractLogger
10 {
11
12 13 14
15 private $loggingLevels = array(
16 LogLevel::EMERGENCY,
17 LogLevel::ALERT,
18 LogLevel::CRITICAL,
19 LogLevel::ERROR,
20 LogLevel::WARNING,
21 LogLevel::NOTICE,
22 LogLevel::INFO,
23 LogLevel::DEBUG
24 );
25
26 27 28 29 30
31 private $loggingLevel;
32
33 34 35 36 37
38 private $loggerFile;
39
40 41 42 43 44
45 private $isLoggingEnabled;
46
47 48 49 50 51
52 private $loggerName;
53
54 public function __construct($className)
55 {
56 $this->loggerName = $className;
57 $this->initialize();
58 }
59
60 public function initialize()
61 {
62 $config = PayPalConfigManager::getInstance()->getConfigHashmap();
63 if (!empty($config)) {
64 $this->isLoggingEnabled = (array_key_exists('log.LogEnabled', $config) && $config['log.LogEnabled'] == '1');
65 if ($this->isLoggingEnabled) {
66 $this->loggerFile = ($config['log.FileName']) ? $config['log.FileName'] : ini_get('error_log');
67 $loggingLevel = strtoupper($config['log.LogLevel']);
68 $this->loggingLevel = (isset($loggingLevel) && defined("\\Psr\\Log\\LogLevel::$loggingLevel")) ?
69 constant("\\Psr\\Log\\LogLevel::$loggingLevel") :
70 LogLevel::INFO;
71 }
72 }
73 }
74
75 public function log($level, $message, array $context = array())
76 {
77 if ($this->isLoggingEnabled) {
78
79 if (array_search($level, $this->loggingLevels) <= array_search($this->loggingLevel, $this->loggingLevels)) {
80 error_log("[" . date('d-m-Y H:i:s') . "] " . $this->loggerName . " : " . strtoupper($level) . ": $message\n", 3, $this->loggerFile);
81 }
82 }
83 }
84 }
85