PluginProbe
Document Gallery / 2.2.6
Document Gallery v2.2.6
trunk 0.8 0.8.5 1.0 1.0.1 1.0.2 1.0.3 1.0.4 1.1 1.2 1.2.1 1.3 1.3.1 1.4 1.4.1 1.4.2 1.4.3 2.0 2.0.1 2.0.10 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 94 releases
document-gallery / inc / class-logger.php

class-logger.php in Document Gallery 2.2.6, at inc/class-logger.php

228 lines 6.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 defined('WPINC') OR exit;
3
4 /**
5 * Encapsulates the logic required to maintain and read log files.
6 */
7 class DG_Logger {
8 /**
9 * Appends DG log file if logging is enabled. The following format is used for each line:
10 * datetime | level | entry | stacktrace (optional)
11 *
12 * @param int The level of serverity (should be passed using DG_LogLevel consts).
13 * @param string $entry Value to be logged.
14 * @param bool $stacktrace Whether to include full stack trace.
15 * @param bool $force Whether to ignore logging flag and log no matter what.
16 */
17 public static function writeLog($level, $entry, $stacktrace = false, $force = false) {
18 if ($force || self::logEnabled()) {
19 $fp = fopen(self::getLogFileName(), 'a');
20 if (false !== $fp) {
21 $fields = array(time(), $level, $entry);
22
23 $trace = debug_backtrace(false);
24 if ($stacktrace) {
25 unset($trace[0]);
26
27 $trace_str = '';
28 $i = 1;
29
30 foreach($trace as $node) {
31 $trace_str .= "#$i ";
32
33 $file = '';
34 if (isset($node['file'])) {
35 // convert to relative path from WP root
36 $file = str_replace(ABSPATH, '', $node['file']);
37 }
38
39 if (isset($node['line'])) {
40 $file .= "({$node['line']})";
41 }
42
43 if ($file) {
44 $trace_str .= "$file: ";
45 }
46
47 if(isset($node['class'])) {
48 $trace_str .= "{$node['class']}{$node['type']}";
49 }
50
51 if (isset($node['function'])) {
52 $args = '';
53 if (isset($node['args'])) {
54 $args = implode(', ', array_map(array(__CLASS__, 'print_r'), $node['args']));
55 }
56
57 $trace_str .= "{$node['function']}($args)" . PHP_EOL;
58 }
59 $i++;
60 }
61
62 $fields[] = $trace_str;
63 } else {
64 // Remove first item from backtrace as it's this function which is redundant.
65 $caller = $trace[1];
66 $caller = (isset($caller['class']) ? $caller['class'] : '') . $caller['type'] . $caller['function'];
67 $fields[2] = '(' . $caller . ') ' . $fields[2];
68 }
69
70 fputcsv($fp, $fields);
71 fclose($fp);
72 } // TODO: else
73 }
74 }
75
76 /**
77 * Reads the current blog's log file, placing the values in to a 2-dimensional array.
78 * @param int $skip How many lines to skip before returning rows.
79 * @param int $limit Max number of lines to read.
80 * @return multitype:multitype:string|null The rows from the log file or null if failed to open log.
81 */
82 public static function readLog($skip = 0, $limit = PHP_INT_MAX) {
83 $ret = null;
84 $fp = @fopen(self::getLogFileName(), 'r');
85
86 if ($fp !== false) {
87 $ret = array();
88 while (count($ret) < $limit && ($fields = fgetcsv($fp)) !== false) {
89 if ($skip > 0) {
90 $skip--;
91 continue;
92 }
93
94 if (!is_null($fields)) {
95 $ret[] = $fields;
96 }
97 }
98 }
99
100 return $ret;
101 }
102
103 /**
104 * Clears the log file for the active blog.
105 */
106 public static function clearLog() {
107 // we don't care if the file actually exists -- it won't when we're done
108 @unlink(self::getLogFileName());
109 }
110
111 /**
112 * @return bool Whether debug logging is currently enabled.
113 */
114 public static function logEnabled() {
115 global $dg_options;
116 return $dg_options['logging'];
117 }
118
119 /**
120 * @return string Full path to log file for current blog.
121 */
122 private static function getLogFileName() {
123 return DG_PATH . 'log/' . get_current_blog_id() . '.log';
124 }
125
126 /**
127 * Wraps print_r passing true for the return argument.
128 * @param unknown $v Value to be printed.
129 * @return string Printed value.
130 */
131 private static function print_r($v) {
132 return print_r($v, true);
133 }
134 }
135
136 /**
137 * LogLevel acts as an enumeration of all possible log levels.
138 */
139 class DG_LogLevel {
140 /**
141 * @var int Log level for anything that doesn't indicate a problem.
142 */
143 const Detail = 0;
144
145 /**
146 * @var int Log level for anything that is a minor issue.
147 */
148 const Warning = 1;
149
150 /**
151 * @var int Log level for when something went wrong.
152 */
153 const Error = 2;
154
155 /**
156 * @var ReflectionClass Backs the getter.
157 */
158 private static $ref = null;
159
160 /**
161 * @return ReflectionClass Instance of reflection class for this class.
162 */
163 private static function getReflectionClass() {
164 if (is_null(self::$ref)) {
165 self::$ref = new ReflectionClass(__CLASS__);
166 }
167
168 return self::$ref;
169 }
170
171 /**
172 * @var multitype Backs the getter.
173 */
174 private static $levels = null;
175
176 /**
177 * @return multitype Associative array containing all log level names mapped to their int value.
178 */
179 public static function getLogLevels() {
180 if (is_null(self::$levels)) {
181 $ref = self::getReflectionClass();
182 self::$levels = $ref->getConstants();
183 }
184
185 return self::$levels;
186 }
187
188 /**
189 * @param string $name Name to be checked for validity.
190 * @return bool Whether given name represents valid log level.
191 */
192 public static function isValidName($name) {
193 return array_key_exists($name, self::getLogLevels());
194 }
195
196 /**
197 * @param int $value Value to be checked for validity.
198 * @return bool Whether given value represents valid log level.
199 */
200 public static function isValidValue($value) {
201 return (false !== array_search($value, self::getLogLevels()));
202 }
203
204 /**
205 * @param string $name The name for which to retrieve a value.
206 * @return int|null The value associated with the given name.
207 */
208 public static function getValueByName($name) {
209 $levels = self::getLogLevels();
210 return array_key_exists($name, self::getLogLevels()) ? $levels[$name] : null;
211 }
212
213 /**
214 * @param int $value The value for which to retrieve a name.
215 * @return string|null The name associated with the given value.
216 */
217 public static function getNameByValue($value) {
218 $ret = array_search($value, self::getLogLevels());
219 return (false !== $ret) ? $ret : null;
220 }
221
222 /**
223 * Blocks instantiation. All functions are static.
224 */
225 private function __construct() {
226
227 }
228 }