Author: grobmeier
Date: Fri Aug 21 18:44:48 2009
New Revision: 806673

URL: http://svn.apache.org/viewvc?rev=806673&view=rev
Log:
LOG4PHP-71: fixed performance issue: Using LoggerAppenderFile logging to the 
log file in one Apache session blocks every other Apache session that tries to 
write to the file until the original request has been processed

Modified:
    incubator/log4php/trunk/src/changes/changes.xml
    incubator/log4php/trunk/src/main/php/appenders/LoggerAppenderFile.php

Modified: incubator/log4php/trunk/src/changes/changes.xml
URL: 
http://svn.apache.org/viewvc/incubator/log4php/trunk/src/changes/changes.xml?rev=806673&r1=806672&r2=806673&view=diff
==============================================================================
--- incubator/log4php/trunk/src/changes/changes.xml (original)
+++ incubator/log4php/trunk/src/changes/changes.xml Fri Aug 21 18:44:48 2009
@@ -69,6 +69,7 @@
                <action type="fix" issue="LOG4PHP-65">Mixing protected and 
private in Logger and the inheriting LoggerRoot (Christian Grobmeier)</action>
                <action type="fix" 
issue="LOG4PHP-66">LoggerConfiguratorBasicTest fails in "mvn test" but not in 
phpunit (Christian Hammers)</action>
                <action type="fix" issue="LOG4PHP-67">Refactoring: Use Logger 
instead of LoggerManager (Christian Hammers)</action>
+               <action type="fix" issue="LOG4PHP-71">Using LoggerAppenderFile 
logging to the log file in one Apache session blocks every other Apache session 
that tries to write to the file until the original request has been processed 
(Christian Grobmeier)</action>
                <action type="update">Initial port to PHP 5 (Knut 
Urdalen)</action>
                <action type="update">Established new unit test suite (Knut 
Urdalen)</action>
                <action type="update">Added a range of examples (Knut 
Urdalen)</action>

Modified: incubator/log4php/trunk/src/main/php/appenders/LoggerAppenderFile.php
URL: 
http://svn.apache.org/viewvc/incubator/log4php/trunk/src/main/php/appenders/LoggerAppenderFile.php?rev=806673&r1=806672&r2=806673&view=diff
==============================================================================
--- incubator/log4php/trunk/src/main/php/appenders/LoggerAppenderFile.php 
(original)
+++ incubator/log4php/trunk/src/main/php/appenders/LoggerAppenderFile.php Fri 
Aug 21 18:44:48 2009
@@ -67,8 +67,9 @@
                                        fseek($this->fp, 0, SEEK_END);
                                }
                                fwrite($this->fp, $this->layout->getHeader());
+                               flock($this->fp, LOCK_UN);
                                $this->closed = false;
-                       } else { // race condition, unable to lock file
+                       } else {
                                // TODO: should we take some action in this 
case?
                                $this->closed = true;
                        }                
@@ -79,57 +80,27 @@
        
        public function close() {
                if($this->fp and $this->layout !== null) {
-                       fwrite($this->fp, $this->layout->getFooter());
-               }
-                       
-               $this->closeFile();
-               $this->closed = true;
-       }
-
-       /**
-        * Closes the previously opened file.
-        */
-       public function closeFile() {
-               if($this->fp) {
+                       if(flock($this->fp, LOCK_EX)) {
+                               fwrite($this->fp, $this->layout->getFooter());
+                               flock($this->fp, LOCK_UN);
+                       }
                        fclose($this->fp);
                }
-       }
-       
-       /**
-        * @return boolean
-        */
-       public function getAppend() {
-               return $this->append;
+               $this->closed = true;
        }
 
-       /**
-        * @return string
-        */
-       public function getFile() {
-               return $this->getFileName();
+       public function append($event) {
+               if($this->fp and $this->layout !== null) {
+                       if(flock($this->fp, LOCK_EX)) {
+                               fwrite($this->fp, 
$this->layout->format($event));
+                               flock($this->fp, LOCK_UN);
+                       } else {
+                               $this->closed = true;
+                       }
+               } 
        }
        
        /**
-        * @return string
-        */
-       public function getFileName() {
-               return $this->fileName;
-       } 
- 
-       /**
-        * Close any previously opened file and call the parent's reset.
-        */
-       public function reset() {
-               $this->closeFile();
-               $this->fileName = null;
-               parent::reset();
-       }
-
-       public function setAppend($flag) {
-               $this->append = LoggerOptionConverter::toBoolean($flag, true);  
          
-       } 
-  
-       /**
         * Sets and opens the file where the log output will go.
         *
         * This is an overloaded method. It can be called with:
@@ -148,13 +119,32 @@
                }
        }
        
-       public function setFileName($fileName) {
-               $this->fileName = $fileName;
+       /**
+        * @return string
+        */
+       public function getFile() {
+               return $this->getFileName();
+       }
+       
+       /**
+        * @return boolean
+        */
+       public function getAppend() {
+               return $this->append;
        }
 
-       public function append($event) {
-               if($this->fp and $this->layout !== null) {
-                       fwrite($this->fp, $this->layout->format($event));
-               } 
+       public function setAppend($flag) {
+               $this->append = LoggerOptionConverter::toBoolean($flag, true);  
          
+       }
+
+       public function setFileName($fileName) {
+               $this->fileName = $fileName;
        }
+       
+       /**
+        * @return string
+        */
+       public function getFileName() {
+               return $this->fileName;
+       } 
 }


Reply via email to