Ejemplo n.º 1
0
 /**
  * Actual writing occurs here.
  *
  * @param logEvent The LogEvent.
  */
 @Override
 public void append(LogEvent logEvent) {
   if (!isStarted()) {
     throw new IllegalStateException("AsyncAppender " + getName() + " is not active");
   }
   if (!(logEvent instanceof Log4jLogEvent)) {
     if (!(logEvent instanceof RingBufferLogEvent)) {
       return; // only know how to Serialize Log4jLogEvents and RingBufferLogEvents
     }
     logEvent = ((RingBufferLogEvent) logEvent).createMemento();
   }
   logEvent.getMessage().getFormattedMessage(); // LOG4J2-763: ask message to freeze parameters
   final Log4jLogEvent coreEvent = (Log4jLogEvent) logEvent;
   boolean appendSuccessful = false;
   if (blocking) {
     if (isAppenderThread.get() == Boolean.TRUE && queue.remainingCapacity() == 0) {
       // LOG4J2-485: avoid deadlock that would result from trying
       // to add to a full queue from appender thread
       coreEvent.setEndOfBatch(false); // queue is definitely not empty!
       appendSuccessful = thread.callAppenders(coreEvent);
     } else {
       final Serializable serialized = Log4jLogEvent.serialize(coreEvent, includeLocation);
       try {
         // wait for free slots in the queue
         queue.put(serialized);
         appendSuccessful = true;
       } catch (final InterruptedException e) {
         // LOG4J2-1049: Some applications use Thread.interrupt() to send
         // messages between application threads. This does not necessarily
         // mean that the queue is full. To prevent dropping a log message,
         // quickly try to offer the event to the queue again.
         // (Yes, this means there is a possibility the same event is logged twice.)
         //
         // Finally, catching the InterruptedException means the
         // interrupted flag has been cleared on the current thread.
         // This may interfere with the application's expectation of
         // being interrupted, so when we are done, we set the interrupted
         // flag again.
         appendSuccessful = queue.offer(serialized);
         if (!appendSuccessful) {
           LOGGER.warn(
               "Interrupted while waiting for a free slot in the AsyncAppender LogEvent-queue {}",
               getName());
         }
         // set the interrupted flag again.
         Thread.currentThread().interrupt();
       }
     }
   } else {
     appendSuccessful = queue.offer(Log4jLogEvent.serialize(coreEvent, includeLocation));
     if (!appendSuccessful) {
       error("Appender " + getName() + " is unable to write primary appenders. queue is full");
     }
   }
   if (!appendSuccessful && errorAppender != null) {
     errorAppender.callAppender(coreEvent);
   }
 }
Ejemplo n.º 2
0
 /**
  * FOR INTERNAL USE ONLY.
  *
  * @param logEvent the event to log
  */
 public void logMessageInCurrentThread(final LogEvent logEvent) {
   logEvent.setEndOfBatch(queue.isEmpty());
   final boolean appendSuccessful = thread.callAppenders(logEvent);
   logToErrorAppenderIfNecessary(appendSuccessful, logEvent);
 }