You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4php-dev@logging.apache.org by gr...@apache.org on 2009/07/28 07:53:19 UTC

svn commit: r798410 - in /incubator/log4php/trunk/src: changes/changes.xml main/php/appenders/LoggerAppenderPDO.php test/php/appenders/LoggerAppenderPDOTest.php

Author: grobmeier
Date: Tue Jul 28 05:53:18 2009
New Revision: 798410

URL: http://svn.apache.org/viewvc?rev=798410&view=rev
Log:
LOG4PHP-63: PDOAppender should throw LoggerException on database problems. Patch applied from Christian Hammers

Modified:
    incubator/log4php/trunk/src/changes/changes.xml
    incubator/log4php/trunk/src/main/php/appenders/LoggerAppenderPDO.php
    incubator/log4php/trunk/src/test/php/appenders/LoggerAppenderPDOTest.php

Modified: incubator/log4php/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/incubator/log4php/trunk/src/changes/changes.xml?rev=798410&r1=798409&r2=798410&view=diff
==============================================================================
--- incubator/log4php/trunk/src/changes/changes.xml (original)
+++ incubator/log4php/trunk/src/changes/changes.xml Tue Jul 28 05:53:18 2009
@@ -62,6 +62,7 @@
 		<action type="fix" issue="LOG4PHP-54">PHPDoc is wrong due to class movements - clean up (Christian Grobmeier)</action>
 		<action type="update" issue="LOG4PHP-60">Improved quickstart.apt (Christian Hammers)</action>
 		<action type="update" issue="LOG4PHP-62">Does not print warning if ini file is corrupt (Christian Hammers)</action>
+		<action type="update" issue="LOG4PHP-63">PDOAppender should throw LoggerException on database problems (Christian Hammers)</action>
 		<action type="fix" issue="LOG4PHP-64">Remove deprecated call-by-reference in LoggerLayoutPattern (Christian Hammers)</action>
 		<action type="update">Initial port to PHP 5 (Knut Urdalen)</action>
 		<action type="update">Established new unit test suite (Knut Urdalen)</action>

Modified: incubator/log4php/trunk/src/main/php/appenders/LoggerAppenderPDO.php
URL: http://svn.apache.org/viewvc/incubator/log4php/trunk/src/main/php/appenders/LoggerAppenderPDO.php?rev=798410&r1=798409&r2=798410&view=diff
==============================================================================
--- incubator/log4php/trunk/src/main/php/appenders/LoggerAppenderPDO.php (original)
+++ incubator/log4php/trunk/src/main/php/appenders/LoggerAppenderPDO.php Tue Jul 28 05:53:18 2009
@@ -70,32 +70,39 @@
      * @throws a PDOException if the attempt to connect to the requested database fails.
      */
     public function activateOptions() {
-    	if($this->user === null) {
-	    	$this->db = new PDO($this->dsn);
-    	} else if($this->password === null) {
-    	    $this->db = new PDO($this->dsn, $this->user);
-    	} else {
-    	    $this->db = new PDO($this->dsn,$this->user,$this->password);
-    	}
+        try {
+        	if($this->user === null) {
+	           	$this->db = new PDO($this->dsn);
+    	   } else if($this->password === null) {
+    	       $this->db = new PDO($this->dsn, $this->user);
+    	   } else {
+    	       $this->db = new PDO($this->dsn,$this->user,$this->password);
+    	   }
+    	   $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
     	
-        // test if log table exists
-        $result = $this->db->query('select * from ' . $this->table . ' where 1 = 0');
-        if ($result == false and $this->createTable) {
-        	// TODO mysql syntax?
-            $query = "CREATE TABLE {$this->table} (	 timestamp varchar(32)," .
+            // test if log table exists
+            try {
+                $result = $this->db->query('select * from ' . $this->table . ' where 1 = 0');
+            } catch (PDOException $e) {
+                // It could be something else but a "no such table" is the most likely
+                $result = false;
+            }
+            
+            // create table if necessary
+            if ($result == false and $this->createTable) {
+        	   // TODO mysql syntax?
+                $query = "CREATE TABLE {$this->table} (	 timestamp varchar(32)," .
             										"logger varchar(32)," .
             										"level varchar(32)," .
             										"message varchar(64)," .
             										"thread varchar(32)," .
             										"file varchar(64)," .
             										"line varchar(4) );";
-
-            $result = $this->db->query($query);
-            if (!$result) {
-                $this->canAppend = false;
-                return;
-                // TODO throw exception?
+                $result = $this->db->query($query);
             }
+        } catch (PDOException $e) {
+            $this->canAppend = false;
+            throw new LoggerException($e);
         }
         
         if($this->sql == '' || $this->sql == null) {
@@ -122,7 +129,11 @@
     public function append($event) {
         if ($this->canAppend) {
             $query = $this->layout->format($event);
-            $this->db->exec($query);
+            try {
+                $this->db->exec($query);
+            } catch (Exception $e) {
+                throw new LoggerException($e);
+            }
         }
     }
     

Modified: incubator/log4php/trunk/src/test/php/appenders/LoggerAppenderPDOTest.php
URL: http://svn.apache.org/viewvc/incubator/log4php/trunk/src/test/php/appenders/LoggerAppenderPDOTest.php?rev=798410&r1=798409&r2=798410&view=diff
==============================================================================
--- incubator/log4php/trunk/src/test/php/appenders/LoggerAppenderPDOTest.php (original)
+++ incubator/log4php/trunk/src/test/php/appenders/LoggerAppenderPDOTest.php Tue Jul 28 05:53:18 2009
@@ -63,8 +63,23 @@
 		
     }
     
+    public function testException() {
+        $dsn = 'doenotexist';
+        $appender = new LoggerAppenderPDO("myname");
+        $appender->setDSN($dsn);
+        $appender->setCreateTable(true);
+        
+        $catchedException = null;
+        try {
+            $appender->activateOptions();
+        } catch (LoggerException $e) {
+            $catchedException = $e;
+        }
+        self::assertNotNull($catchedException);
+    }
+    
     public function tearDown() {
-    	unlink('../../../target/pdotest.sqlite');
+    	@unlink('../../../target/pdotest.sqlite');
     }
     
 }