Error Logging Class

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Error Logging Class

Post by Benjamin »

I've been putting off building an error log class for a while, but I really did need one so I wrote this. It's very simple but it's all I really need. Maybe this can go in snippets.

Code: Select all

// Sample windows path c:\\directory\\error_log.txt

class errorLogger
{
    function __construct($errorLog = '/server/projects/error_log.txt')
    {
        $this->log = @fopen($errorLog, 'a+b'); // open log

        if ((is_resource($this->log)) && (is_writable($errorLog)))
        {
            $this->ready = true;
        } else {
            $this->ready = false; // could not open file
            trigger_error("<b>The file " . $errorLog . " could not be opened or is not writable.</b>", E_USER_NOTICE);
        }
    }

    public function newEntry($entry)
    {
        if ($this->ready) fwrite($this->log, $entry . "\n");
    }

    public function clear()
    {
        if ($this->ready) ftruncate($this->log, 0);
    }

    function __destruct()
    {
        if ($this->ready) fclose($this->log); // close the file
    }
}
Call with..

Code: Select all

$errorLog = new errorLogger();

$errorLog->newEntry("This is test error"); // add entry

$errorLog->clear(); // delete log
Last edited by Benjamin on Thu Jul 27, 2006 7:44 am, edited 3 times in total.
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Post by jamiel »

Put a timestamp with the error.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

you were write when you said simple.. :)

i wanted to have options. ^^
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Maybe a backtrace would be useful...
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Ambush Commander wrote:Maybe a backtrace would be useful...
Very good idea indeed!
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

Backtrace?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

as you are using PHP5, I cannot recommend strongly enough that you use exceptions. They are simply superb.

Perhaps a helper or wrapper class for your logger to use exceptions.

Code: Select all

class ExceptionLogger
{
    private $exception;
    private $logger;

    public function __construct ($errorLogger, $exception)
    {
        $this->logger = $errorLogger;
        $this->exception = $exception;
    }

    public function logError ()
    {
        $text = "Exception caught: " . get_class($this->exception)
              . "\nMessage: " . $this->exception->getMessage()
              . "\nTrace: " . $this->exception->getTraceAsString();

        $this->errorLogger->newEntry($text);
    }
}
Post Reply