| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) die('No direct access allowed'); |
| 4 |
|
| 5 |
if (class_exists('Updraft_Ring_Logger')) return; |
| 6 |
|
| 7 |
/** |
| 8 |
* Class Updraft_Ring_Logger |
| 9 |
*/ |
| 10 |
class Updraft_Ring_Logger extends Updraft_Abstract_Logger { |
| 11 |
|
| 12 |
/** |
| 13 |
* Updraft_Ring_Logger constructor |
| 14 |
*/ |
| 15 |
public function __construct() { |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Returns logger description |
| 20 |
* |
| 21 |
* @return string|void |
| 22 |
*/ |
| 23 |
public function get_description() { |
| 24 |
return __('Store the most recent log entries in the WordPress database', 'wp-optimize'); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Returns list of logger options. |
| 29 |
* |
| 30 |
* @return array |
| 31 |
*/ |
| 32 |
public function get_options_list() { |
| 33 |
return array( |
| 34 |
'ring_logger_limit' => __('How many last records store?', 'wp-optimize') |
| 35 |
); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Emergency message |
| 40 |
* |
| 41 |
* @param string $message |
| 42 |
* @param array $context |
| 43 |
* @return null|void |
| 44 |
*/ |
| 45 |
public function emergency($message, array $context = array()) { |
| 46 |
$this->log(Updraft_Log_Levels::EMERGENCY, $message, $context); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Alert message |
| 51 |
* |
| 52 |
* @param string $message |
| 53 |
* @param array $context |
| 54 |
* @return null|void |
| 55 |
*/ |
| 56 |
public function alert($message, array $context = array()) { |
| 57 |
$this->log(Updraft_Log_Levels::ALERT, $message, $context); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Critical message |
| 62 |
* |
| 63 |
* @param string $message |
| 64 |
* @param array $context |
| 65 |
* @return null|void |
| 66 |
*/ |
| 67 |
public function critical($message, array $context = array()) { |
| 68 |
$this->log(Updraft_Log_Levels::CRITICAL, $message, $context); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Error message |
| 73 |
* |
| 74 |
* @param string $message |
| 75 |
* @param array $context |
| 76 |
* @return null|void |
| 77 |
*/ |
| 78 |
public function error($message, array $context = array()) { |
| 79 |
$this->log(Updraft_Log_Levels::ERROR, $message, $context); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Warning message |
| 84 |
* |
| 85 |
* @param string $message |
| 86 |
* @param array $context |
| 87 |
* @return null|void |
| 88 |
*/ |
| 89 |
public function warning($message, array $context = array()) { |
| 90 |
$this->log(Updraft_Log_Levels::WARNING, $message, $context); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Notice message |
| 95 |
* |
| 96 |
* @param string $message |
| 97 |
* @param array $context |
| 98 |
* @return null|void |
| 99 |
*/ |
| 100 |
public function notice($message, array $context = array()) { |
| 101 |
$this->log(Updraft_Log_Levels::NOTICE, $message, $context); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Info message |
| 106 |
* |
| 107 |
* @param string $message |
| 108 |
* @param array $context |
| 109 |
* @return null|void |
| 110 |
*/ |
| 111 |
public function info($message, array $context = array()) { |
| 112 |
$this->log(Updraft_Log_Levels::INFO, $message, $context); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Debug message |
| 117 |
* |
| 118 |
* @param string $message |
| 119 |
* @param array $context |
| 120 |
* @return null|void |
| 121 |
*/ |
| 122 |
public function debug($message, array $context = array()) { |
| 123 |
$this->log(Updraft_Log_Levels::DEBUG, $message, $context); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Log message with any level |
| 128 |
* |
| 129 |
* @param string $level |
| 130 |
* @param string $message |
| 131 |
* @param array $context |
| 132 |
* @return null|void |
| 133 |
*/ |
| 134 |
public function log($level, $message, array $context = array()) { |
| 135 |
|
| 136 |
if (!$this->is_enabled()) return false; |
| 137 |
|
| 138 |
$message = date("Y-m-d H:i:s").' ['.Updraft_Log_Levels::to_text($level).'] : '.$this->interpolate($message, $context); |
| 139 |
$this->add_log($message); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Add message to log |
| 144 |
* |
| 145 |
* @param string $message Message to be added to log. |
| 146 |
*/ |
| 147 |
public function add_log($message) { |
| 148 |
$log_option_name = $this->get_logger_option_name(); |
| 149 |
$log_limit = $this->get_logger_limit(); |
| 150 |
$log = $this->get_log(); |
| 151 |
$log[] = $message; |
| 152 |
while (count($log) > 0 && count($log) > $log_limit) { |
| 153 |
array_shift($log); |
| 154 |
} |
| 155 |
update_option($log_option_name, $log); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Return logger option name value |
| 160 |
* |
| 161 |
* @return string |
| 162 |
*/ |
| 163 |
public function get_logger_option_name() { |
| 164 |
return 'updraft_ring_log'; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Return logger limit value |
| 169 |
* |
| 170 |
* @return string |
| 171 |
*/ |
| 172 |
public function get_logger_limit() { |
| 173 |
return $this->get_option('ring_logger_limit', 20); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Set logger wordpress option name where log will stored |
| 178 |
* |
| 179 |
* @param string $option_name Name for logger option. |
| 180 |
*/ |
| 181 |
public function set_logger_option_name($option_name) { |
| 182 |
$this->set_option('ring_logger_option_name', $option_name); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Return log content |
| 187 |
* |
| 188 |
* @return mixed|void |
| 189 |
*/ |
| 190 |
public function get_log() { |
| 191 |
return get_option($this->get_logger_option_name(), array()); |
| 192 |
} |
| 193 |
} |
| 194 |
|