/** * Publish an {@code ExtLogRecord}. * <p/> * The logging request was made initially to a Logger object, which initialized the LogRecord and forwarded it here. * <p/> * The {@code ExtHandler} is responsible for formatting the message, when and if necessary. The formatting should * include localization. * * @param record the log record to publish */ public void publish(final ExtLogRecord record) { if (enabled && record != null && isLoggable(record)) { doPublish(record); } }
@Override public final void unprotect(Object protectionKey) throws SecurityException { if (protectKeyUpdater.compareAndSet(this, protectionKey, null)) { return; } throw accessDenied(); }
/** * Enables or disables the handler based on the value passed in. * * @param enabled {@code true} to enable the handler or {@code false} to disable the handler. * * @throws SecurityException if a security manager exists and if the caller does not have {@code * LoggingPermission(control)} or the handler is {@link #protect(Object) protected}. */ public final void setEnabled(final boolean enabled) throws SecurityException { checkAccess(this); this.enabled = enabled; }
/** * Change the autoflush setting for this handler. * * @param autoFlush {@code true} to automatically flush after each write; false otherwise * * @throws SecurityException if a security manager exists and if the caller does not have {@code * LoggingPermission(control)} or the handler is {@link #protect(Object) protected}. */ public void setAutoFlush(final boolean autoFlush) throws SecurityException { checkAccess(this); this.autoFlush = autoFlush; if (autoFlush) { flush(); } }
/** * Close all child handlers. */ @Override public void close() throws SecurityException { checkAccess(this); if (closeChildren) { for (Handler handler : handlers) try { handler.close(); } catch (Exception ex) { reportError("Failed to close child handler", ex, ErrorManager.CLOSE_FAILURE); } catch (Throwable ignored) { } } }
/** * Publish a log record. * * @param record the log record to publish */ protected void doPublish(final ExtLogRecord record) { final Appender appender = this.appender; if (appender == null) { throw new IllegalStateException("Appender is closed"); } final LoggingEvent event = new ConvertedLoggingEvent(record); appender.doAppend(event); super.doPublish(record); }
@Override public void setFormatter(final Formatter newFormatter) throws SecurityException { if (applyLayout) { final Appender appender = this.appender; if (appender != null) { appender.setLayout(new FormatterLayout(newFormatter)); } } super.setFormatter(newFormatter); }
/** * A convenience method to atomically get and replace the sub-handler array. * * @param newHandlers the new sub-handlers * @return the old sub-handler array * * @throws SecurityException if a security manager exists and if the caller does not have {@code * LoggingPermission(control)} or the handler is {@link #protect(Object) protected}. */ public Handler[] setHandlers(final Handler[] newHandlers) throws SecurityException { if (newHandlers == null) { throw new IllegalArgumentException("newHandlers is null"); } if (newHandlers.length == 0) { return clearHandlers(); } else { checkAccess(this); final Handler[] handlers = handlersUpdater.getAndSet(this, newHandlers); return handlers.length > 0 ? handlers.clone() : handlers; } }
@Override public void close() { synchronized (outputLock) { safeClose(out); out = null; } super.close(); }
/** * {@inheritDoc} * <p> * Note that once this is invoked the handler will be activated and the messages will no longer be queued. If more * than one child handler is required the {@link #setHandlers(Handler[])} should be used. * </p> * * @see #setHandlers(Handler[]) */ @Override public void addHandler(final Handler handler) throws SecurityException { super.addHandler(handler); activate(); }
/** * {@inheritDoc} * <p> * Note that once this is invoked the handler will no longer be activated and messages will again be queued. * </p> * * @see #removeHandler(Handler) */ @Override public Handler[] clearHandlers() throws SecurityException { activated = false; return super.clearHandlers(); }
@Override protected void doPublish(final ExtLogRecord record) { // If activated just delegate if (activated) { publishToChildren(record); super.doPublish(record); } else { synchronized (this) { // Check one more time to see if we've been activated before queuing the messages if (activated) { publishToChildren(record); super.doPublish(record); } else { // Determine if we need to calculate the caller information before we queue the record if (isCallerCalculationRequired()) { // prepare record to move to another thread record.copyAll(); } else { // Disable the caller calculation since it's been determined we won't be using it record.disableCallerCalculation(); // Copy the MDC over record.copyMdc(); // In case serialization is required by a child handler record.getFormattedMessage(); } logRecords.addLast(record); } } } }
@Override public void setFormatter(final Formatter newFormatter) throws SecurityException { if (applyLayout) { final Appender appender = this.appender; if (appender != null) { appender.setLayout(new FormatterLayout(newFormatter)); } } super.setFormatter(newFormatter); }
/** {@inheritDoc} */ public void close() throws SecurityException { checkAccess(this); if (stateUpdater.getAndSet(this, 2) != 2) { thread.interrupt(); super.close(); } }
public void applyPostCreate(final Void param) { final Map<String, Handler> handlerRefs = configuration.getHandlerRefs(); final ExtHandler handler = (ExtHandler) handlerRefs.get(getName()); handler.addHandler(handlerRefs.get(name)); }
@Override protected void doPublish(final ExtLogRecord record) { final String formatted; final Formatter formatter = getFormatter(); try { formatted = formatter.format(record); } catch (Exception e) { reportError("Could not format message", e, ErrorManager.FORMAT_FAILURE); return; } if (formatted.isEmpty()) { // nothing to write; move along return; } try { synchronized (outputLock) { if (initialize) { initialize(); initialize = false; } if (writer == null) { return; } writer.write(formatted); super.doPublish(record); } } catch (Exception e) { reportError("Error writing log message", e, ErrorManager.WRITE_FAILURE); } }
/** {@inheritDoc} */ public void publish(final LogRecord record) { if (enabled && record != null && isLoggable(record)) { doPublish(ExtLogRecord.wrap(record)); } }
@Override public void setFormatter(final Formatter newFormatter) throws SecurityException { if (applyLayout) { final Appender appender = this.appender; if (appender != null) { appender.setLayout(new FormatterLayout(newFormatter)); } } super.setFormatter(newFormatter); }
/** * Sets whether or not children handlers should be closed when this handler is {@linkplain #close() closed}. * * @param closeChildren {@code true} if all children handlers should be closed when this handler is closed, * {@code false} if children handlers will <em>not</em> be closed when this handler * is closed */ public void setCloseChildren(final boolean closeChildren) { checkAccess(this); this.closeChildren = closeChildren; }
@Override public void close() throws SecurityException { checkAccess(this); synchronized (outputLock) { safeClose(writer); writer = null; initialize = true; } super.close(); }