| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) die('No direct access allowed'); |
| 4 |
|
| 5 |
require_once('class-updraft-logger-interface.php'); |
| 6 |
require_once('class-updraft-log-levels.php'); |
| 7 |
require_once('class-updraft-abstract-logger.php'); |
| 8 |
require_once('class-updraft-logger.php'); |
| 9 |
|
| 10 |
if (class_exists('Updraft_Logger')) return; |
| 11 |
|
| 12 |
/** |
| 13 |
* Class Updraft_Logger |
| 14 |
*/ |
| 15 |
class Updraft_Logger implements Updraft_Logger_Interface { |
| 16 |
|
| 17 |
/** |
| 18 |
* Array of loggers |
| 19 |
* |
| 20 |
* @var Updraft_Logger_Interface[] |
| 21 |
*/ |
| 22 |
protected $_loggers = array(); |
| 23 |
|
| 24 |
/** |
| 25 |
* Constructor method |
| 26 |
* |
| 27 |
* @param Updraft_Logger_Interface|null $logger |
| 28 |
*/ |
| 29 |
public function __construct($logger = null) { |
| 30 |
if (!empty($logger)) $this->_loggers = array($logger); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Returns singleton instance object |
| 35 |
* |
| 36 |
* @return Updraft_Logger Returns `Updraft_Logger` object |
| 37 |
*/ |
| 38 |
public static function instance() { |
| 39 |
static $_instance = null; |
| 40 |
if (null === $_instance) { |
| 41 |
$_instance = new self(); |
| 42 |
} |
| 43 |
return $_instance; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Add logger to loggers list |
| 48 |
* |
| 49 |
* @param Updraft_Logger_Interface $logger |
| 50 |
* @return false|void |
| 51 |
*/ |
| 52 |
public function add_logger($logger) { |
| 53 |
$logger_id = $logger_class = get_class($logger); |
| 54 |
|
| 55 |
// don't add logger if it doesn't support multiple loggers. |
| 56 |
if (!empty($this->_loggers) && array_key_exists($logger_id, $this->_loggers) && false === $logger->is_allow_multiple()) return false; |
| 57 |
|
| 58 |
$index = 0; |
| 59 |
|
| 60 |
// get free id key. |
| 61 |
while (array_key_exists($logger_id, $this->_loggers)) { |
| 62 |
$index++; |
| 63 |
$logger_id = $logger_class.'_'.$index; |
| 64 |
} |
| 65 |
|
| 66 |
$this->_loggers[$logger_id] = $logger; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Return list of loggers |
| 71 |
* |
| 72 |
* @return array |
| 73 |
*/ |
| 74 |
public function get_loggers() { |
| 75 |
return $this->_loggers; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* System is unusable. |
| 80 |
* |
| 81 |
* @param string $message |
| 82 |
* @param array $context |
| 83 |
* @return void |
| 84 |
*/ |
| 85 |
public function emergency($message, $context = array()) { |
| 86 |
|
| 87 |
if (empty($this->_loggers)) return; |
| 88 |
|
| 89 |
foreach ($this->_loggers as $logger) { |
| 90 |
$logger->emergency($message, $context); |
| 91 |
} |
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Action must be taken immediately. |
| 97 |
* |
| 98 |
* Example: Entire website down, database unavailable, etc. This should |
| 99 |
* trigger the SMS alerts and wake you up. |
| 100 |
* |
| 101 |
* @param string $message |
| 102 |
* @param array $context |
| 103 |
* @return void |
| 104 |
*/ |
| 105 |
public function alert($message, $context = array()) { |
| 106 |
|
| 107 |
if (empty($this->_loggers)) return; |
| 108 |
|
| 109 |
foreach ($this->_loggers as $logger) { |
| 110 |
$logger->alert($message, $context); |
| 111 |
} |
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Critical conditions. |
| 117 |
* |
| 118 |
* Example: Application component unavailable, unexpected exception. |
| 119 |
* |
| 120 |
* @param string $message |
| 121 |
* @param array $context |
| 122 |
* @return void |
| 123 |
*/ |
| 124 |
public function critical($message, $context = array()) { |
| 125 |
|
| 126 |
if (empty($this->_loggers)) return; |
| 127 |
|
| 128 |
foreach ($this->_loggers as $logger) { |
| 129 |
$logger->critical($message, $context); |
| 130 |
} |
| 131 |
|
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Runtime errors that do not require immediate action but should typically |
| 136 |
* be logged and monitored. |
| 137 |
* |
| 138 |
* @param string $message |
| 139 |
* @param array $context |
| 140 |
* @return void |
| 141 |
*/ |
| 142 |
public function error($message, $context = array()) { |
| 143 |
|
| 144 |
if (empty($this->_loggers)) return; |
| 145 |
|
| 146 |
foreach ($this->_loggers as $logger) { |
| 147 |
$logger->error($message, $context); |
| 148 |
} |
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Exceptional occurrences that are not errors. |
| 154 |
* |
| 155 |
* Example: Use of deprecated APIs, poor use of an API, undesirable things |
| 156 |
* that are not necessarily wrong. |
| 157 |
* |
| 158 |
* @param string $message |
| 159 |
* @param array $context |
| 160 |
* @return void |
| 161 |
*/ |
| 162 |
public function warning($message, $context = array()) { |
| 163 |
|
| 164 |
if (empty($this->_loggers)) return; |
| 165 |
|
| 166 |
foreach ($this->_loggers as $logger) { |
| 167 |
$logger->warning($message, $context); |
| 168 |
} |
| 169 |
|
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Normal but significant events. |
| 174 |
* |
| 175 |
* @param string $message |
| 176 |
* @param array $context |
| 177 |
* @return void |
| 178 |
*/ |
| 179 |
public function notice($message, $context = array()) { |
| 180 |
|
| 181 |
if (empty($this->_loggers)) return; |
| 182 |
|
| 183 |
foreach ($this->_loggers as $logger) { |
| 184 |
$logger->notice($message, $context); |
| 185 |
} |
| 186 |
|
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Interesting events. |
| 191 |
* |
| 192 |
* Example: User logs in, SQL logs. |
| 193 |
* |
| 194 |
* @param string $message |
| 195 |
* @param array $context |
| 196 |
* @return void |
| 197 |
*/ |
| 198 |
public function info($message, $context = array()) { |
| 199 |
|
| 200 |
if (empty($this->_loggers)) return; |
| 201 |
|
| 202 |
foreach ($this->_loggers as $logger) { |
| 203 |
$logger->info($message, $context); |
| 204 |
} |
| 205 |
|
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Detailed debug information. |
| 210 |
* |
| 211 |
* @param string $message |
| 212 |
* @param array $context |
| 213 |
* @return void |
| 214 |
*/ |
| 215 |
public function debug($message, $context = array()) { |
| 216 |
|
| 217 |
if (empty($this->_loggers)) return; |
| 218 |
|
| 219 |
foreach ($this->_loggers as &$logger) { |
| 220 |
$logger->debug($message, $context); |
| 221 |
} |
| 222 |
|
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Logs with an arbitrary level. |
| 227 |
* |
| 228 |
* @param string $message |
| 229 |
* @param mixed $level |
| 230 |
* @param array $context |
| 231 |
* @return void |
| 232 |
*/ |
| 233 |
public function log($message, $level, $context = array()) { |
| 234 |
|
| 235 |
if (empty($this->_loggers)) return; |
| 236 |
|
| 237 |
foreach ($this->_loggers as $logger) { |
| 238 |
$logger->log($message, $level, $context); |
| 239 |
} |
| 240 |
|
| 241 |
} |
| 242 |
} |
| 243 |
|