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