You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by ca...@apache.org on 2005/07/22 19:59:04 UTC

cvs commit: logging-log4j/tests/src/java/org/apache/log4j FileAppenderTest.java

carnold     2005/07/22 10:59:04

  Modified:    src/java/org/apache/log4j FileAppender.java
               tests/src/java/org/apache/log4j FileAppenderTest.java
  Log:
  Bug 9150: No check in FileAppender if parent-path exists
  
  Revision  Changes    Path
  1.54      +20 -1     logging-log4j/src/java/org/apache/log4j/FileAppender.java
  
  Index: FileAppender.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/FileAppender.java,v
  retrieving revision 1.53
  retrieving revision 1.54
  diff -u -r1.53 -r1.54
  --- FileAppender.java	23 May 2005 23:51:01 -0000	1.53
  +++ FileAppender.java	22 Jul 2005 17:59:03 -0000	1.54
  @@ -261,7 +261,26 @@
   
       closeWriter();
   
  -    this.writer = createWriter(new FileOutputStream(filename, append));
  +    FileOutputStream ostream = null;
  +    try {
  +        //
  +        //   attempt to create file
  +        //
  +        ostream = new FileOutputStream(filename, append);
  +    } catch(FileNotFoundException ex) {
  +        //
  +        //   if parent directory does not exist then
  +        //      attempt to create it and try to create file
  +        //      see bug 9150
  +        //
  +        File parentDir = new File(new File(filename).getParent());
  +        if(!parentDir.exists() && parentDir.mkdirs()) {
  +            ostream = new FileOutputStream(filename, append);
  +        } else {
  +            throw ex;
  +        }
  +    }
  +    this.writer = createWriter(ostream);
   
       if (bufferedIO) {
         this.writer = new BufferedWriter(this.writer, bufferSize);
  
  
  
  1.6       +21 -0     logging-log4j/tests/src/java/org/apache/log4j/FileAppenderTest.java
  
  Index: FileAppenderTest.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/tests/src/java/org/apache/log4j/FileAppenderTest.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- FileAppenderTest.java	8 Mar 2005 22:32:57 -0000	1.5
  +++ FileAppenderTest.java	22 Jul 2005 17:59:03 -0000	1.6
  @@ -15,6 +15,7 @@
    */
   
   package org.apache.log4j;
  +import java.io.File;
   
   
   /**
  @@ -22,6 +23,7 @@
    * Test if WriterAppender honors the Appender contract.
    *
    * @author <a href="http://www.qos.ch/log4j/">Ceki G&uuml;lc&uuml;</a>
  + * @author Curt Arnold
    */
   public class FileAppenderTest extends AbstractAppenderTest {
     protected AppenderSkeleton getAppender() {
  @@ -44,4 +46,23 @@
       wa2.setLayout(new DummyLayout());
       assertFalse(wa2.isActive());
     }
  +
  +  /**
  +   * Tests that any necessary directories are attempted to
  +   * be created if they don't exist.  See bug 9150.
  +   *
  +   */
  +  public void testDirectoryCreation() {
  +      File newFile = new File("output/newdir/temp.log");
  +      newFile.delete();
  +      File newDir = new File("output/newdir");
  +      newDir.delete();
  +
  +      FileAppender wa = new FileAppender();
  +      wa.setFile("output/newdir/temp.log");
  +      wa.setLayout(new DummyLayout());
  +      wa.activateOptions();
  +
  +      assertTrue(new File("output/newdir/temp.log").exists());
  +  }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org