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

PHP Result Center PHP Result Center เป็นหมวด ที่ไว้รวบรวม โปรแกรม Code php Javascript CSS CMS

Moderator: mindphp, ผู้ดูแลกระดาน

ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41239
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

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

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

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();
}
?>
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ภาพประจำตัวสมาชิก
imsn
PHP Super Member
PHP Super Member
โพสต์: 375
ลงทะเบียนเมื่อ: 07/05/2010 12:58 pm
ติดต่อ:

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

โพสต์ที่ยังไม่ได้อ่าน โดย imsn »

ขอบคุณครับ แล่มเลย
ช่วยตอบเพราะอยากโปรโหมทเว็บ คิคิคลิกเลย
หาเพื่อนฝรั่งคุย M ...
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41239
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

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

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

class นี้ใช้ได้ กับ php5 เป็นต้นไปนะครับ
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 77