1 17 package org.apache.log.output.io.rotate; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import org.apache.log.format.Formatter; 22 import org.apache.log.output.io.FileTarget; 23 24 31 public class RotatingFileTarget 32 extends FileTarget 33 { 34 private boolean m_append; 36 37 private RotateStrategy m_rotateStrategy; 39 40 private FileStrategy m_fileStrategy; 42 43 51 public RotatingFileTarget( final Formatter formatter, 52 final RotateStrategy rotateStrategy, 53 final FileStrategy fileStrategy ) 54 throws IOException 55 { 56 this( false, formatter, rotateStrategy, fileStrategy ); 57 } 58 59 68 public RotatingFileTarget( final boolean append, 69 final Formatter formatter, 70 final RotateStrategy rotateStrategy, 71 final FileStrategy fileStrategy ) 72 throws IOException 73 { 74 super( null, append, formatter ); 75 76 m_append = append; 77 m_rotateStrategy = rotateStrategy; 78 m_fileStrategy = fileStrategy; 79 80 rotate(); 81 } 82 83 87 protected synchronized void rotate() 88 throws IOException 89 { 90 close(); 91 92 final File file = m_fileStrategy.nextFile(); 93 setFile( file, m_append ); 94 openFile(); 95 } 96 97 101 protected synchronized void write( final String data ) 102 { 103 if( m_rotateStrategy.isRotationNeeded( data, getFile() ) ) 105 { 106 try 107 { 108 rotate(); 109 110 m_rotateStrategy.reset(); 111 } 112 catch( final IOException ioe ) 113 { 114 getErrorHandler().error( "Error rotating file", ioe, null ); 115 } 116 } 117 118 super.write( data ); 120 } 121 } 122 123 | Popular Tags |