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 import com.protomatter.util.*; 59 60 66 67 public class TimeRolloverLog 68 extends BasicLogger 69 { 70 73 public static int ROLL_MINUTELY = 0; 74 75 78 public static int ROLL_HOURLY = 1; 79 80 83 public static int ROLL_DAILY = 2; 84 85 88 public static int ROLL_MONTHLY = 3; 89 90 93 public static int ROLL_WEEKLY = 4; 94 95 private Writer out = null; 96 private DateFormat namefmt; private Object syncObject = new Object (); 98 private String basename; 99 private long nextRolloverTime = 0; 100 private int rolltype; 101 private boolean append = true; 102 private String extension = ""; 103 private String nameFormatString; 104 private boolean autoFlush = true; 105 private File currentFile = null; 106 private File previousFile = null; 107 108 private int weeklyRollDay = Calendar.MONDAY; 110 111 114 public int getRollType() 115 { 116 return this.rolltype; 117 } 118 119 122 public void setRollType(int type) 123 { 124 this.rolltype = type; 125 } 126 127 130 public int getWeeklyRollDay() 131 { 132 return this.weeklyRollDay; 133 } 134 135 139 public void setWeeklyRollDay(int day) 140 { 141 if ( (day != Calendar.SUNDAY) && 142 (day != Calendar.MONDAY) && 143 (day != Calendar.TUESDAY) && 144 (day != Calendar.WEDNESDAY) && 145 (day != Calendar.THURSDAY) && 146 (day != Calendar.FRIDAY) && 147 (day != Calendar.SATURDAY)) 148 { 149 throw new IllegalArgumentException (MessageFormat.format( 150 Syslog.getResourceString(MessageConstants.TIMEROLLOVERLOG_BAD_ROLL_DAY_VALUE_MESSAGE), 151 new Object [] { "Calendar.MONDAY", "Calendar.SUNDAY" })); 152 } 153 this.weeklyRollDay = day; 154 } 155 156 159 public String getBaseFilename() 160 { 161 return this.basename; 162 } 163 164 167 public void setBaseFilename(String basename) 168 { 169 this.basename = basename; 170 } 171 172 175 public String getFileExtension() 176 { 177 return this.extension; 178 } 179 180 183 public void setFileExtension(String extension) 184 { 185 this.extension = extension; 186 } 187 188 191 public void setAppend(boolean append) 192 { 193 this.append = append; 194 } 195 196 199 public boolean getAppend() 200 { 201 return this.append; 202 } 203 204 207 public boolean getAutoFlush() 208 { 209 return this.autoFlush; 210 } 211 212 215 public void setAutoFlush(boolean flush) 216 { 217 this.autoFlush = flush; 218 } 219 220 225 public TimeRolloverLog(String basename, int roll, String extension) 226 { 227 this(basename, extension, roll, "yyyy.MM.dd-HH.mm.ss", true, false); 228 } 229 230 235 public TimeRolloverLog(String basename, String extension, int roll, String nameformat, boolean append, boolean autoFlush) 236 { 237 this(); 238 239 if (roll < 0 || roll > 4) 240 throw new IllegalArgumentException (MessageFormat.format( 241 Syslog.getResourceString(MessageConstants.TIMEROLLOVERLOG_BAD_ROLL_VALUE_MESSAGE), 242 new Object [] { "0", "4" })); 243 244 this.basename = basename; 245 this.extension = extension; 246 this.rolltype = roll; 247 setNameFormat(nameformat); 248 this.append = append; 249 this.autoFlush = autoFlush; 250 rollover(new Date()); 251 } 252 253 257 public TimeRolloverLog() 258 { 259 super(); 260 } 261 262 265 private void rollover() 266 { 267 if (nextRolloverTime == 0) 268 nextRolloverTime = getNextRolloverTime().getTime(); 269 270 rollover(new Date(nextRolloverTime)); 271 } 272 273 276 public File getPreviousFile() 277 { 278 return this.previousFile; 279 } 280 281 284 public File getCurrentFile() 285 { 286 return this.currentFile; 287 } 288 289 292 public void rollover(Date d) 293 { 294 synchronized(syncObject) 295 { 296 if (out != null) 297 { 298 try 299 { 300 out.write(formatter.getLogFooter()); 301 } 302 catch (IOException x) 303 { 304 x.printStackTrace(); 305 } 306 307 } 308 resetDateFormat(); 309 Writer old = out; 310 311 try 312 { 313 File f = new File(basename + namefmt.format(d) + extension); 314 out = new BufferedWriter( 315 new FileWriter(f.getCanonicalPath(), append)); 316 out.write(formatter.getLogHeader()); 317 318 if (old != null) 319 { 320 old.flush(); 321 old.close(); 322 } 323 324 this.previousFile = this.currentFile; 325 this.currentFile = f; 326 } 327 catch (IOException x) 328 { 329 System.err.println(MessageFormat.format( 330 Syslog.getResourceString(MessageConstants.TIMEROLLOVER_CANNOT_ROLL_MESSAGE), 331 new Object [] { "IOException" })); 332 x.printStackTrace(); 333 out = old; 334 } 335 336 Date date = getNextRolloverTime(); 337 nextRolloverTime = date.getTime(); } 339 } 340 341 344 public void setNameFormat(String fmt) 345 { 346 this.nameFormatString = fmt; 347 namefmt = new SimpleDateFormat(fmt); 348 namefmt.setTimeZone(TimeZone.getDefault()); 349 } 350 351 354 public String getNameFormat() 355 { 356 return this.nameFormatString; 357 } 358 359 public final void log(SyslogMessage message) 360 { 361 StringBuffer b = 362 (message.detail == null) ? 363 (new StringBuffer (128)) : 364 (new StringBuffer (256)); 365 formatLogEntry(b, message); 366 367 synchronized (syncObject) 368 { 369 if (message.time >= nextRolloverTime) 370 rollover(); 371 372 try 373 { 374 out.write(b.toString()); 375 376 if (autoFlush) 377 out.flush(); 378 } 379 catch (IOException x) 380 { 381 x.printStackTrace(); 382 } 383 384 } 385 } 386 387 391 public Date getNextRolloverTime() 392 { 393 GregorianCalendar now = new GregorianCalendar(); 394 GregorianCalendar c = null; 395 396 if (rolltype == ROLL_MINUTELY) 397 { 398 now.add(Calendar.MINUTE, 1); 399 c = new GregorianCalendar( 400 now.get(Calendar.YEAR), 401 now.get(Calendar.MONTH), 402 now.get(Calendar.DATE), 403 now.get(Calendar.HOUR_OF_DAY), 404 now.get(Calendar.MINUTE) 405 ); 406 } 407 else if (rolltype == ROLL_HOURLY) 408 { 409 now.add(Calendar.HOUR_OF_DAY, 1); 410 c = new GregorianCalendar( 411 now.get(Calendar.YEAR), 412 now.get(Calendar.MONTH), 413 now.get(Calendar.DATE), 414 now.get(Calendar.HOUR_OF_DAY), 415 0 416 ); 417 } 418 else if (rolltype == ROLL_DAILY) 419 { 420 now.add(Calendar.DATE, 1); 421 c = new GregorianCalendar( 422 now.get(Calendar.YEAR), 423 now.get(Calendar.MONTH), 424 now.get(Calendar.DATE), 425 0, 426 0 427 ); 428 } 429 else if (rolltype == ROLL_MONTHLY) 430 { 431 now.add(Calendar.MONTH, 1); 432 c = new GregorianCalendar( 433 now.get(Calendar.YEAR), 434 now.get(Calendar.MONTH), 435 1, 436 0, 437 0 438 ); 439 } 440 else if (rolltype == ROLL_WEEKLY) 441 { 442 while (now.get(Calendar.DAY_OF_WEEK) != weeklyRollDay) 445 { 446 now.add(Calendar.DATE, 1); 447 } 448 c = new GregorianCalendar( 449 now.get(Calendar.YEAR), 450 now.get(Calendar.MONTH), 451 now.get(Calendar.DATE), 452 0, 453 0 454 ); 455 if (c.getTime().before(new Date())) 456 { 457 c.add(Calendar.DATE, 7); 458 } 459 } 460 461 return c.getTime(); 462 } 463 464 467 public void shutdown() 468 { 469 if (out != null) 470 { 471 try 472 { 473 out.write(formatter.getLogFooter()); 474 out.flush(); 475 out.close(); 476 out = null; 477 } 478 catch (IOException x) 479 { 480 x.printStackTrace(); 481 } 482 483 } 484 } 485 486 public void flush() 487 { 488 try 489 { 490 if (out != null) 491 out.flush(); 492 } 493 catch (IOException x) 494 { 495 x.printStackTrace(); 496 } 497 } 498 } 499 | Popular Tags |