Location: PHPKode > scripts > IDlog > idlog/idlog.class.php

class IDLog {
	const _VER= '1.1'; //class version
	private $_enabled = true;
	private $_path = '';
	private $_prefix = '';
	private $_dateformat = '';
	private $_devider = '';
	private $_format = array();
	private $_logcount = 0;
	private $_startmtime = 0;

	function __construct($path) {
		$this->_startmtime= $this->Mtime();
		$this->_logcount= 0;
	//local function
	private function Mtime() {
		$temp= explode(' ', microtime());
		return $temp[0] + $temp[1];
	//set this to enable or disable logger
	public function Enabled($enabled=true) {
		$this->_enabled= $enabled;
	//set path to log file
	public function SetPath($path) {
		$this->_path= $path;
	//set default prefix if needed
	public function SetPrefix($prefix) {
		$this->_prefix= $prefix;
	//set DateTime format in log
	public function SetDateFormat($format='d-m-y H:i:s') {
		$this->_dateformat= $format;
	//change log section devider if needed
	public function SetDevider($devider='|') {
		$this->_devider= $devider;
	log format
		prefix - log prefix (for example if different scripts uses the same log file)
		id - number of log entry from script executing start
		code - error code or other "id" or code
		ip - client ip
		uagent - user agent (browser)
		date - date when entry adding into log
		time - result of function time();
		mtime - microtime from script start sctipt executing (to be exact time from class creation in script)
		text - log message itself
	public function SetLogFormat($format=array('id','date','code','text','ip','uagent','mtime')) {
		$this->_format= $format;

	//add log
	public function Log($text='',$code=0) {
		$logstring= '';
		foreach ($this->_format as $key) {
			switch ($key) {
				case 'prefix': $logstring.= $this->_prefix.$this->_devider; break;
				case 'id': $logstring.= $this->_logcount.$this->_devider; break;
				case 'code': $logstring.= $code.$this->_devider; break;
				case 'ip': $logstring.= $_SERVER['REMOTE_ADDR'].$this->_devider; break;
				case 'uagent': $logstring.= $_SERVER['HTTP_USER_AGENT'].$this->_devider; break;
				case 'date': $logstring.= date($this->_dateformat).$this->_devider; break;
				case 'time': $logstring.= time().$this->_devider; break;
				case 'mtime': $logstring.= round($this->Mtime()-$this->_startmtime,4).$this->_devider; break;
				case 'text': $logstring.= $text.$this->_devider; break;
				default: $logstring.= 'wrong_log_format_entry'.$this->_devider; break;


Return current item: IDlog