/** * Construct a new instance. * * @param limit the queue length to use */ public QueueHandler(final int limit) { if (limit < 1) { throw badQueueLength(); } this.limit = limit; }
/** * Get a copy of the queue, rendering each record as a string. * * @return the copy of the queue rendered as strings */ public String[] getQueueAsStrings() { final ExtLogRecord[] queue = getQueue(); final int length = queue.length; final String[] strings = new String[length]; final Formatter formatter = getFormatter(); for (int i = 0, j = 0; j < length; j++) { final String formatted; try { formatted = formatter.format(queue[j]); if (formatted.length() > 0) { strings[i++] = getFormatter().format(queue[j]); } } catch (Exception ex) { reportError("Formatting error", ex, ErrorManager.FORMAT_FAILURE); } } return strings; }
protected void doPublish(final ExtLogRecord record) { synchronized (buffer) { if (isLoggable(record)) { // 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(); } if (buffer.size() == limit) { buffer.removeFirst(); } buffer.addLast(record); } for (Handler handler : getHandlers()) { handler.publish(record); } } }
/** * Replay the stored queue to the nested handlers. */ public void replay() { final Handler[] handlers = getHandlers(); if (handlers.length > 0) for (ExtLogRecord record : getQueue()) { for (Handler handler : handlers) { handler.publish(record); } } }
/** * Set the queue length limit. This is the number of messages that will be saved before old messages roll off * of the queue. * * @param limit the queue length limit */ public void setLimit(final int limit) { if (limit < 1) { throw badQueueLength(); } synchronized (buffer) { this.limit = limit; } }