<?php
/* Multypurpose logger class v1.0.0
* ### Functions ###
* $log = log::start([$die_limit, $log_url, $log_ip, $log_time])
* Starts logger, arguments are optional and should be sent on first init,
* any further attempts will be ignored.
* If using inside functions $log = log::start should be called to get an access to object
* ### Arguments ###
* $die_limit - maximum rows in logger, if reached will cause script break (defaults to 0)
* $log_url - if set to 0, will not log url
* $log_ip - if set to 0, will not log ip
* $log_time - if set to 0, will not log time
* $log->add($string)
* Adds new entry to logger with $string text
* $log->build([$type])
* Returns log in string
* ### Arguments ###
* $type = ('html' || 'text' || 'plain'), defaults to 'html'
* 'html' - build log as html table
* 'text' - build as a text
* 'plain' - build as serialized object string
* $log->show([$row])
* Returns an object with parameters for $row(defaults to last string)
* $row - is a $row number to show
* returns $obj->string(entry text), [$obj->url, $obj->ip, $obj->time]*if set
* $log->write($filename [, $type, $rewrite])
* Writes data to file ($filename)
* ### Arguments ###
* $type - ('html', 'text', 'plain') same as for build
* $rewrite - bool, if set will not append, but rewrite file
*
* Under condition and terms of GNU/GPL
* 2011, Oleg Kasian, o-kasian@yandex.ru
*/
private $die_limit;
private $log_time;
private $log_ip;
private $log_url;
private $counter;
private $row;
private static $instance;
private function __construct($die_limit, $log_time, $log_ip, $log_url) {
$this->die_limit=$die_limit;
$this->log_time=$log_time;
$this->log_ip=$log_ip;
$this->log_url=$log_url;
$this->counter=0;
}
public function __clone() {}
//typical singleton construction function
public static function start($die_limit=0, $log_url=1, $log_ip=1, $log_time=1) {
if (!isset(self::$instance)) { $c = __CLASS__;
self::$instance = new $c($die_limit, $log_time, $log_ip, $log_url);
}
return self::$instance;
}
//adds new string to log, any string to log must be set as an argument
public function add($string) {
$this->row[$this->counter]->string = $string;
if($this->log_url) $this->row[$this->counter]->url = $_SERVER["REQUEST_URI"];
if($this->log_ip) $this->row[$this->counter]->ip = $_SERVER['REMOTE_ADDR'];
if($this->log_time) $this->row[$this->counter]->time = time(); ++$this->counter;
$this->limit();
}
//builds log to str, argument(optional) $type=('html' || 'text' || 'plain')
public function build($type='html') {
case 'html':
$output = '<h3>Site Log</h3><table style="width:100%" id="site_log">';
$output .= '<tr><td><b>Text</b></td>';
if($this->log_url) $output .= '<td><b>Url</b></td>';
if($this->log_ip) $output .= '<td><b>Ip</b></td>';
if($this->log_time) $output .= '<td><b>Time</b></td>';
$output .= '</tr>';
foreach($this->row as $value) {
$output .= '<tr><td>'.$value->string.'</td>';
if($this->log_url) $output .= '<td>'.($value->url).'</td>';
if($this->log_ip) $output .= '<td>'.($value->ip).'</td>';
if($this->log_time) $output .= '<td>'.date('r', $value->time).'</td>'; $output .= '</tr>';
}
$output .= '</table>';
break;
case 'text':
foreach($this->row as $value) {
$output .= $value->string.' called';
if($this->log_url) $output .= ' at '.$value->url;
if($this->log_ip) $output .= ' by '.$value->ip;
if($this->log_time) $output .= ' time '.date('r', $value->time); $output .= "\r\n";
}
break;
case 'plain':
break;
default:
$output='bad argument for build: '.strtolower($type); }
return $output;
}
//shows last row if no argument set, else shows row number $row(int value)
public function show($row='last') {
$output = $this->row[$this->counter-1];
} else {
if((int)$row>($this->counter-1)) $output='no row exist'; else $output = $this->row[$row];
}
return $output;
}
//writes log to $file (local path must be set)
public function write($file, $type='text', $rewrite=0) {
if($rewrite) $fp = fopen($file, 'w'); else $fp = fopen($file, 'a'); fwrite($fp, $this->build($type)); }
//writes custom error message if limit is reached
private function limit() {
if($this->die_limit and $this->die_limit==$this->counter) {
echo '<p style="display:block;color:red;background-color:yellow;border:1px solid red;">Log limit is reached. Will now stop.</p>';
}
}
}
?>