From c5f0825326af0d9d01fc06d27b875ef238ec0498 Mon Sep 17 00:00:00 2001 From: Miikka Virtanen Date: Tue, 15 Jan 2013 00:33:48 +0200 Subject: [PATCH] Added a custom Exception and changed the die() statements to use that. Also added an option to change the timestamp format in the constructor. --- src/class.log.php | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/src/class.log.php b/src/class.log.php index 2138941..d62318b 100644 --- a/src/class.log.php +++ b/src/class.log.php @@ -1,4 +1,11 @@ logs = $logfiles; @@ -39,6 +46,9 @@ public function __construct($logfiles = 'temp.log', $timezone = 0) $timezone = (is_string($timezone)) ? $timezone : date_default_timezone_get(); date_default_timezone_set($timezone); + + // If the user specified a custom timestamp format, use that + if ($timestamp_format) $this->timestamp_format = $timestamp_format; $this->activateLog(); } @@ -73,7 +83,7 @@ public function entry($entry, $meta = array(), $timestamp = true, $log = 'defaul // Add timestamp if($timestamp){ - $this->entry = '[' . date(self::TIMESTAMP_FORMAT, time()) . ']: '; + $this->entry = '[' . date($this->timestamp_format, time()) . ']: '; } // Add meta data, if available @@ -89,14 +99,15 @@ public function entry($entry, $meta = array(), $timestamp = true, $log = 'defaul if($this->log_status && is_string($log)){ - $fp = @fopen($this->logs[$log],'a') or die('Can\'t open log file: ' . $log); - - if($fp){ - fwrite($fp, $this->entry); - }else{ + $fp = @fopen($this->logs[$log],'a'); + + if (!$fp) + { $this->log_status = self::LOG_FAILED; + throw new LogException('Can\'t open log file: ' . $log); } - + + fwrite($fp, $this->entry); fclose($fp); } } @@ -109,7 +120,8 @@ public function entry($entry, $meta = array(), $timestamp = true, $log = 'defaul public function clearLog($log = 'default') { - $fp = fopen($this->logs[$log],'w') or die("can't open log"); + $fp = @fopen($this->logs[$log],'w'); + if (!$fp) throw new LogException('Can\'t open log file: ' . $log); } /**