1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40:
41: class cLog {
42: const EMERG = 0;
43: const ALERT = 1;
44: const CRIT = 2;
45: const ERR = 3;
46: const WARN = 4;
47: const NOTICE = 5;
48: const INFO = 6;
49: const DEBUG = 7;
50:
51: 52: 53:
54: protected $_writer;
55:
56: 57: 58:
59: protected $_shortcutHandlers = array();
60:
61: 62: 63:
64: protected $_priorities = array();
65:
66: 67: 68:
69: protected $_defaultPriorities = array();
70:
71: 72: 73:
74: protected $_buffer = array();
75:
76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90:
91: public function __construct($writer = false) {
92: global $cfg;
93:
94: $createWriter = false;
95:
96: if ($writer == false) {
97: $createWriter = true;
98: } else if (!is_object($writer) || ($writer instanceof cLogWriter) == false) {
99: cWarning(__FILE__, __LINE__, "The passed class is not a subclass of cLogWriter. Creating new one.");
100: $createWriter = true;
101: }
102:
103: if ($createWriter == true) {
104: $options = array('destination' => $cfg['path']['contenido_logs'] . 'data/contenido.log');
105: $writer = cLogWriter::factory("File", $options);
106: }
107:
108: $this->setWriter($writer);
109: $this->setShortcutHandler("%date", array($this, "shDate"));
110: $this->setShortcutHandler("%level", array($this, "shLevel"));
111: $this->setShortcutHandler("%message", array($this, "shMessage"));
112:
113: $this->getWriter()->setOption('log_format', '[%date] [%level] %message', false);
114:
115: $reflection = new ReflectionClass($this);
116: $this->_priorities = $this->_defaultPriorities = array_flip($reflection->getConstants());
117: }
118:
119: 120: 121: 122:
123: public function getWriter() {
124: return $this->_writer;
125: }
126:
127: 128: 129: 130: 131:
132: public function setWriter(cLogWriter $writer) {
133: $this->_writer = $writer;
134: }
135:
136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147:
148: public function setShortcutHandler($shortcut, $handler) {
149: if ($shortcut == '') {
150: throw new cInvalidArgumentException('The shortcut name must not be empty.');
151: }
152:
153: if (substr($shortcut, 0, 1) == "%") {
154: $shortcut = substr($shortcut, 1);
155: }
156:
157: if (is_callable($handler) == false) {
158: throw new cInvalidArgumentException('The specified shortcut handler does not exist.');
159: }
160:
161: if (array_key_exists($shortcut, $this->_shortcutHandlers)) {
162: throw new cInvalidArgumentException('The shortcut ' . $shortcut . ' is already in use!');
163: }
164:
165: $this->_shortcutHandlers[$shortcut] = $handler;
166:
167: return true;
168: }
169:
170: 171: 172: 173: 174: 175: 176: 177:
178: public function unsetShortcutHandler($shortcut) {
179: if (!in_array($shortcut, $this->_shortcutHandlers)) {
180: throw new cInvalidArgumentException('The specified shortcut handler does not exist.');
181: }
182:
183: unset($this->_shortcutHandlers[$shortcut]);
184: return true;
185: }
186:
187: 188: 189: 190: 191: 192:
193: public function buffer($message, $priority = NULL) {
194: $this->_buffer[] = array($message, $priority);
195: }
196:
197: 198: 199: 200: 201:
202: public function commit($revoke = true) {
203: if (count($this->_buffer) == 0) {
204: cWarning(__FILE__, __LINE__, "There are no buffered messages to commit.");
205: return false;
206: }
207:
208: foreach ($this->_buffer as $bufferInfo) {
209: $this->log($bufferInfo[0], $bufferInfo[1]);
210: }
211:
212: if ($revoke == true) {
213: $this->revoke();
214: }
215: }
216:
217: 218: 219:
220: public function revoke() {
221: $this->_buffer = array();
222: }
223:
224: 225: 226: 227: 228: 229:
230: public function log($message, $priority = NULL) {
231: if ($priority && is_int($priority) == false && in_array($priority, $this->_priorities)) {
232: $priority = array_search($priority, $this->_priorities);
233: }
234:
235: if ($priority === NULL || array_key_exists($priority, $this->_priorities) == false) {
236: $priority = $this->getWriter()->getOption('default_priority');
237: }
238:
239: $logMessage = $this->getWriter()->getOption('log_format');
240: $lineEnding = $this->getWriter()->getOption('line_ending');
241:
242: foreach ($this->_shortcutHandlers as $shortcut => $handler) {
243: if (substr($shortcut, 0, 1) != "%") {
244: $shortcut = "%" . $shortcut;
245: }
246:
247: $info = array(
248: 'message' => $message,
249: 'priority' => $priority
250: );
251:
252: $value = call_user_func($handler, $info);
253:
254: $logMessage = str_replace($shortcut, $value, $logMessage);
255: }
256:
257: $this->getWriter()->write($logMessage . $lineEnding, $priority);
258: }
259:
260: 261: 262: 263: 264: 265: 266: 267:
268: public function addPriority($name, $value) {
269: if ($name == '') {
270: throw new cInvalidArgumentException('Priority name must not be empty.');
271: }
272:
273: if (in_array($name, $this->_priorities)) {
274: throw new cInvalidArgumentException('The given priority name already exists.');
275: }
276:
277: if (array_key_exists($value, $this->_priorities)) {
278: throw new cInvalidArgumentException('The priority value already exists.');
279: }
280:
281: $this->_priorities[$value] = $name;
282: }
283:
284: 285: 286: 287: 288: 289: 290: 291:
292: public function removePriority($name) {
293: if ($name == '') {
294: throw new cInvalidArgumentException('Priority name must not be empty.');
295: }
296:
297: if (in_array($name, $this->_priorities) == false) {
298: throw new cInvalidArgumentException('Priority name does not exist.');
299: }
300:
301: if (in_array($name, $this->_defaultPriorities) == true) {
302: throw new cInvalidArgumentException('Removing default priorities is not allowed.');
303: }
304:
305: $priorityIndex = array_search($name, $this->_priorities);
306:
307: unset($this->_priorities[$priorityIndex]);
308: }
309:
310: 311: 312: 313: 314: 315: 316:
317: public function __call($method, $arguments) {
318: $priorityName = strtoupper($method);
319:
320: if (in_array($priorityName, $this->_priorities) == false) {
321: throw new cInvalidArgumentException('The given priority ' . $priorityName . ' is not supported.');
322: }
323:
324: $priorityIndex = array_search($priorityName, $this->_priorities);
325:
326: return $this->log($arguments[0], $priorityIndex);
327: }
328:
329: 330: 331: 332: 333:
334: public function shDate() {
335: return date("Y-m-d H:i:s");
336: }
337:
338: 339: 340: 341: 342: 343:
344: public function shLevel($info) {
345: $logLevel = $info['priority'];
346: return str_pad($this->_priorities[$logLevel], 10, " ", STR_PAD_BOTH);
347: }
348:
349: 350: 351: 352: 353:
354: public function shMessage($info) {
355: return $info['message'];
356: }
357: }
358:
359: ?>