1 16 package org.jboss.logging.jdk.handlers; 17 18 import java.io.IOException ; 19 import java.io.FileOutputStream ; 20 import java.io.FileNotFoundException ; 21 import java.io.File ; 22 import java.io.Writer ; 23 import java.io.BufferedWriter ; 24 import java.util.logging.Formatter ; 25 import java.util.logging.ErrorManager ; 26 27 34 public class FileHandler extends WriterHandler 35 { 36 45 protected boolean fileAppend = true; 46 47 50 protected String fileName = null; 51 52 55 public FileHandler() 56 { 57 } 58 59 71 public FileHandler(Formatter layout, String filename, boolean append, boolean bufferedIO, 72 int bufferSize) 73 throws IOException 74 { 75 super.setFormatter(layout); 76 this.setFile(filename, append, bufferedIO, bufferSize); 77 } 78 79 88 public FileHandler(Formatter layout, String filename, boolean append) 89 throws IOException 90 { 91 this(layout, filename, append, true, 2048); 92 } 93 94 101 public FileHandler(Formatter layout, String filename) throws IOException 102 { 103 this(layout, filename, true); 104 } 105 106 113 public void setFile(String file) 114 { 115 String val = file.trim(); 118 fileName = val; 119 } 120 121 124 public boolean getAppend() 125 { 126 return fileAppend; 127 } 128 129 130 133 public String getFile() 134 { 135 return fileName; 136 } 137 138 144 public void activateOptions() 145 { 146 if (fileName != null) 147 { 148 try 149 { 150 setFile(fileName, fileAppend, bufferedIO, bufferSize); 151 } 152 catch (java.io.IOException e) 153 { 154 reportError("setFile(" + fileName + "," + fileAppend + ") call failed.", 155 e, ErrorManager.OPEN_FAILURE); 156 } 157 } 158 else 159 { 160 reportError("File option not set for appender [" + name + "]." 161 +" Are you using FileHandler instead of ConsoleAppender?", 162 null, ErrorManager.OPEN_FAILURE); 163 } 164 } 165 166 176 public void setAppend(boolean flag) 177 { 178 fileAppend = flag; 179 } 180 181 196 public synchronized void setFile(String fileName, boolean append, boolean bufferedIO, int bufferSize) 197 throws IOException 198 { 199 super.setBufferedIO(bufferedIO); 201 super.setBufferSize(bufferSize); 202 FileOutputStream ostream = null; 203 try 204 { 205 ostream = new FileOutputStream (fileName, append); 207 } 208 catch (FileNotFoundException ex) 209 { 210 String parentName = new File (fileName).getParent(); 213 if (parentName != null) 214 { 215 File parentDir = new File (parentName); 216 if (!parentDir.exists() && parentDir.mkdirs()) 217 { 218 ostream = new FileOutputStream (fileName, append); 219 } 220 else 221 { 222 throw ex; 223 } 224 } 225 else 226 { 227 throw ex; 228 } 229 } 230 super.setOutputStream(ostream); 231 this.fileName = fileName; 232 this.fileAppend = append; 233 } 235 236 } 237 238 | Popular Tags |