Skip to content

Commit

Permalink
GZIP Compression with GelfUdpAppender (osiegmar#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinebesnardof committed Nov 23, 2021
1 parent 9c90fe9 commit 881fc69
Show file tree
Hide file tree
Showing 7 changed files with 268 additions and 25 deletions.
39 changes: 39 additions & 0 deletions src/main/java/de/siegmar/logbackgelf/CompressionMethod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Logback GELF - zero dependencies Logback GELF appender library.
* Copyright (C) 2019 Oliver Siegmar
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

package de.siegmar.logbackgelf;

import de.siegmar.logbackgelf.compressor.Compressor;
import de.siegmar.logbackgelf.compressor.GZIPCompressor;
import de.siegmar.logbackgelf.compressor.ZLIBCompressor;

public enum CompressionMethod {
ZLIB(new ZLIBCompressor()),
GZIP(new GZIPCompressor());

private Compressor compressor;

CompressionMethod(final Compressor compressor) {
this.compressor = compressor;
}

public Compressor getCompressor() {
return this.compressor;
}
}
36 changes: 21 additions & 15 deletions src/main/java/de/siegmar/logbackgelf/GelfUdpAppender.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@

package de.siegmar.logbackgelf;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.util.function.Supplier;
import java.util.zip.DeflaterOutputStream;

import de.siegmar.logbackgelf.compressor.Compressor;
import de.siegmar.logbackgelf.compressor.NoneCompressor;

public class GelfUdpAppender extends AbstractGelfAppender {

Expand All @@ -42,6 +42,11 @@ public class GelfUdpAppender extends AbstractGelfAppender {
*/
private boolean useCompression = true;

/**
* Compression method used if useCompression is true. Default: ZLIB.
*/
private CompressionMethod compressionMethod = CompressionMethod.ZLIB;

private Supplier<Long> messageIdSupplier = new MessageIdSupplier();

private RobustChannel robustChannel;
Expand All @@ -50,6 +55,8 @@ public class GelfUdpAppender extends AbstractGelfAppender {

private AddressResolver addressResolver;

private Compressor compressor;

public Integer getMaxChunkSize() {
return maxChunkSize;
}
Expand All @@ -66,6 +73,14 @@ public void setUseCompression(final boolean useCompression) {
this.useCompression = useCompression;
}

public CompressionMethod getCompressionMethod() {
return compressionMethod;
}

public void setCompressionMethod(final CompressionMethod compressionMethod) {
this.compressionMethod = compressionMethod;
}

public Supplier<Long> getMessageIdSupplier() {
return messageIdSupplier;
}
Expand All @@ -79,14 +94,15 @@ protected void startAppender() throws IOException {
robustChannel = new RobustChannel();
chunker = new GelfUdpChunker(messageIdSupplier, maxChunkSize);
addressResolver = new AddressResolver(getGraylogHost());
compressor = useCompression ? compressionMethod.getCompressor() : new NoneCompressor();
}

@Override
protected void appendMessage(final byte[] binMessage) throws IOException {
final byte[] messageToSend = useCompression ? compress(binMessage) : binMessage;
final byte[] messageToSend = compressor.compress(binMessage);

final InetSocketAddress remote = new InetSocketAddress(addressResolver.resolve(),
getGraylogPort());
getGraylogPort());

for (final ByteBuffer chunk : chunker.chunks(messageToSend)) {
while (chunk.hasRemaining()) {
Expand All @@ -95,16 +111,6 @@ protected void appendMessage(final byte[] binMessage) throws IOException {
}
}

private static byte[] compress(final byte[] binMessage) {
final ByteArrayOutputStream bos = new ByteArrayOutputStream(binMessage.length);
try (OutputStream deflaterOut = new DeflaterOutputStream(bos)) {
deflaterOut.write(binMessage);
} catch (IOException e) {
throw new IllegalStateException(e);
}
return bos.toByteArray();
}

@Override
protected void close() throws IOException {
robustChannel.close();
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/de/siegmar/logbackgelf/compressor/Compressor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Logback GELF - zero dependencies Logback GELF appender library.
* Copyright (C) 2016 Oliver Siegmar
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

package de.siegmar.logbackgelf.compressor;


public interface Compressor {

byte[] compress(byte[] binMessage);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Logback GELF - zero dependencies Logback GELF appender library.
* Copyright (C) 2019 Oliver Siegmar
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

package de.siegmar.logbackgelf.compressor;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.GZIPOutputStream;

public class GZIPCompressor implements Compressor {

@Override
public byte[] compress(final byte[] binMessage) {
final ByteArrayOutputStream bos = new ByteArrayOutputStream(binMessage.length);
try (OutputStream gzipOut = new GZIPOutputStream(bos)) {
gzipOut.write(binMessage);
} catch (IOException e) {
throw new IllegalStateException(e);
}
return bos.toByteArray();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Logback GELF - zero dependencies Logback GELF appender library.
* Copyright (C) 2019 Oliver Siegmar
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

package de.siegmar.logbackgelf.compressor;

public class NoneCompressor implements Compressor {

@Override
public byte[] compress(final byte[] binMessage) {
return binMessage;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Logback GELF - zero dependencies Logback GELF appender library.
* Copyright (C) 2019 Oliver Siegmar
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

package de.siegmar.logbackgelf.compressor;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.DeflaterOutputStream;

public class ZLIBCompressor implements Compressor {

@Override
public byte[] compress(final byte[] binMessage) {
final ByteArrayOutputStream bos = new ByteArrayOutputStream(binMessage.length);
try (OutputStream deflaterOut = new DeflaterOutputStream(bos)) {
deflaterOut.write(binMessage);
} catch (IOException e) {
throw new IllegalStateException(e);
}
return bos.toByteArray();
}

}
Loading

0 comments on commit 881fc69

Please sign in to comment.