cc817e3eb53183c0de400595e0bd0c19945b55e3
[WP-Plugins/glm-member-db-registrations.git] /
1 <?php
2 namespace PayPal\Core;
3 /**
4  * Simple Logging Manager.
5  * This does an error_log for now
6  * Potential frameworks to use are PEAR logger, log4php from Apache
7  */
8
9 class PPLoggingLevel {
10
11         // FINE Logging Level
12         const FINE = 3;
13
14         // INFO Logging Level
15         const INFO = 2;
16
17         // WARN Logging Level
18         const WARN = 1;
19
20         // ERROR Logging Level
21         const ERROR = 0;
22 }
23
24 class PPLoggingManager {
25
26         // Default Logging Level
27         const DEFAULT_LOGGING_LEVEL = 0;
28
29         // Logger name
30         private $loggerName;
31
32         // Log enabled
33         private $isLoggingEnabled;
34
35         // Configured logging level
36         private $loggingLevel;
37
38         // Configured logging file
39         private $loggerFile;
40
41         public function __construct($loggerName, $config = null) {
42                 $this->loggerName = $loggerName;
43                 if($config == null) {
44                         $config = PPConfigManager::getInstance()->getConfigHashmap();
45                 }               
46                 $this->isLoggingEnabled = (array_key_exists('log.LogEnabled', $config) && $config['log.LogEnabled'] == '1');            
47                  
48                 if($this->isLoggingEnabled) {
49                         $this->loggerFile = ($config['log.FileName']) ? $config['log.FileName'] : ini_get('error_log');
50                         $loggingLevel = strtoupper($config['log.LogLevel']);
51                         $this->loggingLevel = (isset($loggingLevel) && defined(__NAMESPACE__."\\PPLoggingLevel::$loggingLevel")) ? constant(__NAMESPACE__."\\PPLoggingLevel::$loggingLevel") : PPLoggingManager::DEFAULT_LOGGING_LEVEL;
52                 }
53         }
54
55         private function log($message, $level=PPLoggingLevel::INFO) {
56                 if($this->isLoggingEnabled && ($level <= $this->loggingLevel)) {
57                         error_log( $this->loggerName . ": $message\n", 3, $this->loggerFile);
58                 }
59         }
60
61         public function error($message) {
62                 $this->log($message, PPLoggingLevel::ERROR);
63         }
64
65         public function warning($message) {
66                 $this->log($message, PPLoggingLevel::WARN);
67         }
68
69         public function info($message) {
70                 $this->log($message, PPLoggingLevel::INFO);
71         }
72
73         public function fine($message) {
74                 $this->log($message, PPLoggingLevel::FINE);
75         }
76
77 }