|
| 1 | +<?php |
| 2 | +/* ⣠⣾⣿ Simple PHP log class PHP 4.3 and > ⣿⣷⣄ */ |
| 3 | +class log { |
| 4 | + protected $options = array( |
| 5 | + // e.g. '/path/to/logfile' OR use '.' = logfile life in same directory |
| 6 | + 'path' => '.', |
| 7 | + // logfile filename without extention ('-date' in Y-m-d format will be attached) |
| 8 | + 'filename' => 'log', |
| 9 | + // e.g. true = use system log function (works only in txt format) |
| 10 | + 'syslog' => false, |
| 11 | + // e.g. 0644 OR 0777 see: http://php.net/manual/en/function.chmod.php |
| 12 | + 'filePermission' => 0644, |
| 13 | + // Maximal LogFile Size in MB |
| 14 | + 'maxSize' => 10, |
| 15 | + // e.g. 'txt' = Text with TAB OR 'csv' = Comma-Separated Values with (,) OR 'htm' = HTML |
| 16 | + 'format' => 'htm', |
| 17 | + // e.g. 'terminal' = terminalcss.xyz OR 'barecss' = barecss.com OR plain = simple HTML |
| 18 | + 'template' => 'barecss', |
| 19 | + // e.g. 'UTC' see: http://php.net/manual/en/timezones.php |
| 20 | + 'timeZone' => 'UTC', |
| 21 | + // e.g. 'Y-m-d H:i:s' see: http://php.net/manual/en/function.date.php |
| 22 | + 'dateFormat' => 'Y-m-d H:i:s', |
| 23 | + // e.g. true = Calling Prog. AND Linenumber OR false = Only calling Prog. |
| 24 | + 'backtrace' => true, |
| 25 | + ); |
| 26 | + // log message severities from RFC 3164, section 4.1.1, table 2. |
| 27 | + // http://www.faqs.org/rfcs/rfc3164.html |
| 28 | + protected $level = array( |
| 29 | + 100 => 'DEBUG', // Debug: debug messages |
| 30 | + 200 => 'INFO', // Informational: informational messages |
| 31 | + 300 => 'NOTICE', // Notice: normal but significant condition |
| 32 | + 400 => 'WARNING', // Warning: warning conditions |
| 33 | + 500 => 'ERROR', // Error: error conditions |
| 34 | + 600 => 'CRITICAL', // Critical: critical conditions |
| 35 | + 700 => 'ALERT', // Alert: action must be taken immediately |
| 36 | + 800 => 'EMERGENCY', // Emergency: system is unusable |
| 37 | + 900 => 'GAU', // Maximum Credible Accident: no system anymore, we need no log |
| 38 | + ); |
| 39 | + // set open and write error messages for die() |
| 40 | + protected $error = array( |
| 41 | + 'openA' => 'The logfile could not be opened for appending. Check permissions: ', |
| 42 | + 'openW' => 'The logfile exists, but could not be opened for writing. Check permissions: ', |
| 43 | + 'write' => 'The logfile could not be written. Check logfile: ', |
| 44 | + ); |
| 45 | + // set logfile name and options |
| 46 | + public function __construct($params=array()) { |
| 47 | + $this->params = array_merge($this->options, $params); |
| 48 | + // set default max logfile size |
| 49 | + $this->maxSize(); |
| 50 | + } |
| 51 | + // set logfile max. filesize |
| 52 | + public function maxSize($size=0) { |
| 53 | + if ($size==0) $size=$this->params['maxSize']; |
| 54 | + $this->log_size = $size * (1024 * 1024); // calc megabyt to byte |
| 55 | + } |
| 56 | + // alias functions |
| 57 | + public function debug($message){ |
| 58 | + $this->write((string) $message, (int) 100); |
| 59 | + } |
| 60 | + public function info($message){ |
| 61 | + $this->write((string) $message, (int) 200); |
| 62 | + } |
| 63 | + public function notice($message){ |
| 64 | + $this->write((string) $message, (int) 300); |
| 65 | + } |
| 66 | + public function warning($message){ |
| 67 | + $this->write((string) $message, (int) 400); |
| 68 | + } |
| 69 | + public function error($message){ |
| 70 | + $this->write((string) $message, (int) 500); |
| 71 | + } |
| 72 | + public function critical($message){ |
| 73 | + $this->write((string) $message, (int) 600); |
| 74 | + } |
| 75 | + public function alert($message){ |
| 76 | + $this->write((string) $message, (int) 700); |
| 77 | + } |
| 78 | + public function emergency($message){ |
| 79 | + $this->write((string) $message, (int) 800); |
| 80 | + } |
| 81 | + public function gau($message){ |
| 82 | + $this->write((string) $message, (int) 900); |
| 83 | + } |
| 84 | + // write message to the logfile |
| 85 | + public function write($message, $status) { |
| 86 | + if (is_array($message)) { |
| 87 | + $message = implode(' ', $message); |
| 88 | + } |
| 89 | + // if status is a number set status name |
| 90 | + if (isset($this->level[$status])) { |
| 91 | + $status = $status.' '.$this->level[$status]; |
| 92 | + } |
| 93 | + // if file handler doesn't exist, then open logfile |
| 94 | + if (!isset($this->fh) || !is_resource($this->fh)) { |
| 95 | + $this->lopen(); |
| 96 | + } |
| 97 | + // use sys log |
| 98 | + if ($this->params['syslog'] == true) { |
| 99 | + $type = 'txt'; |
| 100 | + $time = ''; |
| 101 | + }else{ |
| 102 | + // get logfile type |
| 103 | + $type = strtolower($this->params['format']); |
| 104 | + // define current time for logfile entry - (@ suppress the E_WARNING) |
| 105 | + @ini_set('date.timezone', $this->params['timeZone']); // set timezone |
| 106 | + @date_default_timezone_set($this->params['timeZone']); // set timezone |
| 107 | + $time = @date($this->params['dateFormat']); |
| 108 | + } |
| 109 | + // look for the caller |
| 110 | + if ($this->params['backtrace']==true) { |
| 111 | + $backtrace = debug_backtrace(); |
| 112 | + $caller = array_shift($backtrace); |
| 113 | + $proc = $caller['file'].' ('.$caller['line'].')'; |
| 114 | + } else { |
| 115 | + $proc = $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; |
| 116 | + } |
| 117 | + // build logfile entry |
| 118 | + if ($type=='txt') { |
| 119 | + $line = $time."\t".$proc."\t".$message."\t".$status.PHP_EOL; |
| 120 | + } elseif ($type=='csv') { |
| 121 | + $line = $time.','.$proc.','.$message.','.$status.PHP_EOL; |
| 122 | + } elseif ($type=='htm'||'html') { |
| 123 | + $html1 = '<tr><td data-label="Date/Time">'; |
| 124 | + $html2 = '</td><td data-label="Programm">'; |
| 125 | + $html3 = '</td><td data-label="Message">'; |
| 126 | + $html4 = '</td><td data-label="Status">'; |
| 127 | + $html5 = '</tag></td></tr>'; |
| 128 | + $status= '<tag '.$status.'>'.$status; |
| 129 | + $line = $html1.$time.$html2.$proc.$html3.$message.$html4.$status.$html5.PHP_EOL; |
| 130 | + } |
| 131 | + // write current entry to the log file |
| 132 | + if ($this->params['syslog'] == true) { |
| 133 | + error_log($line); |
| 134 | + } else { |
| 135 | + flock($this->fh, LOCK_EX); |
| 136 | + fwrite($this->fh, $line) or die($this->error['write'].$this->file); |
| 137 | + flock($this->fh, LOCK_UN); |
| 138 | + } |
| 139 | + // in debug mode print entry also to the browser |
| 140 | + if (defined('DEBUG') == true && DEBUG === true) { |
| 141 | + echo '<pre>'.$line.'</pre>'; |
| 142 | + } |
| 143 | + } |
| 144 | + // open logfile (private method) |
| 145 | + private function lopen() { |
| 146 | + // get logfile type |
| 147 | + $type = strtolower($this->params['format']); |
| 148 | + // get logfile path |
| 149 | + $this->path = rtrim($this->params['path'], '\\/'); |
| 150 | + // sys log only in txt format |
| 151 | + if ($this->params['syslog'] == true) { |
| 152 | + $type='txt'; |
| 153 | + } |
| 154 | + // get default logfile name |
| 155 | + $this->file = $this->path.'/'.$this->params['filename'].'-'.date('Y-m-d').'.'.$type; |
| 156 | + // if logfile is to big, delete it |
| 157 | + if (file_exists($this->file) and isset($this->log_size)) { |
| 158 | + clearstatcache(FALSE, $this->file); |
| 159 | + if (filesize($this->file) > $this->log_size) { |
| 160 | + unlink($this->file); |
| 161 | + } |
| 162 | + } |
| 163 | + // use sys log |
| 164 | + if ($this->params['syslog'] == true) { |
| 165 | + @ini_set('log_errors', 1); |
| 166 | + @ini_set('error_log', $this->file); |
| 167 | + return; |
| 168 | + } |
| 169 | + // if logfile not exist create it |
| 170 | + if (!file_exists($this->file)) { |
| 171 | + $this->fh = fopen($this->file, 'w') |
| 172 | + or die($this->error['openW'].$this->file); |
| 173 | + if ($type=='htm' || $type=='html') { |
| 174 | + if ($html = @file_get_contents('tpl.'.$this->params['template'].'.htm')) { |
| 175 | + fwrite($this->fh, $html) or die($this->error['write'].$this->file); |
| 176 | + } else { |
| 177 | + $html = '<html><head><title></title><body><tt><h2>Log</h2><table>'; |
| 178 | + fwrite($this->fh, $html) or die($this->error['write'].$this->file); |
| 179 | + } |
| 180 | + } |
| 181 | + fclose($this->fh); |
| 182 | + if (!is_writable($this->file)) { |
| 183 | + chmod($this->file, $this->params['filePermission']); |
| 184 | + } |
| 185 | + } |
| 186 | + // file exist, so open logfile for appending |
| 187 | + $this->fh = fopen($this->file, 'a') |
| 188 | + or die($this->error['openA'].$this->file); |
| 189 | + } |
| 190 | + // close logfile |
| 191 | + public function __destruct(){ |
| 192 | + if ($this->fh) { |
| 193 | + fclose($this->fh); |
| 194 | + } |
| 195 | + elseif ($this->params['syslog'] == true){ |
| 196 | + closelog(); |
| 197 | + } |
| 198 | + } |
| 199 | +} |
| 200 | +/* ⣠⣾⣿ EOF - END OF FILE ⣿⣷⣄ */ |
0 commit comments