source: t29-www/lib/logging.php @ 299

Last change on this file since 299 was 299, checked in by heribert, 12 years ago

Zahlreiche Bugfixes, vor allem an der englischen Seite und im Deutschen. Letzte Änderungen, bevor Homepage offiziell ihren Dienst antritt.

File size: 3.2 KB
Line 
1<?php
2/**
3 * t29Log is a very lightweight logging system for t29v6.
4 *
5 * The logging class is accessible via the Singleton pattern, but should
6 * be stored in a $GLOBAL["log"] and accessible like that everywhere.
7 * The contents are printed in t29Template.
8 *
9 * Inspired by Klooger for PHP: https://github.com/katzgrau/KLogger/
10 **/
11
12
13class t29Log {
14        const EMERG  = 'emerg';  // Emergency: system is unusable
15        const ALERT  = 'alert';  // Alert: action must be taken immediately
16        const CRIT   = 'crit';   // Critical: critical conditions
17        const ERR    = 'err';    // Error: error conditions
18        const WARN   = 'warn';   // Warning: warning conditions
19        const NOTICE = 'notice'; // Notice: normal but significant condition
20        const INFO   = 'info';   // Informational: informational messages
21        const DEBUG  = 'debug';  // Debug: debug messages
22       
23    /**
24     * We need a default argument value in order to add the ability to easily
25     * print out objects etc. But we can't use NULL, 0, FALSE, etc, because those
26     * are often the values the developers will test for. So we'll make one up.
27     */
28    const NO_ARGUMENTS = 't29Log::NO_ARGUMENTS';
29        /*
30                log array format:
31                        [
32                                [LEVEL,string],
33                                [LEVEL,string],
34                                [LEVEL,string],
35                                ...
36                        ]
37        */
38        public $entries = array();
39       
40        // the one global t29Log instance
41        private static $instance;
42       
43        // singleton access method
44        static public function get() {
45                if(!isset(self::$instance))
46                        self::$instance = new t29Log;
47                return self::$instance;
48        }
49       
50        private function __construct() {
51                // we shall be the PHP error handler
52                set_error_handler(array($this, 'log_phperror'));
53               
54                // and register a final shutdown function
55                register_shutdown_function(array($this, 'php_shutdown'));
56        }
57       
58        function log($line, $severity, $args = self::NO_ARGUMENTS) {
59                if($args !== self::NO_ARGUMENTS)
60                        $line .= '; '. var_export($args, true);
61                $this->entries[] = array($severity, $line);
62        }
63       
64        function log_phperror($errno, $errstr, $errfile, $errline) {
65                switch($errno) {
66                        case E_WARNING: $errno = self::WARN; break;
67                        case E_NOTICE: $errno = self::NOTICE; break;
68                        default: $errno = self::WARN; break;
69                }
70               
71                $this->log("Error on line <tt class='line'>$errline</tt> in file <tt class='file'>$errfile</tt>:\n<pre>".htmlspecialchars($errstr)."</pre>",
72                        $errno);
73
74                /* Don't execute PHP internal error handler */
75                return true;
76        }
77       
78        function php_shutdown() {
79                if(!$this->is_empty()) {
80                        // we still have errors. print them!
81                        $this->print_all('final shutdown');
82                }
83        }
84       
85        function is_empty() {
86                return empty($this->entries);
87        }
88       
89        function print_all($ul_classes='') {
90                // causal printing function. Flushes entries afterwards!
91                print "<ul class='$ul_classes'>";
92                foreach($this->entries as $entry) {
93                        printf('<li class="%s">%s</li>'.PHP_EOL, $entry[0], $entry[1]);
94                }
95                print "</ul>";
96                $this->entries = array(); // flush entries!
97        }
98       
99        // convenience functions
100        public function FATAL($line, $args = self::NO_ARGUMENTS){
101                $this->log($line, self::FATAL, $args);
102        }
103
104        public function INFO($line, $args = self::NO_ARGUMENTS){
105                $this->log($line, self::INFO, $args);
106        }
107
108        public function DEBUG($line, $args = self::NO_ARGUMENTS){
109                $this->log($line, self::INFO, $args);
110        }
111       
112} // class
Note: See TracBrowser for help on using the repository browser.
© 2008 - 2013 technikum29 • Sven Köppel • Some rights reserved
Powered by Trac
Expect where otherwise noted, content on this site is licensed under a Creative Commons 3.0 License