@Override public int getDiscardedBucketsCount() { return outputStream.getDiscardedBucketCount(); }
@Override public long getCurrentLogEntriesBufferSizeInBytes() { return outputStream.getCurrentOutputStreamSize(); }
/** * Roll current bucket if size threshold has been reached. */ private void rollCurrentBucketIfNeeded() { if (currentBucket.size() < maxBucketSizeInBytes) { return; } rollCurrentBucket(); }
/** * Moves the current active bucket to the list of filled buckets and defines a new one. * * The new active bucket is reused from the {@link #recycledBucketPool} pool if one is available or recreated. */ public void rollCurrentBucket() { currentBucketLock.lock(); try { boolean offered = filledBuckets.offer(currentBucket); if (offered) { onBucketRoll(currentBucket); } else { onBucketDiscard(currentBucket); discardedBucketCount.incrementAndGet(); } currentBucket = newBucket(); } finally { currentBucketLock.unlock(); } }
/** * Send log entries to Loggly */ @Override public void processLogEntries() { if (isDebug()) { addInfo("Process log entries - " + getDebugInfo()); } outputStream.rollCurrentBucketIfNotEmpty(); BlockingDeque<ByteArrayOutputStream> filledBuckets = outputStream.getFilledBuckets(); ByteArrayOutputStream bucket; while ((bucket = filledBuckets.poll()) != null) { try { InputStream in = new ByteArrayInputStream(bucket.toByteArray()); processLogEntries(in); } catch (Exception e) { addWarn("Internal error", e); } outputStream.recycleBucket(bucket); } }
@Override protected void processLogEntries(InputStream in) { // super.processLogEntries(in); try { IoUtils.copy(in, out); } catch (IOException e) { e.printStackTrace(); } } };
@Override public void write(byte[] b) throws IOException { currentBucketLock.lock(); try { currentBucket.write(b); rollCurrentBucketIfNeeded(); } finally { currentBucketLock.unlock(); } }
/** * @param maxBucketSizeInBytes maximum byte size of each bucket * @param maxBucketCount maximum number of buckets */ public DiscardingRollingOutputStream(int maxBucketSizeInBytes, int maxBucketCount) { if (maxBucketCount < 2) { throw new IllegalArgumentException("'maxBucketCount' must be >1"); } this.maxBucketSizeInBytes = maxBucketSizeInBytes; this.filledBuckets = new LinkedBlockingDeque<ByteArrayOutputStream>(maxBucketCount); this.recycledBucketPool = new ConcurrentLinkedQueue<ByteArrayOutputStream>(); this.currentBucket = newBucket(); }
@Override protected void append(E eventObject) { if (!isStarted()) { return; } String msg = this.layout.doLayout(eventObject); // Issue #21: Make sure messages end with new-line to delimit // individual log events within the batch sent to loggly. if (!msg.endsWith("\n")) { msg += "\n"; } try { outputStream.write(msg.getBytes(charset)); } catch (IOException e) { throw new RuntimeException(e); } }
@Override public void stop() { scheduledExecutor.shutdown(); processLogEntries(); if (registeredObjectName != null) { try { mbeanServer.unregisterMBean(registeredObjectName); } catch (Exception e) { addWarn("Exception unRegistering mbean " + registeredObjectName, e); } } try { scheduledExecutor.awaitTermination(2 * this.flushIntervalInSeconds, TimeUnit.SECONDS); } catch (InterruptedException e) { addWarn("Exception waiting for termination of LogglyAppender scheduler", e); } // stop appender (ie close outputStream) after sending it to Loggly outputStream.close(); super.stop(); }
BufferedOutputStream out = new BufferedOutputStream(conn.getOutputStream()); long len = IoUtils.copy(in, out); sentBytes.addAndGet(len);
@Override public void write(byte[] b, int off, int len) throws IOException { currentBucketLock.lock(); try { currentBucket.write(b, off, len); rollCurrentBucketIfNeeded(); } finally { currentBucketLock.unlock(); } }
/** * Roll current bucket if size threshold has been reached. */ public void rollCurrentBucketIfNotEmpty() { if (currentBucket.size() == 0) { return; } rollCurrentBucket(); }
@Override public void write(int b) throws IOException { currentBucketLock.lock(); try { currentBucket.write(b); rollCurrentBucketIfNeeded(); } finally { currentBucketLock.unlock(); } }