1 8 package org.jivesoftware.util.log.output.io.rotate; 9 10 import org.jivesoftware.util.log.format.Formatter; 11 import org.jivesoftware.util.log.output.io.FileTarget; 12 import java.io.File ; 13 import java.io.IOException ; 14 15 22 public class RotatingFileTarget extends FileTarget { 23 24 private RotateStrategy m_rotateStrategy; 26 27 private FileStrategy m_fileStrategy; 29 30 35 public RotatingFileTarget(final Formatter formatter, 36 final RotateStrategy rotateStrategy, 37 final FileStrategy fileStrategy) 38 throws IOException { 39 super(null, false, formatter); 40 41 m_rotateStrategy = rotateStrategy; 42 m_fileStrategy = fileStrategy; 43 44 getInitialFile(); 45 } 46 47 public synchronized void rotate() 48 throws IOException { 49 close(); 50 51 final File file = m_fileStrategy.nextFile(); 52 setFile(file, false); 53 openFile(); 54 } 55 56 59 public synchronized void write(final String data) { 60 super.write(data); 62 63 final boolean rotate = 65 m_rotateStrategy.isRotationNeeded(data, getFile()); 66 if (rotate) { 67 try { 68 rotate(); 69 } 70 catch (final IOException ioe) { 71 getErrorHandler().error("Error rotating file", ioe, null); 72 } 73 } 74 } 75 76 private void getInitialFile() throws IOException { 77 close(); 78 79 boolean rotate = m_rotateStrategy.isRotationNeeded("", m_fileStrategy.currentFile()); 80 81 if (rotate) { 82 setFile(m_fileStrategy.nextFile(), false); 83 } 84 else { 85 setFile(m_fileStrategy.currentFile(), true); 86 } 87 88 openFile(); 89 } 90 } | Popular Tags |