1 /* 2 * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.util.logging; 27 28 import static java.nio.file.StandardOpenOption.CREATE_NEW; 29 import static java.nio.file.StandardOpenOption.WRITE; 30 31 import java.io.BufferedOutputStream; 32 import java.io.File; 33 import java.io.FileOutputStream; 34 import java.io.IOException; 35 import java.io.OutputStream; 36 import java.nio.channels.FileChannel; 37 import java.nio.file.FileAlreadyExistsException; 38 import java.nio.file.Paths; 39 import java.security.AccessController; 40 import java.security.PrivilegedAction; 41 42 /** 43 * Simple file logging <tt>Handler</tt>. 44 * <p> 45 * The <tt>FileHandler</tt> can either write to a specified file, 46 * or it can write to a rotating set of files. 47 * <p> 48 * For a rotating set of files, as each file reaches a given size 49 * limit, it is closed, rotated out, and a new file opened. 50 * Successively older files are named by adding "0", "1", "2", 51 * etc. into the base filename. 52 * <p> 53 * By default buffering is enabled in the IO libraries but each log 54 * record is flushed out when it is complete. 55 * <p> 56 * By default the <tt>XMLFormatter</tt> class is used for formatting. 57 * <p> 58 * <b>Configuration:</b> 59 * By default each <tt>FileHandler</tt> is initialized using the following 60 * <tt>LogManager</tt> configuration properties where <tt><handler-name></tt> 61 * refers to the fully-qualified class name of the handler. 62 * If properties are not defined 63 * (or have invalid values) then the specified default values are used. 64 * <ul> 65 * <li> <handler-name>.level 66 * specifies the default level for the <tt>Handler</tt> 67 * (defaults to <tt>Level.ALL</tt>). </li> 68 * <li> <handler-name>.filter 69 * specifies the name of a <tt>Filter</tt> class to use 70 * (defaults to no <tt>Filter</tt>). </li> 71 * <li> <handler-name>.formatter 72 * specifies the name of a <tt>Formatter</tt> class to use 73 * (defaults to <tt>java.util.logging.XMLFormatter</tt>) </li> 74 * <li> <handler-name>.encoding 75 * the name of the character set encoding to use (defaults to 76 * the default platform encoding). </li> 77 * <li> <handler-name>.limit 78 * specifies an approximate maximum amount to write (in bytes) 79 * to any one file. If this is zero, then there is no limit. 80 * (Defaults to no limit). </li> 81 * <li> <handler-name>.count 82 * specifies how many output files to cycle through (defaults to 1). </li> 83 * <li> <handler-name>.pattern 84 * specifies a pattern for generating the output file name. See 85 * below for details. (Defaults to "%h/java%u.log"). </li> 86 * <li> <handler-name>.append 87 * specifies whether the FileHandler should append onto 88 * any existing files (defaults to false). </li> 89 * </ul> 90 * <p> 91 * For example, the properties for {@code FileHandler} would be: 92 * <ul> 93 * <li> java.util.logging.FileHandler.level=INFO </li> 94 * <li> java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter </li> 95 * </ul> 96 * <p> 97 * For a custom handler, e.g. com.foo.MyHandler, the properties would be: 98 * <ul> 99 * <li> com.foo.MyHandler.level=INFO </li> 100 * <li> com.foo.MyHandler.formatter=java.util.logging.SimpleFormatter </li> 101 * </ul> 102 * <p> 103 * A pattern consists of a string that includes the following special 104 * components that will be replaced at runtime: 105 * <ul> 106 * <li> "/" the local pathname separator </li> 107 * <li> "%t" the system temporary directory </li> 108 * <li> "%h" the value of the "user.home" system property </li> 109 * <li> "%g" the generation number to distinguish rotated logs </li> 110 * <li> "%u" a unique number to resolve conflicts </li> 111 * <li> "%%" translates to a single percent sign "%" </li> 112 * </ul> 113 * If no "%g" field has been specified and the file count is greater 114 * than one, then the generation number will be added to the end of 115 * the generated filename, after a dot. 116 * <p> 117 * Thus for example a pattern of "%t/java%g.log" with a count of 2 118 * would typically cause log files to be written on Solaris to 119 * /var/tmp/java0.log and /var/tmp/java1.log whereas on Windows 95 they 120 * would be typically written to C:\TEMP\java0.log and C:\TEMP\java1.log 121 * <p> 122 * Generation numbers follow the sequence 0, 1, 2, etc. 123 * <p> 124 * Normally the "%u" unique field is set to 0. However, if the <tt>FileHandler</tt> 125 * tries to open the filename and finds the file is currently in use by 126 * another process it will increment the unique number field and try 127 * again. This will be repeated until <tt>FileHandler</tt> finds a file name that 128 * is not currently in use. If there is a conflict and no "%u" field has 129 * been specified, it will be added at the end of the filename after a dot. 130 * (This will be after any automatically added generation number.) 131 * <p> 132 * Thus if three processes were all trying to log to fred%u.%g.txt then 133 * they might end up using fred0.0.txt, fred1.0.txt, fred2.0.txt as 134 * the first file in their rotating sequences. 135 * <p> 136 * Note that the use of unique ids to avoid conflicts is only guaranteed 137 * to work reliably when using a local disk file system. 138 * 139 * @since 1.4 140 */ 141 142 public class FileHandler extends StreamHandler { 143 private MeteredStream meter; 144 private boolean append; 145 private int limit; // zero => no limit. 146 private int count; 147 private String pattern; 148 private String lockFileName; 149 private FileChannel lockFileChannel; 150 private File files[]; 151 private static final int MAX_LOCKS = 100; 152 private static final java.util.HashMap<String, String> locks = new java.util.HashMap<>(); 153 154 /** 155 * A metered stream is a subclass of OutputStream that 156 * (a) forwards all its output to a target stream 157 * (b) keeps track of how many bytes have been written 158 */ 159 private class MeteredStream extends OutputStream { 160 final OutputStream out; 161 int written; 162 163 MeteredStream(OutputStream out, int written) { 164 this.out = out; 165 this.written = written; 166 } 167 168 @Override 169 public void write(int b) throws IOException { 170 out.write(b); 171 written++; 172 } 173 174 @Override 175 public void write(byte buff[]) throws IOException { 176 out.write(buff); 177 written += buff.length; 178 } 179 180 @Override 181 public void write(byte buff[], int off, int len) throws IOException { 182 out.write(buff,off,len); 183 written += len; 184 } 185 186 @Override 187 public void flush() throws IOException { 188 out.flush(); 189 } 190 191 @Override 192 public void close() throws IOException { 193 out.close(); 194 } 195 } 196 197 private void open(File fname, boolean append) throws IOException { 198 int len = 0; 199 if (append) { 200 len = (int)fname.length(); 201 } 202 FileOutputStream fout = new FileOutputStream(fname.toString(), append); 203 BufferedOutputStream bout = new BufferedOutputStream(fout); 204 meter = new MeteredStream(bout, len); 205 setOutputStream(meter); 206 } 207 208 /** 209 * Configure a FileHandler from LogManager properties and/or default values 210 * as specified in the class javadoc. 211 */ 212 private void configure() { 213 LogManager manager = LogManager.getLogManager(); 214 215 String cname = getClass().getName(); 216 217 pattern = manager.getStringProperty(cname + ".pattern", "%h/java%u.log"); 218 limit = manager.getIntProperty(cname + ".limit", 0); 219 if (limit < 0) { 220 limit = 0; 221 } 222 count = manager.getIntProperty(cname + ".count", 1); 223 if (count <= 0) { 224 count = 1; 225 } 226 append = manager.getBooleanProperty(cname + ".append", false); 227 setLevel(manager.getLevelProperty(cname + ".level", Level.ALL)); 228 setFilter(manager.getFilterProperty(cname + ".filter", null)); 229 setFormatter(manager.getFormatterProperty(cname + ".formatter", new XMLFormatter())); 230 try { 231 setEncoding(manager.getStringProperty(cname +".encoding", null)); 232 } catch (Exception ex) { 233 try { 234 setEncoding(null); 235 } catch (Exception ex2) { 236 // doing a setEncoding with null should always work. 237 // assert false; 238 } 239 } 240 } 241 242 243 /** 244 * Construct a default <tt>FileHandler</tt>. This will be configured 245 * entirely from <tt>LogManager</tt> properties (or their default values). 246 * <p> 247 * @exception IOException if there are IO problems opening the files. 248 * @exception SecurityException if a security manager exists and if 249 * the caller does not have <tt>LoggingPermission("control"))</tt>. 250 * @exception NullPointerException if pattern property is an empty String. 251 */ 252 public FileHandler() throws IOException, SecurityException { 253 checkPermission(); 254 configure(); 255 openFiles(); 256 } 257 258 /** 259 * Initialize a <tt>FileHandler</tt> to write to the given filename. 260 * <p> 261 * The <tt>FileHandler</tt> is configured based on <tt>LogManager</tt> 262 * properties (or their default values) except that the given pattern 263 * argument is used as the filename pattern, the file limit is 264 * set to no limit, and the file count is set to one. 265 * <p> 266 * There is no limit on the amount of data that may be written, 267 * so use this with care. 268 * 269 * @param pattern the name of the output file 270 * @exception IOException if there are IO problems opening the files. 271 * @exception SecurityException if a security manager exists and if 272 * the caller does not have <tt>LoggingPermission("control")</tt>. 273 * @exception IllegalArgumentException if pattern is an empty string 274 */ 275 public FileHandler(String pattern) throws IOException, SecurityException { 276 if (pattern.length() < 1 ) { 277 throw new IllegalArgumentException(); 278 } 279 checkPermission(); 280 configure(); 281 this.pattern = pattern; 282 this.limit = 0; 283 this.count = 1; 284 openFiles(); 285 } 286 287 /** 288 * Initialize a <tt>FileHandler</tt> to write to the given filename, 289 * with optional append. 290 * <p> 291 * The <tt>FileHandler</tt> is configured based on <tt>LogManager</tt> 292 * properties (or their default values) except that the given pattern 293 * argument is used as the filename pattern, the file limit is 294 * set to no limit, the file count is set to one, and the append 295 * mode is set to the given <tt>append</tt> argument. 296 * <p> 297 * There is no limit on the amount of data that may be written, 298 * so use this with care. 299 * 300 * @param pattern the name of the output file 301 * @param append specifies append mode 302 * @exception IOException if there are IO problems opening the files. 303 * @exception SecurityException if a security manager exists and if 304 * the caller does not have <tt>LoggingPermission("control")</tt>. 305 * @exception IllegalArgumentException if pattern is an empty string 306 */ 307 public FileHandler(String pattern, boolean append) throws IOException, 308 SecurityException { 309 if (pattern.length() < 1 ) { 310 throw new IllegalArgumentException(); 311 } 312 checkPermission(); 313 configure(); 314 this.pattern = pattern; 315 this.limit = 0; 316 this.count = 1; 317 this.append = append; 318 openFiles(); 319 } 320 321 /** 322 * Initialize a <tt>FileHandler</tt> to write to a set of files. When 323 * (approximately) the given limit has been written to one file, 324 * another file will be opened. The output will cycle through a set 325 * of count files. 326 * <p> 327 * The <tt>FileHandler</tt> is configured based on <tt>LogManager</tt> 328 * properties (or their default values) except that the given pattern 329 * argument is used as the filename pattern, the file limit is 330 * set to the limit argument, and the file count is set to the 331 * given count argument. 332 * <p> 333 * The count must be at least 1. 334 * 335 * @param pattern the pattern for naming the output file 336 * @param limit the maximum number of bytes to write to any one file 337 * @param count the number of files to use 338 * @exception IOException if there are IO problems opening the files. 339 * @exception SecurityException if a security manager exists and if 340 * the caller does not have <tt>LoggingPermission("control")</tt>. 341 * @exception IllegalArgumentException if {@code limit < 0}, or {@code count < 1}. 342 * @exception IllegalArgumentException if pattern is an empty string 343 */ 344 public FileHandler(String pattern, int limit, int count) 345 throws IOException, SecurityException { 346 if (limit < 0 || count < 1 || pattern.length() < 1) { 347 throw new IllegalArgumentException(); 348 } 349 checkPermission(); 350 configure(); 351 this.pattern = pattern; 352 this.limit = limit; 353 this.count = count; 354 openFiles(); 355 } 356 357 /** 358 * Initialize a <tt>FileHandler</tt> to write to a set of files 359 * with optional append. When (approximately) the given limit has 360 * been written to one file, another file will be opened. The 361 * output will cycle through a set of count files. 362 * <p> 363 * The <tt>FileHandler</tt> is configured based on <tt>LogManager</tt> 364 * properties (or their default values) except that the given pattern 365 * argument is used as the filename pattern, the file limit is 366 * set to the limit argument, and the file count is set to the 367 * given count argument, and the append mode is set to the given 368 * <tt>append</tt> argument. 369 * <p> 370 * The count must be at least 1. 371 * 372 * @param pattern the pattern for naming the output file 373 * @param limit the maximum number of bytes to write to any one file 374 * @param count the number of files to use 375 * @param append specifies append mode 376 * @exception IOException if there are IO problems opening the files. 377 * @exception SecurityException if a security manager exists and if 378 * the caller does not have <tt>LoggingPermission("control")</tt>. 379 * @exception IllegalArgumentException if {@code limit < 0}, or {@code count < 1}. 380 * @exception IllegalArgumentException if pattern is an empty string 381 * 382 */ 383 public FileHandler(String pattern, int limit, int count, boolean append) 384 throws IOException, SecurityException { 385 if (limit < 0 || count < 1 || pattern.length() < 1) { 386 throw new IllegalArgumentException(); 387 } 388 checkPermission(); 389 configure(); 390 this.pattern = pattern; 391 this.limit = limit; 392 this.count = count; 393 this.append = append; 394 openFiles(); 395 } 396 397 /** 398 * Open the set of output files, based on the configured 399 * instance variables. 400 */ 401 private void openFiles() throws IOException { 402 LogManager manager = LogManager.getLogManager(); 403 manager.checkPermission(); 404 if (count < 1) { 405 throw new IllegalArgumentException("file count = " + count); 406 } 407 if (limit < 0) { 408 limit = 0; 409 } 410 411 // We register our own ErrorManager during initialization 412 // so we can record exceptions. 413 InitializationErrorManager em = new InitializationErrorManager(); 414 setErrorManager(em); 415 416 // Create a lock file. This grants us exclusive access 417 // to our set of output files, as long as we are alive. 418 int unique = -1; 419 for (;;) { 420 unique++; 421 if (unique > MAX_LOCKS) { 422 throw new IOException("Couldn't get lock for " + pattern); 423 } 424 // Generate a lock file name from the "unique" int. 425 lockFileName = generate(pattern, 0, unique).toString() + ".lck"; 426 // Now try to lock that filename. 427 // Because some systems (e.g., Solaris) can only do file locks 428 // between processes (and not within a process), we first check 429 // if we ourself already have the file locked. 430 synchronized(locks) { 431 if (locks.get(lockFileName) != null) { 432 // We already own this lock, for a different FileHandler 433 // object. Try again. 434 continue; 435 } 436 437 try { 438 lockFileChannel = FileChannel.open(Paths.get(lockFileName), 439 CREATE_NEW, WRITE); 440 } catch (FileAlreadyExistsException ix) { 441 // try the next lock file name in the sequence 442 continue; 443 } 444 445 boolean available; 446 try { 447 available = lockFileChannel.tryLock() != null; 448 // We got the lock OK. 449 } catch (IOException ix) { 450 // We got an IOException while trying to get the lock. 451 // This normally indicates that locking is not supported 452 // on the target directory. We have to proceed without 453 // getting a lock. Drop through. 454 available = true; 455 } 456 if (available) { 457 // We got the lock. Remember it. 458 locks.put(lockFileName, lockFileName); 459 break; 460 } 461 462 // We failed to get the lock. Try next file. 463 lockFileChannel.close(); 464 } 465 } 466 467 files = new File[count]; 468 for (int i = 0; i < count; i++) { 469 files[i] = generate(pattern, i, unique); 470 } 471 472 // Create the initial log file. 473 if (append) { 474 open(files[0], true); 475 } else { 476 rotate(); 477 } 478 479 // Did we detect any exceptions during initialization? 480 Exception ex = em.lastException; 481 if (ex != null) { 482 if (ex instanceof IOException) { 483 throw (IOException) ex; 484 } else if (ex instanceof SecurityException) { 485 throw (SecurityException) ex; 486 } else { 487 throw new IOException("Exception: " + ex); 488 } 489 } 490 491 // Install the normal default ErrorManager. 492 setErrorManager(new ErrorManager()); 493 } 494 495 /** 496 * Generate a file based on a user-supplied pattern, generation number, 497 * and an integer uniqueness suffix 498 * @param pattern the pattern for naming the output file 499 * @param generation the generation number to distinguish rotated logs 500 * @param unique a unique number to resolve conflicts 501 * @return the generated File 502 * @throws IOException 503 */ 504 private File generate(String pattern, int generation, int unique) 505 throws IOException { 506 File file = null; 507 String word = ""; 508 int ix = 0; 509 boolean sawg = false; 510 boolean sawu = false; 511 while (ix < pattern.length()) { 512 char ch = pattern.charAt(ix); 513 ix++; 514 char ch2 = 0; 515 if (ix < pattern.length()) { 516 ch2 = Character.toLowerCase(pattern.charAt(ix)); 517 } 518 if (ch == '/') { 519 if (file == null) { 520 file = new File(word); 521 } else { 522 file = new File(file, word); 523 } 524 word = ""; 525 continue; 526 } else if (ch == '%') { 527 if (ch2 == 't') { 528 String tmpDir = System.getProperty("java.io.tmpdir"); 529 if (tmpDir == null) { 530 tmpDir = System.getProperty("user.home"); 531 } 532 file = new File(tmpDir); 533 ix++; 534 word = ""; 535 continue; 536 } else if (ch2 == 'h') { 537 file = new File(System.getProperty("user.home")); 538 if (isSetUID()) { 539 // Ok, we are in a set UID program. For safety's sake 540 // we disallow attempts to open files relative to %h. 541 throw new IOException("can't use %h in set UID program"); 542 } 543 ix++; 544 word = ""; 545 continue; 546 } else if (ch2 == 'g') { 547 word = word + generation; 548 sawg = true; 549 ix++; 550 continue; 551 } else if (ch2 == 'u') { 552 word = word + unique; 553 sawu = true; 554 ix++; 555 continue; 556 } else if (ch2 == '%') { 557 word = word + "%"; 558 ix++; 559 continue; 560 } 561 } 562 word = word + ch; 563 } 564 if (count > 1 && !sawg) { 565 word = word + "." + generation; 566 } 567 if (unique > 0 && !sawu) { 568 word = word + "." + unique; 569 } 570 if (word.length() > 0) { 571 if (file == null) { 572 file = new File(word); 573 } else { 574 file = new File(file, word); 575 } 576 } 577 return file; 578 } 579 580 /** 581 * Rotate the set of output files 582 */ 583 private synchronized void rotate() { 584 Level oldLevel = getLevel(); 585 setLevel(Level.OFF); 586 587 super.close(); 588 for (int i = count-2; i >= 0; i--) { 589 File f1 = files[i]; 590 File f2 = files[i+1]; 591 if (f1.exists()) { 592 if (f2.exists()) { 593 f2.delete(); 594 } 595 f1.renameTo(f2); 596 } 597 } 598 try { 599 open(files[0], false); 600 } catch (IOException ix) { 601 // We don't want to throw an exception here, but we 602 // report the exception to any registered ErrorManager. 603 reportError(null, ix, ErrorManager.OPEN_FAILURE); 604 605 } 606 setLevel(oldLevel); 607 } 608 609 /** 610 * Format and publish a <tt>LogRecord</tt>. 611 * 612 * @param record description of the log event. A null record is 613 * silently ignored and is not published 614 */ 615 @Override 616 public synchronized void publish(LogRecord record) { 617 if (!isLoggable(record)) { 618 return; 619 } 620 super.publish(record); 621 flush(); 622 if (limit > 0 && meter.written >= limit) { 623 // We performed access checks in the "init" method to make sure 624 // we are only initialized from trusted code. So we assume 625 // it is OK to write the target files, even if we are 626 // currently being called from untrusted code. 627 // So it is safe to raise privilege here. 628 AccessController.doPrivileged(new PrivilegedAction<Object>() { 629 @Override 630 public Object run() { 631 rotate(); 632 return null; 633 } 634 }); 635 } 636 } 637 638 /** 639 * Close all the files. 640 * 641 * @exception SecurityException if a security manager exists and if 642 * the caller does not have <tt>LoggingPermission("control")</tt>. 643 */ 644 @Override 645 public synchronized void close() throws SecurityException { 646 super.close(); 647 // Unlock any lock file. 648 if (lockFileName == null) { 649 return; 650 } 651 try { 652 // Close the lock file channel (which also will free any locks) 653 lockFileChannel.close(); 654 } catch (Exception ex) { 655 // Problems closing the stream. Punt. 656 } 657 synchronized(locks) { 658 locks.remove(lockFileName); 659 } 660 new File(lockFileName).delete(); 661 lockFileName = null; 662 lockFileChannel = null; 663 } 664 665 private static class InitializationErrorManager extends ErrorManager { 666 Exception lastException; 667 @Override 668 public void error(String msg, Exception ex, int code) { 669 lastException = ex; 670 } 671 } 672 673 /** 674 * check if we are in a set UID program. 675 */ 676 private static native boolean isSetUID(); 677 }