Location: PHPKode > scripts > Simple class for logs > Logger.class.php
<?php
/**
 * Simple class for logs
 * @description:
  Simple class for logs. 
    - mode of read/write
    - you can write date, memory use.
 * @version 1.0
 * @author Afanasiev Stas <hide@address.com>
 * @create 27.02.2011
 */
class Logger {

    /**
     * Link to opened file
     * @access private
     * @var void
     */
    private $logSource ='';

    /**
     * Log file name
     * @access private
     * @var string
     */
    private $logFilename ='';

    /**
     * Constructor
     */
    function __construct($logFilename,$createMode='w'){

        $this->logFilename = $logFilename;
        $this->logSource = fopen($this->logFilename,$createMode);

        if(!$this->logSource) {
            trigger_error('Don create file: '.$this->logFilename,E_USER_ERROR);
        } else {
            fclose($this->logSource);
        }

    }

    /**
     * Write string in log file
     * @param string $str - Your string
     * @param bool $timestamp - Write date in file? (true/false)
     * @param string $writeMode - Mode of write.(see http://ru2.php.net/manual/en/function.fopen.php)
     * @param bool $showMemoryUse - Write memory use in file? (true/false)
     * @return void
     */
    function LogIt($str,$timestamp=true,$separator="\n",$writeMode='a+',$showMemoryUse=true) {

        $this->logSource = fopen($this->logFilename,$writeMode);

        if(!$this->logSource) {
            trigger_error('Don read/write file: '.$this->logFilename,E_USER_ERROR);
        }   else  {

            if($timestamp) {
                $str = date('Y-d-m H:i:s',time()).' -- '.$str;
            }

            if($showMemoryUse) {
                $str = $str.' -- Memory usage: '.$this->ConvertBytes(memory_get_usage(true)).' kb.';
            }

            $ok = fwrite($this->logSource,$str.$separator);
            fclose($this->logSource);
        }
    }
    /**
     * Function convert byte to kilobyte
     * @param string $str - Number use of memory
     * @return int
     */
    function ConvertBytes($str) {
        return intval($str)/1024;
    }
}
Return current item: Simple class for logs