Open the writer for the current log file.
()
| 459 | * Open the writer for the current log file. |
| 460 | */ |
| 461 | protected void openWriter() { |
| 462 | |
| 463 | // Create the directory if necessary |
| 464 | File dir = new File(directory); |
| 465 | if (!dir.mkdirs() && !dir.isDirectory()) { |
| 466 | reportError("Unable to create [" + dir + "]", null, ErrorManager.OPEN_FAILURE); |
| 467 | writer = null; |
| 468 | return; |
| 469 | } |
| 470 | |
| 471 | // Open the current log file |
| 472 | writerLock.writeLock().lock(); |
| 473 | FileOutputStream fos = null; |
| 474 | OutputStream os = null; |
| 475 | try { |
| 476 | File pathname = new File(dir.getAbsoluteFile(), prefix + (rotatable.booleanValue() ? date : "") + suffix); |
| 477 | File parent = pathname.getParentFile(); |
| 478 | if (!parent.mkdirs() && !parent.isDirectory()) { |
| 479 | reportError("Unable to create [" + parent + "]", null, ErrorManager.OPEN_FAILURE); |
| 480 | writer = null; |
| 481 | return; |
| 482 | } |
| 483 | String encoding = getEncoding(); |
| 484 | fos = new FileOutputStream(pathname, true); |
| 485 | os = bufferSize.intValue() > 0 ? new BufferedOutputStream(fos, bufferSize.intValue()) : fos; |
| 486 | writer = new PrintWriter( |
| 487 | (encoding != null) ? new OutputStreamWriter(os, encoding) : new OutputStreamWriter(os), false); |
| 488 | writer.write(getFormatter().getHead(this)); |
| 489 | } catch (Exception e) { |
| 490 | reportError(null, e, ErrorManager.OPEN_FAILURE); |
| 491 | writer = null; |
| 492 | if (fos != null) { |
| 493 | try { |
| 494 | fos.close(); |
| 495 | } catch (IOException ignore) { |
| 496 | // Ignore |
| 497 | } |
| 498 | } |
| 499 | if (os != null) { |
| 500 | try { |
| 501 | os.close(); |
| 502 | } catch (IOException ignore) { |
| 503 | // Ignore |
| 504 | } |
| 505 | } |
| 506 | } finally { |
| 507 | writerLock.writeLock().unlock(); |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | private void clean() { |
| 512 | if (maxDays.intValue() <= 0 || Files.notExists(getDirectoryAsPath())) { |
no test coverage detected