1 package com.protomatter.syslog; 2 3 52 53 import java.io.*; 54 import java.net.*; 55 import java.util.*; 56 import java.util.zip.*; 57 import java.text.*; 58 59 import com.protomatter.util.*; 60 61 68 public class LengthRolloverLog 69 extends BasicLogger 70 { 71 private Writer out = null; 72 private Object syncObject = new Object (); 73 private int rolllength = 1048576; private int written; 75 private String basename; 76 private String extension; 77 private boolean append = true; 78 private boolean autoFlush = true; 79 private File currentFile = null; 80 private File previousFile = null; 81 82 90 public LengthRolloverLog(String basename, String extension, int roll, boolean append, boolean autoFlush) 91 { 92 this(); 93 this.basename = basename; 94 this.extension = extension; 95 this.rolllength = roll; 96 this.append = append; 97 this.autoFlush = autoFlush; 98 rollover(); 99 } 100 101 105 public LengthRolloverLog() 106 { 107 super(); 108 } 109 110 113 public void setRollLength(int rolllength) 114 { 115 this.rolllength = rolllength; 116 } 117 120 public int getRollLength() 121 { 122 return this.rolllength; 123 } 124 125 128 public void setBasename(String basename) 129 { 130 this.basename = basename; 131 } 132 135 public String getBasename() 136 { 137 return this.basename; 138 } 139 140 143 public void setExtension(String extension) 144 { 145 this.extension = extension; 146 } 147 150 public String getExtension() 151 { 152 return this.extension; 153 } 154 155 158 public void setAppend(boolean append) 159 { 160 this.append = append; 161 } 162 165 public boolean getAppend() 166 { 167 return this.append; 168 } 169 170 173 public void setAutoFlush(boolean autoFlush) 174 { 175 this.autoFlush = autoFlush; 176 } 177 180 public boolean getAutoFlush() 181 { 182 return this.autoFlush; 183 } 184 185 188 public File getCurrentFile() 189 { 190 return this.currentFile; 191 } 192 193 196 public File getPreviousFile() 197 { 198 return this.previousFile; 199 } 200 201 204 public void rollover() 205 { 206 synchronized(syncObject) 207 { 208 if (out != null) 209 { 210 try 211 { 212 out.write(formatter.getLogFooter()); 213 } 214 catch (IOException x) 215 { 216 x.printStackTrace(); 217 } 218 } 219 resetDateFormat(); 220 Writer old = out; 221 try 222 { 223 int i; 224 File f = null; 225 for (i=1; (new File(basename + i + extension)).exists(); i++); 226 f = new File(basename + i + extension); 227 out = new BufferedWriter( 228 new FileWriter(f.getCanonicalPath(), append)); 229 out.write(formatter.getLogHeader()); 230 written = 0; 231 232 if (old != null) 233 { 234 old.flush(); 235 old.close(); 236 } 237 this.previousFile = this.currentFile; 238 this.currentFile = f; 239 } 240 catch (IOException x) 241 { 242 System.err.println(MessageFormat.format( 243 Syslog.getResourceString(MessageConstants.LENGTHRO_CANNOT_ROLL_MESSAGE), 244 new Object [] { "IOException" } )); 245 x.printStackTrace(); 246 out = old; 247 } 248 } 249 } 250 251 public final void log(SyslogMessage message) 252 { 253 StringBuffer b = new StringBuffer (128); 254 formatLogEntry(b, message); 255 int length = b.length(); 256 synchronized(syncObject) 257 { 258 if ((length + written) > rolllength) 259 rollover(); 260 try 261 { 262 out.write(b.toString()); 263 if (autoFlush) 264 out.flush(); 265 written += length; 266 } 267 catch (IOException x) 268 { 269 x.printStackTrace(); 270 } 271 } 272 } 273 274 public void flush() 275 { 276 try 277 { 278 if (out != null) 279 out.flush(); 280 } 281 catch (IOException x) 282 { 283 x.printStackTrace(); 284 } 285 } 286 287 public void shutdown() 288 { 289 if (out != null) 290 { 291 try 292 { 293 out.write(formatter.getLogFooter()); 294 out.flush(); 295 out.close(); 296 out = null; 297 } 298 catch (IOException x) 299 { 300 x.printStackTrace(); 301 } 302 } 303 } 304 } 305 | Popular Tags |