/** * Some output streams synchronize writes while others do not. * @param bytes The serialized Log event. * @throws AppenderLoggingException if an error occurs. */ protected void write(final byte[] bytes) { write(bytes, 0, bytes.length, false); }
/** * Some output streams synchronize writes while others do not. * @param bytes The serialized Log event. * @param immediateFlush If true, flushes after writing. * @throws AppenderLoggingException if an error occurs. */ protected void write(final byte[] bytes, final boolean immediateFlush) { write(bytes, 0, bytes.length, immediateFlush); }
@Override public void writeBytes(final byte[] data, final int offset, final int length) { write(data, offset, length, false); }
/** * Writes the footer. */ protected void writeFooter() { if (layout == null || skipFooter) { return; } final byte[] footer = layout.getFooter(); if (footer != null) { write(footer); } }
@Override protected synchronized void write(final byte[] bytes, final int offset, final int length, final boolean immediateFlush) { if (isLocking) { try { @SuppressWarnings("resource") final FileChannel channel = ((FileOutputStream) getOutputStream()).getChannel(); /* * Lock the whole file. This could be optimized to only lock from the current file position. Note that * locking may be advisory on some systems and mandatory on others, so locking just from the current * position would allow reading on systems where locking is mandatory. Also, Java 6 will throw an * exception if the region of the file is already locked by another FileChannel in the same JVM. * Hopefully, that will be avoided since every file should have a single file manager - unless two * different files strings are configured that somehow map to the same file. */ try (final FileLock lock = channel.lock(0, Long.MAX_VALUE, false)) { super.write(bytes, offset, length, immediateFlush); } } catch (final IOException ex) { throw new AppenderLoggingException("Unable to obtain lock on " + getName(), ex); } } else { super.write(bytes, offset, length, immediateFlush); } }
protected void writeByteArrayToManager(final LogEvent event) { final byte[] bytes = getLayout().toByteArray(event); if (bytes != null && bytes.length > 0) { manager.write(bytes, this.immediateFlush || event.isEndOfBatch()); } } }