Fixed a bug in file appenders where they would attempt to write the log event even...
authorIvan Habunek <ihabunek@apache.org>
Sat, 8 Sep 2012 07:51:18 +0000 (07:51 +0000)
committerIvan Habunek <ihabunek@apache.org>
Sat, 8 Sep 2012 07:51:18 +0000 (07:51 +0000)
git-svn-id: https://svn.apache.org/repos/asf/logging/log4php/trunk@1382243 13f79535-47bb-0310-9956-ffa450edef68

src/main/php/appenders/LoggerAppenderFile.php
src/main/php/appenders/LoggerAppenderRollingFile.php

index 078212c..c9aad10 100644 (file)
@@ -75,6 +75,8 @@ class LoggerAppenderFile extends LoggerAppender {
        /**
         * Acquires the target file resource, creates the destination folder if 
         * necessary. Writes layout header to file.
+        * 
+        * @return boolean FALSE if opening failed
         */
        protected function openFile() {
                $file = $this->getTargetFile();
@@ -88,7 +90,7 @@ class LoggerAppenderFile extends LoggerAppender {
                                if ($success === false) {
                                        $this->warn("Failed creating target directory [$dir]. Closing appender.");
                                        $this->closed = true;
-                                       return;
+                                       return false;
                                }
                        }
                }
@@ -97,8 +99,9 @@ class LoggerAppenderFile extends LoggerAppender {
                $this->fp = fopen($file, $mode);
                if ($this->fp === false) {
                        $this->warn("Failed opening target file. Closing appender.");
+                       $this->fp = null;
                        $this->closed = true;
-                       return;
+                       return false;
                }
                
                // Required when appending with concurrent access
@@ -117,7 +120,9 @@ class LoggerAppenderFile extends LoggerAppender {
        protected function write($string) {
                // Lazy file open
                if(!isset($this->fp)) {
-                       $this->openFile();
+                       if ($this->openFile() === false) {
+                               return; // Do not write if file open failed.
+                       }
                }
                
                if ($this->locking) {
@@ -159,6 +164,7 @@ class LoggerAppenderFile extends LoggerAppender {
                if (is_resource($this->fp)) {
                        $this->write($this->layout->getFooter());
                        fclose($this->fp);
+                       unset($this->fp);
                }
                $this->closed = true;
        }
index f3d06ce..5b262d7 100644 (file)
@@ -182,7 +182,9 @@ class LoggerAppenderRollingFile extends LoggerAppenderFile {
        protected function write($string) {
                // Lazy file open
                if(!isset($this->fp)) {
-                       $this->openFile();
+                       if ($this->openFile() === false) {
+                               return; // Do not write if file open failed.
+                       }
                }
                
                // Lock the file while writing and possible rolling over