php Log Class เอาไว้เขียน log ไฟล์ ลง text file การใช้งาน ใช้ try catch เข้ามาช่

ตอบกระทู้

รูปแสดงอารมณ์
:icon_plusone: :like: :plusone: :gfb: :-D :) :( :-o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: :angry: :baa: :biggrin:
รูปแสดงอารมณ์อื่นๆ

BBCode เปิด
[img] เปิด
[url] เปิด
[Smile icon] เปิด

กระทู้แนะนำ
   

มุมมองที่ขยายได้ กระทู้แนะนำ: php Log Class เอาไว้เขียน log ไฟล์ ลง text file การใช้งาน ใช้ try catch เข้ามาช่

Re: php Log Class เอาไว้เขียน log ไฟล์ ลง text file การใช้งาน ใช้ try catch เข้ามาช่

โดย mindphp » 22/12/2010 1:26 pm

class นี้ใช้ได้ กับ php5 เป็นต้นไปนะครับ

Re: php Log Class เอาไว้เขียน log ไฟล์ ลง text file การใช้งาน ใช้ try catch เข้ามาช่

โดย imsn » 07/12/2010 8:16 pm

ขอบคุณครับ แล่มเลย

php Log Class เอาไว้เขียน log ไฟล์ ลง text file การใช้งาน ใช้ try catch เข้ามาช่

โดย mindphp » 06/12/2010 2:50 am

php Log Class เอาไว้เขียน log ไฟล์ ลง text file
เป็น class
class logger

โค้ด: เลือกทั้งหมด


<?php

/**
 *
 * @Class logger
 *
 * @Purpose: Logs text to a file
 *
 * @Author: Kevin Waterson
 *
 * @copyright PHPRO.ORG (2009)
 *
 * @example usage
 * $log = logger::getInstance();
 * $log->logfile = '/tmp/errors.log';
 * $log->write('An error has occured', __FILE__, __LINE__);
 *
 */
class logger
{
    /*** Declare instance ***/
    private static $instance = NULL;

    /**
     *
     * @Constructor is set to private to stop instantion
     *
     */
    private function __construct()
    {
    }

    /**
     *
     * @settor
     *
     * @access public
     *
     * @param string $name
     *
     * @param mixed $value
     *
     */
    public function __set($name, $value)
    {
        switch($name)
        {
            case 'logfile':
            if(!file_exists($value) || !is_writeable($value))
            {
                throw new Exception("$value is not a valid file path");
            }
            $this->logfile = $value;
            break;

            default:
            throw new Exception("$name cannot be set");
        }
    }

    /**
     *
     * @write to the logfile
     *
     * @access public
     *
     * @param string $message
     *
     * @param string $file The filename that caused the error
     *
     * @param int $line The line that the error occurred on
     *
     * @return number of bytes written, false other wise
     *
     */
    public function write($message, $file=null, $line=null)
    {
        $message = time() .' - '.$message;
        $message .= is_null($file) ? '' : " in $file";
        $message .= is_null($line) ? '' : " on line $line";
        $message .= "\n";
        return file_put_contents( $this->logfile, $message, FILE_APPEND );
    }

    /**
    *
    * Return logger instance or create new instance
    *
    * @return object (PDO)
    *
    * @access public
    *
    */
    public static function getInstance()
    {
        if (!self::$instance)
        {
            self::$instance = new logger;
        }
        return self::$instance;
    }


    /**
     * Clone is set to private to stop cloning
     *
     */
    private function __clone()
    {
    }

} /*** end of log class ***/

?>

ตัวอย่างการใช้

โค้ด: เลือกทั้งหมด

<?php

try
{
    /*** a new logger instance ***/
    $log = logger::getInstance();
    /*** the file to write to ***/
    $log->logfile = '/tmp/errors.log';
    /*** write an error message with filename and line number ***/
    $log->write('An error has occured', __FILE__, __LINE__);
}
catch(Exception $e)
{
    echo $e->getMessage();
}
?>

ข้างบน