ch.qos.logback.core.AsyncAppenderBase类的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(11.1k)|赞(0)|评价(0)|浏览(272)

本文整理了Java中ch.qos.logback.core.AsyncAppenderBase类的一些代码示例,展示了AsyncAppenderBase类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。AsyncAppenderBase类的具体详情如下:
包路径:ch.qos.logback.core.AsyncAppenderBase
类名称:AsyncAppenderBase

AsyncAppenderBase介绍

[英]This appender and derived classes, log events asynchronously. In order to avoid loss of logging events, this appender should be closed. It is the user's responsibility to close appenders, typically at the end of the application lifecycle.

This appender buffers events in a BlockingQueue. ch.qos.logback.core.AsyncAppenderBase.Worker thread created by this appender takes events from the head of the queue, and dispatches them to the single appender attached to this appender.

Please refer to the logback manual for further information about this appender.
[中]此附加器和派生类异步记录事件。为了避免日志事件丢失,应关闭此附加器。用户有责任关闭appender,通常在应用程序生命周期结束时关闭。
此追加器缓冲BlockingQueue中的事件。ch.qos。向后退。果心异步附加数据库。此appender创建的工作线程从队列头获取事件,并将它们分派到附加到此appender的单个appender。
请参阅logback manual以了解有关此追加器的更多信息。

代码示例

代码示例来源:origin: camunda/camunda-bpm-platform

public void addAppender(Appender<E> newAppender) {
 if (appenderCount == 0) {
  appenderCount++;
  addInfo("Attaching appender named ["+newAppender.getName()+"] to AsyncAppender.");
  aai.addAppender(newAppender);
 } else {
  addWarn("One and only one appender may be attached to AsyncAppender.");
  addWarn("Ignoring additional appender named [" + newAppender.getName() + "]");
 }
}

代码示例来源:origin: camunda/camunda-bpm-platform

@Override
protected void append(E eventObject) {
 if (isQueueBelowDiscardingThreshold() && isDiscardable(eventObject)) {
  return;
 }
 preprocess(eventObject);
 put(eventObject);
}

代码示例来源:origin: dropwizard/dropwizard

protected Appender<E> wrapAsync(Appender<E> appender, AsyncAppenderFactory<E> asyncAppenderFactory, Context context) {
  final AsyncAppenderBase<E> asyncAppender = asyncAppenderFactory.build();
  if (asyncAppender instanceof AsyncAppender) {
    ((AsyncAppender) asyncAppender).setIncludeCallerData(includeCallerData);
  }
  asyncAppender.setQueueSize(queueSize);
  asyncAppender.setDiscardingThreshold(discardingThreshold);
  asyncAppender.setContext(context);
  asyncAppender.setName("async-" + appender.getName());
  asyncAppender.addAppender(appender);
  asyncAppender.setNeverBlock(neverBlock);
  asyncAppender.start();
  if (messageRate == null) {
    return asyncAppender;
  } else {
    return new ThrottlingAppenderWrapper<>(asyncAppender, messageRate.getQuantity(), messageRate.getUnit());
  }
}

代码示例来源:origin: dropwizard/dropwizard

private void flushAppender(AsyncAppenderBase<?> appender) throws InterruptedException {
  int timeWaiting = 0;
  while (timeWaiting < appender.getMaxFlushTime() && appender.getNumberOfElementsInQueue() > 0) {
    Thread.sleep(100);
    timeWaiting += 100;
  }
  if (appender.getNumberOfElementsInQueue() > 0) {
    // It may seem odd to log when we're trying to flush a logger that
    // isn't flushing, but the same warning is issued inside
    // appender.stop() if the appender isn't able to flush.
    appender.addWarn(appender.getNumberOfElementsInQueue() + " events may be discarded");
  }
}

代码示例来源:origin: com.hynnet/logback-core

@Override
public void stop() {
 if (!isStarted())
  return;
 // mark this appender as stopped so that Worker can also processPriorToRemoval if it is invoking aii.appendLoopOnAppenders
 // and sub-appenders consume the interruption
 super.stop();
 // interrupt the worker thread so that it can terminate. Note that the interruption can be consumed
 // by sub-appenders
 worker.interrupt();
 try {
  worker.join(maxFlushTime);
  
  //check to see if the thread ended and if not add a warning message
  if(worker.isAlive()) {
   addWarn("Max queue flush timeout (" + maxFlushTime + " ms) exceeded. Approximately " + blockingQueue.size() +
     " queued events were possibly discarded.");
  }else {
   addInfo("Queue flush finished successfully within timeout.");
  }
  
 } catch (InterruptedException e) {
  addError("Failed to join worker thread. " + blockingQueue.size() + " queued events may be discarded.", e);
 }
}

代码示例来源:origin: tony19/logback-android

@Test(timeout = 2000)
public void noEventLoss() {
 int bufferSize = 10;
 int loopLen = bufferSize * 2;
 asyncAppenderBase.addAppender(delayingListAppender);
 asyncAppenderBase.setQueueSize(bufferSize);
 asyncAppenderBase.start();
 for (int i = 0; i < loopLen; i++) {
  asyncAppenderBase.doAppend(i);
 }
 asyncAppenderBase.stop();
 verify(delayingListAppender, loopLen);
}

代码示例来源:origin: com.impetus.fabric/fabric-jdbc-driver-shaded

@Override
public void start() {
  if (isStarted())
    return;
  if (appenderCount == 0) {
    addError("No attached appenders found.");
    return;
  }
  if (queueSize < 1) {
    addError("Invalid queue size [" + queueSize + "]");
    return;
  }
  blockingQueue = new ArrayBlockingQueue<E>(queueSize);
  if (discardingThreshold == UNDEFINED)
    discardingThreshold = queueSize / 5;
  addInfo("Setting discardingThreshold to " + discardingThreshold);
  worker.setDaemon(true);
  worker.setName("AsyncAppender-Worker-" + getName());
  // make sure this instance is marked as "started" before staring the worker Thread
  super.start();
  worker.start();
}

代码示例来源:origin: com.proofpoint.platform/log

asyncAppender = new AsyncAppenderBase<>();
  asyncAppender.setContext(context);
  asyncAppender.setQueueSize(queueSize);
  asyncAppender.addAppender(fileAppender);
fileAppender.start();
if (queueSize > 0) {
  asyncAppender.start();
  return asyncAppender;

代码示例来源:origin: camunda/camunda-bpm-platform

@Override
public void stop() {
 if (!isStarted())
  return;
 // mark this appender as stopped so that Worker can also processPriorToRemoval if it is invoking aii.appendLoopOnAppenders
 // and sub-appenders consume the interruption
 super.stop();
 // interrupt the worker thread so that it can terminate. Note that the interruption can be consumed
 // by sub-appenders
 worker.interrupt();
 try {
  worker.join(1000);
 } catch (InterruptedException e) {
  addError("Failed to join worker thread", e);
 }
}

代码示例来源:origin: camunda/camunda-bpm-platform

@Override
public void start() {
 if (appenderCount == 0) {
  addError("No attached appenders found.");
  return;
 }
 if (queueSize < 1) {
  addError("Invalid queue size [" + queueSize + "]");
  return;
 }
 blockingQueue = new ArrayBlockingQueue<E>(queueSize);
 if (discardingThreshold == UNDEFINED)
  discardingThreshold = queueSize / 5;
 addInfo("Setting discardingThreshold to " + discardingThreshold);
 worker.setDaemon(true);
 worker.setName("AsyncAppender-Worker-" + worker.getName());
 // make sure this instance is marked as "started" before staring the worker Thread
 super.start();
 worker.start();
}

代码示例来源:origin: tony19/logback-android

@Test
public void invalidQueueCapacityShouldResultInNonStartedAppender() {
 asyncAppenderBase.addAppender(new NOPAppender<Integer>());
 asyncAppenderBase.setQueueSize(0);
 assertEquals(0, asyncAppenderBase.getQueueSize());
 asyncAppenderBase.start();
 assertFalse(asyncAppenderBase.isStarted());
 statusChecker.assertContainsMatch("Invalid queue size");
}

代码示例来源:origin: tony19/logback-android

@SuppressWarnings("deprecation")
@Test
public void workerThreadFlushesOnStop() {
 int loopLen = 5;
 int maxRuntime = (loopLen + 1) * Math.max(1000, delayingListAppender.delay);
 ListAppender<Integer> la = delayingListAppender;
 asyncAppenderBase.addAppender(la);
 asyncAppenderBase.setDiscardingThreshold(0);
 asyncAppenderBase.setMaxFlushTime(maxRuntime);
 asyncAppenderBase.start();
 asyncAppenderBase.worker.suspend();
 for (int i = 0; i < loopLen; i++) {
  asyncAppenderBase.doAppend(i);
 }
 assertEquals(loopLen, asyncAppenderBase.getNumberOfElementsInQueue());
 assertEquals(0, la.list.size());
 asyncAppenderBase.worker.resume();
 asyncAppenderBase.stop();
 assertEquals(0, asyncAppenderBase.getNumberOfElementsInQueue());
 verify(la, loopLen);
}

代码示例来源:origin: tony19/logback-android

@Test
public void exceptionsShouldNotCauseHalting() throws InterruptedException {
 NPEAppender<Integer> npeAppender = new NPEAppender<Integer>();
 npeAppender.setName("bad");
 npeAppender.setContext(context);
 npeAppender.start();
 asyncAppenderBase.addAppender(npeAppender);
 asyncAppenderBase.start();
 assertTrue(asyncAppenderBase.isStarted());
 for (int i = 0; i < 10; i++)
  asyncAppenderBase.append(i);
 asyncAppenderBase.stop();
 assertFalse(asyncAppenderBase.isStarted());
 assertEquals(AppenderBase.ALLOWED_REPEATS, statusChecker.matchCount("Appender \\[bad\\] failed to append."));
}

代码示例来源:origin: com.hynnet/logback-core

@Override
public void start() {
 if (appenderCount == 0) {
  addError("No attached appenders found.");
  return;
 }
 if (queueSize < 1) {
  addError("Invalid queue size [" + queueSize + "]");
  return;
 }
 blockingQueue = new ArrayBlockingQueue<E>(queueSize);
 if (discardingThreshold == UNDEFINED)
  discardingThreshold = queueSize / 5;
 addInfo("Setting discardingThreshold to " + discardingThreshold);
 worker.setDaemon(true);
 worker.setName("AsyncAppender-Worker-" + getName());
 // make sure this instance is marked as "started" before staring the worker Thread
 super.start();
 worker.start();
}

代码示例来源:origin: tony19/logback-android

@Test(timeout = 2000)
public void emptyQueueShouldBeStoppable() {
 asyncAppenderBase.addAppender(listAppender);
 asyncAppenderBase.start();
 asyncAppenderBase.stop();
 verify(listAppender, 0);
}

代码示例来源:origin: tony19/logback-android

@Test
public void verifyInterruptionIsNotSwallowed() {
 asyncAppenderBase.addAppender(delayingListAppender);
 asyncAppenderBase.start();
 Thread.currentThread().interrupt();
 asyncAppenderBase.doAppend(0);
 assertTrue(Thread.currentThread().isInterrupted());
 // clear interrupt flag for subsequent tests
 Thread.interrupted();
}

代码示例来源:origin: tony19/logback-android

@Test
 public void checkThatStartMethodIsIdempotent() {
  asyncAppenderBase.addAppender(lossyAsyncAppender);
  asyncAppenderBase.start();

  // we don't need mockito for this test, but if we did here is how it would look
  //AsyncAppenderBase<Integer>  spied = Mockito.spy(asyncAppenderBase);
  //Mockito.doThrow(new IllegalStateException("non idempotent start")).when((UnsynchronizedAppenderBase<Integer>) spied).start();

  // a second invocation of start will cause a IllegalThreadStateException thrown by the asyncAppenderBase.worker thread
  asyncAppenderBase.start();
 }
}

代码示例来源:origin: tony19/logback-android

@Before
public void setUp() {
 asyncAppenderBase.setContext(context);
 lossyAsyncAppender.setContext(context);
 listAppender.setContext(context);
 listAppender.setName("list");
 listAppender.start();
 delayingListAppender.setContext(context);
 delayingListAppender.setName("list");
 delayingListAppender.start();
}

代码示例来源:origin: Nextdoor/bender

@Override
public void stop() {
  if (!isStarted())
    return;
  // mark this appender as stopped so that Worker can also processPriorToRemoval if it is invoking
  // aii.appendLoopOnAppenders
  // and sub-appenders consume the interruption
  super.stop();
  // interrupt the worker thread so that it can terminate. Note that the interruption can be consumed
  // by sub-appenders
  worker.interrupt();
  try {
    worker.join(maxFlushTime);
    // check to see if the thread ended and if not add a warning message
    if (worker.isAlive()) {
      addWarn("Max queue flush timeout (" + maxFlushTime + " ms) exceeded. Approximately " + blockingQueue.size()
              + " queued events were possibly discarded.");
    } else {
      addInfo("Queue flush finished successfully within timeout.");
    }
  } catch (InterruptedException e) {
    addError("Failed to join worker thread. " + blockingQueue.size() + " queued events may be discarded.", e);
  }
}

代码示例来源:origin: tony19/logback-android

@Test(timeout = 2000)
public void smoke() {
 asyncAppenderBase.addAppender(listAppender);
 asyncAppenderBase.start();
 asyncAppenderBase.doAppend(0);
 asyncAppenderBase.stop();
 verify(listAppender, 1);
}

相关文章