private static OutputStreamManager getDefaultManager(final Target target, final boolean follow, final boolean direct, final Layout<? extends Serializable> layout) { OutputStream os = NullOutputStream.getInstance(); String managerName = target.name() + '.' + follow + '.' + direct + "-" + COUNT.get(); return OutputStreamManager.getManager(managerName, new FactoryData(os, managerName, layout), MANAGER_FACTORY); }
/** * Some output streams synchronize writes while others do not. Synchronizing here insures that * log events won't be intertwined. * @param bytes The serialized Log event. * @param offset The offset into the byte array. * @param length The number of bytes to write. * @param immediateFlush flushes immediately after writing. * @throws AppenderLoggingException if an error occurs. */ protected synchronized void write(final byte[] bytes, final int offset, final int length, final boolean immediateFlush) { if (immediateFlush && byteBuffer.position() == 0) { writeToDestination(bytes, offset, length); flushDestination(); return; } if (length >= byteBuffer.capacity()) { // if request length exceeds buffer capacity, flush the buffer and write the data directly flush(); writeToDestination(bytes, offset, length); } else { if (length > byteBuffer.remaining()) { flush(); } byteBuffer.put(bytes, offset, length); } if (immediateFlush) { flush(); } }
protected synchronized boolean closeOutputStream() { flush(); final OutputStream stream = outputStream; // access volatile field only once per method if (stream == null || stream == System.out || stream == System.err) { return true; } try { stream.close(); } catch (final IOException ex) { logError("Unable to close stream", ex); return false; } return true; }
public static void flushAll() { final LoggerContext logCtx = ((LoggerContext) LogManager.getContext()); for(final org.apache.logging.log4j.core.Logger logger : logCtx.getLoggers()) { for(final Appender appender : logger.getAppenders().values()) { if(appender instanceof AbstractOutputStreamAppender) { ((AbstractOutputStreamAppender) appender).getManager().flush(); } } } } //
/** * Flushes any buffers. */ public synchronized void flush() { flushBuffer(byteBuffer); flushDestination(); }
/** * Default hook to write footer during close. */ @Override public boolean releaseSub(final long timeout, final TimeUnit timeUnit) { writeFooter(); return closeOutputStream(); }
/** * FileManager's content format is specified by: <code>Key: "fileURI" Value: provided "advertiseURI" param</code>. * * @return Map of content format keys supporting FileManager */ @Override public Map<String, String> getContentFormat() { final Map<String, String> result = new HashMap<>(super.getContentFormat()); result.put("fileURI", advertiseURI); return result; }
/** * Create an OutputStreamManager. * * @param name The name of the entity to manage. * @param data The data required to create the entity. * @return The OutputStreamManager */ @Override public OutputStreamManager createManager(final String name, final FactoryData data) { return new OutputStreamManager(data.os, data.name, data.layout, true); } }
/** * Returns the status of the stream. * @return true if the stream is open, false if it is not. */ public boolean isOpen() { return getCount() > 0; }
protected OutputStream getOutputStream() throws IOException { if (outputStream == null) { outputStream = createOutputStream(); } return outputStream; }
/** * Drains the ByteBufferDestination's buffer into the destination. By default this calls * {@link #flushBuffer(ByteBuffer)} with the specified buffer. Subclasses may override. * <p> * Do not call this method lightly! For some subclasses this is a very expensive operation. For example, * {@link MemoryMappedFileManager} will assume this method was called because the end of the mapped region * was reached during a text encoding operation and will {@linkplain MemoryMappedFileManager#remap() remap} its * buffer. * </p><p> * To just flush the buffered contents to the underlying stream, call * {@link #flushBuffer(ByteBuffer)} directly instead. * </p> * * @param buf the buffer whose contents to write the the destination * @return the specified buffer * @since 2.6 */ @Override public ByteBuffer drain(final ByteBuffer buf) { flushBuffer(buf); return buf; }
static void flushAll() { final LoggerContext logCtx = ((LoggerContext) LogManager.getContext()); for (final org.apache.logging.log4j.core.Logger logger : logCtx.getLoggers()) { for (final Appender appender : logger.getAppenders().values()) { if (appender instanceof AbstractOutputStreamAppender) { ((AbstractOutputStreamAppender) appender).getManager().flush(); } } } }
/** * Gets this FileManager's content format specified by: * <p> * Key: "fileURI" Value: provided "advertiseURI" param. * </p> * * @return Map of content format keys supporting FileManager */ @Override public Map<String, String> getContentFormat() { final Map<String, String> result = new HashMap<>(super.getContentFormat()); result.put("fileURI", advertiseURI); return result; }
/** * Creates an OutputStreamManager. * * @param name * The name of the entity to manage. * @param data * The data required to create the entity. * @return The OutputStreamManager */ @Override public OutputStreamManager createManager(final String name, final FactoryData data) { return new OutputStreamManager(data.os, data.name, data.layout, true); } }
protected void directEncodeEvent(final LogEvent event) { getLayout().encode(event, manager); if (this.immediateFlush || event.isEndOfBatch()) { manager.flush(); } }
private static OutputStreamManager getManager(final Target target, final boolean follow, final boolean direct, final Layout<? extends Serializable> layout) { OutputStream os = NullOutputStream.getInstance(); String managerName = "null." + target.name() + '.' + follow + '.' + direct; return OutputStreamManager.getManager(managerName, new FactoryData(os, managerName, layout), MANAGER_FACTORY); }
/** * Gets this FileManager's content format specified by: * <p> * Key: "fileURI" Value: provided "advertiseURI" param. * </p> * * @return Map of content format keys supporting FileManager */ @Override public Map<String, String> getContentFormat() { final Map<String, String> result = new HashMap<>( super.getContentFormat()); result.put("fileURI", advertiseURI); return result; }
private static OutputStreamManager getManager(final OutputStream target, final boolean follow, final Layout<? extends Serializable> layout) { final OutputStream os = new CloseShieldOutputStream(target); final String managerName = target.getClass().getName() + "@" + Integer.toHexString(target.hashCode()) + '.' + follow; return OutputStreamManager.getManager(managerName, new FactoryData(os, managerName, layout), factory); }
/** * Gets this AbstractSocketManager's content format. Specified by: * <ul> * <li>Key: "port" Value: provided "port" param</li> * <li>Key: "address" Value: provided "address" param</li> * </ul> * * @return Map of content format keys supporting AbstractSocketManager */ @Override public Map<String, String> getContentFormat() { final Map<String, String> result = new HashMap<>(super.getContentFormat()); result.put("port", Integer.toString(port)); result.put("address", inetAddress.getHostAddress()); return result; } }
private static OutputStreamManager getDefaultManager(final Target target, final boolean follow, final boolean direct, final Layout<? extends Serializable> layout) { final OutputStream os = getOutputStream(follow, direct, target); // LOG4J2-1176 DefaultConfiguration should not share OutputStreamManager instances to avoid memory leaks. final String managerName = target.name() + '.' + follow + '.' + direct + "-" + COUNT.get(); return OutputStreamManager.getManager(managerName, new FactoryData(os, managerName, layout), factory); }