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

Last change on this file since 930 was 357, checked in by sven, 11 years ago

t29Host-Webroot-System entwickelt.

Damit können Installationen der technikum29-Website ab nun auch in Unterverzeichnissen
(bislang allerdings nur unterhalb des DocumentRoots) installiert werden, was einem
größere Flexibilität beim lokalen Aufsetzen der Seite liefert

File size: 3.8 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
12class t29Log {
13        const EMERG  = 'emerg';  // Emergency: system is unusable
14        const ALERT  = 'alert';  // Alert: action must be taken immediately
15        const CRIT   = 'crit';   // Critical: critical conditions
16        const ERR    = 'err';    // Error: error conditions
17        const WARN   = 'warn';   // Warning: warning conditions
18        const NOTICE = 'notice'; // Notice: normal but significant condition
19        const INFO   = 'info';   // Informational: informational messages
20        const DEBUG  = 'debug';  // Debug: debug messages
21       
22    /**
23     * We need a default argument value in order to add the ability to easily
24     * print out objects etc. But we can't use NULL, 0, FALSE, etc, because those
25     * are often the values the developers will test for. So we'll make one up.
26     */
27    const NO_ARGUMENTS = 't29Log::NO_ARGUMENTS';
28   
29    /**
30     * A magic argument which immediately forces the print_all()
31     * function to be called. This is good when the log method is called while
32     * php_shutdown is already in progress, otherwise your log lines will end
33     * in nirvana.
34     **/
35    const IMMEDIATELY_PRINT = 't29Log::IMMEDIATELY_PRINT';
36        /*
37                log array format:
38                        [
39                                [LEVEL,string],
40                                [LEVEL,string],
41                                [LEVEL,string],
42                                ...
43                        ]
44        */
45        public $entries = array();
46       
47        // the one global t29Log instance
48        private static $instance;
49       
50        // singleton access method
51        static public function get() {
52                if(!isset(self::$instance))
53                        self::$instance = new t29Log;
54                return self::$instance;
55        }
56       
57        private function __construct() {
58                // we shall be the PHP error handler
59                set_error_handler(array($this, 'log_phperror'));
60               
61                // and register a final shutdown function
62                register_shutdown_function(array($this, 'php_shutdown'));
63        }
64       
65        function log($line, $severity, $args = self::NO_ARGUMENTS) {
66                if($args === self::IMMEDIATELY_PRINT) {
67                        // to be used when in shutdown already
68                        $this->entries[] = array($severity, $line);
69                        $this->print_all('immediately final');
70                        return;
71                }
72                if($args !== self::NO_ARGUMENTS)
73                        $line .= '; '. var_export($args, true);
74                $this->entries[] = array($severity, $line);
75        }
76       
77        function log_phperror($errno, $errstr, $errfile, $errline) {
78                switch($errno) {
79                        case E_WARNING: $errno = self::WARN; break;
80                        case E_NOTICE: $errno = self::NOTICE; break;
81                        default: $errno = self::WARN; break;
82                }
83               
84                $this->log("Error on line <tt class='line'>$errline</tt> in file <tt class='file'>$errfile</tt>:\n<pre>".htmlspecialchars($errstr)."</pre>",
85                        $errno);
86
87                /* Don't execute PHP internal error handler */
88                return true;
89        }
90       
91        function php_shutdown() {
92                if(!$this->is_empty()) {
93                        // we still have errors. print them!
94                        $this->print_all('final shutdown');
95                }
96        }
97       
98        function is_empty() {
99                return empty($this->entries);
100        }
101       
102        function print_all($ul_classes='') {
103                // causal printing function. Flushes entries afterwards!
104                // if $ul_classes is given, will print a list around
105                if($ul_classes)
106                        print "<ul class='messages footer $ul_classes'>";
107                foreach($this->entries as $entry) {
108                        printf('<li class="%s">%s</li>'.PHP_EOL, $entry[0], $entry[1]);
109                }
110                if($ul_classes) print "</ul>";
111                $this->entries = array(); // flush entries!
112        }
113       
114        // convenience functions
115        public function FATAL($line, $args = self::NO_ARGUMENTS) { $this->log($line, self::FATAL, $args); }
116        public function INFO($line, $args = self::NO_ARGUMENTS)  { $this->log($line, self::INFO, $args); }
117        public function DEBUG($line, $args = self::NO_ARGUMENTS) { $this->log($line, self::DEBUG, $args); }
118        public function WARN($line, $args = self::NO_ARGUMENTS) { $this->log($line, self::WARN, $args); }
119} // 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