org.graylog2.gelfclient.GelfMessage Java Examples

The following examples show how to use org.graylog2.gelfclient.GelfMessage. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GelfAppenderThrowableTest.java    From log4j2-gelf with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAppendStacktraceWithoutCauses() throws InterruptedException {
    // given
    final GelfAppender gelfAppender = createGelfAppender(true, false);
    final LogEvent event = createLogEventMock();
    final RuntimeException exception = new RuntimeException("Outer Exception", new Exception("Inner Exception"));
    given(event.getThrown()).willReturn(exception);

    // when
    gelfAppender.append(event);

    // then
    ArgumentCaptor<GelfMessage> gelfMessageCaptor = ArgumentCaptor.forClass(GelfMessage.class);
    verify(mockedGelfTransport).trySend(gelfMessageCaptor.capture());
    final Object exceptionStackTrace = gelfMessageCaptor.getValue()
            .getAdditionalFields()
            .get("exceptionStackTrace");
    assertThat(exceptionStackTrace, notNullValue());

    assertThat(exceptionStackTrace.toString(), is(gelfAppender.getSimpleStacktraceAsString(exception)));
    assertThat(exceptionStackTrace.toString(), not(containsString("Caused by: java.lang.Exception: Inner Exception")));
}
 
Example #2
Source File: GelfAppenderThrowableTest.java    From log4j2-gelf with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAppendStacktraceWithCauses() throws InterruptedException {
    // given
    final GelfAppender gelfAppender = createGelfAppender(true, true);
    final LogEvent event = createLogEventMock();
    given(event.getThrown()).willReturn(new RuntimeException("Outer Exception", new Exception("Inner Exception")));

    // when
    gelfAppender.append(event);

    // then
    ArgumentCaptor<GelfMessage> gelfMessageCaptor = ArgumentCaptor.forClass(GelfMessage.class);
    verify(mockedGelfTransport).trySend(gelfMessageCaptor.capture());
    final Object exceptionStackTrace = gelfMessageCaptor.getValue()
            .getAdditionalFields()
            .get("exceptionStackTrace");
    assertThat(exceptionStackTrace, notNullValue());
    assertThat(exceptionStackTrace.toString(), containsString("Caused by: java.lang.Exception: Inner Exception"));
}
 
Example #3
Source File: GelfAppenderThrowableTest.java    From log4j2-gelf with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldNotFailIfNoExceptionAvailable() throws InterruptedException {
    // given
    final GelfAppender gelfAppender = createGelfAppender(false, false);
    final LogEvent event = createLogEventMock();
    given(event.getThrown()).willReturn(null);

    // when
    gelfAppender.append(event);

    // then
    ArgumentCaptor<GelfMessage> gelfMessageCaptor = ArgumentCaptor.forClass(GelfMessage.class);
    verify(mockedGelfTransport).trySend(gelfMessageCaptor.capture());
    final Object exceptionMessage = gelfMessageCaptor.getValue()
            .getAdditionalFields()
            .get("exceptionMessage");
    final Object exceptionClass = gelfMessageCaptor.getValue()
            .getAdditionalFields()
            .get("exceptionClass");
    final Object exceptionStackTrace = gelfMessageCaptor.getValue()
            .getAdditionalFields()
            .get("exceptionStackTrace");
    assertThat(exceptionMessage, nullValue());
    assertThat(exceptionClass, nullValue());
    assertThat(exceptionStackTrace, nullValue());
}
 
Example #4
Source File: GelfAppenderThrowableTest.java    From log4j2-gelf with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldNotAppendExceptionInformationIfNotRequested() throws InterruptedException {
    // given
    final GelfAppender gelfAppender = createGelfAppender(false, false);
    final LogEvent event = createLogEventMock();
    given(event.getThrown()).willReturn(new RuntimeException("Outer Exception", new Exception("Inner Exception")));

    // when
    gelfAppender.append(event);

    // then
    ArgumentCaptor<GelfMessage> gelfMessageCaptor = ArgumentCaptor.forClass(GelfMessage.class);
    verify(mockedGelfTransport).trySend(gelfMessageCaptor.capture());
    final Object exceptionMessage = gelfMessageCaptor.getValue()
            .getAdditionalFields()
            .get("exceptionMessage");
    final Object exceptionClass = gelfMessageCaptor.getValue()
            .getAdditionalFields()
            .get("exceptionClass");
    final Object exceptionStackTrace = gelfMessageCaptor.getValue()
            .getAdditionalFields()
            .get("exceptionStackTrace");
    assertThat(exceptionMessage, nullValue());
    assertThat(exceptionClass, nullValue());
    assertThat(exceptionStackTrace, nullValue());
}
 
Example #5
Source File: GelfAppenderThrowableTest.java    From log4j2-gelf with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAppendExceptionClassAndMessage() throws InterruptedException {
    // given
    final GelfAppender gelfAppender = createGelfAppender(true, true);
    final LogEvent event = createLogEventMock();
    given(event.getThrown()).willReturn(new RuntimeException("Outer Exception", new Exception("Inner Exception")));

    // when
    gelfAppender.append(event);

    // then
    ArgumentCaptor<GelfMessage> gelfMessageCaptor = ArgumentCaptor.forClass(GelfMessage.class);
    verify(mockedGelfTransport).trySend(gelfMessageCaptor.capture());
    final Object exceptionMessage = gelfMessageCaptor.getValue()
            .getAdditionalFields()
            .get("exceptionMessage");
    final Object exceptionClass = gelfMessageCaptor.getValue()
            .getAdditionalFields()
            .get("exceptionClass");
    assertThat(exceptionMessage, notNullValue());
    assertThat(exceptionMessage.toString(), containsString("Outer Exception"));
    assertThat(exceptionClass, notNullValue());
    assertThat(exceptionClass.toString(), containsString("java.lang.RuntimeException"));
}
 
Example #6
Source File: GelfAppenderThrowableTest.java    From log4j2-gelf with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldNotAddExceptionToFullMessage() throws InterruptedException {
    // given
    final GelfAppender gelfAppender = createGelfAppender(true, true);
    final LogEvent event = createLogEventMock();
    given(event.getThrown()).willReturn(new RuntimeException("Outer Exception", new Exception("Inner Exception")));

    // when
    gelfAppender.append(event);

    // then
    ArgumentCaptor<GelfMessage> gelfMessageCaptor = ArgumentCaptor.forClass(GelfMessage.class);
    verify(mockedGelfTransport).trySend(gelfMessageCaptor.capture());
    final String fullMessage = gelfMessageCaptor.getValue().getFullMessage();
    assertThat(fullMessage, is("Some Message"));
}
 
Example #7
Source File: GelfMessageJsonEncoder.java    From gelfclient with Apache License 2.0 5 votes vote down vote up
private byte[] toJson(final GelfMessage message) throws Exception {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();

    try (final JsonGenerator jg = jsonFactory.createGenerator(out, JsonEncoding.UTF8)) {
        jg.writeStartObject();

        jg.writeStringField("version", message.getVersion().toString());
        jg.writeNumberField("timestamp", message.getTimestamp());
        jg.writeStringField("host", message.getHost());
        jg.writeStringField("short_message", message.getMessage());
        if (message.getLevel() != null) {
            jg.writeNumberField("level", message.getLevel().getNumericLevel());
        }

        if(null != message.getFullMessage()) {
            jg.writeStringField("full_message", message.getFullMessage());
        }

        for (Map.Entry<String, Object> field : message.getAdditionalFields().entrySet()) {
            final String realKey = field.getKey().startsWith("_") ? field.getKey() : ("_" + field.getKey());

            if (field.getValue() instanceof Number) {
                // Let Jackson figure out how to write Number values.
                jg.writeObjectField(realKey, field.getValue());
            } else if (field.getValue() == null) {
                jg.writeNullField(realKey);
            } else {
                jg.writeStringField(realKey, field.getValue().toString());
            }
        }

        jg.writeEndObject();
    }

    return out.toByteArray();
}
 
Example #8
Source File: GelfclientAppender.java    From play2-graylog2 with Apache License 2.0 5 votes vote down vote up
private GelfMessage convertToGelfMessage(ILoggingEvent event) {
    return new GelfMessageBuilder(event.getFormattedMessage(), hostname)
            .timestamp(event.getTimeStamp() / 1000d)
            .level(toGelfMessageLevel(event.getLevel()))
            .additionalField("threadname", event.getThreadName())
            .additionalField("logger", event.getLoggerName())
            .build();
}
 
Example #9
Source File: GelfclientAppender.java    From play2-graylog2 with Apache License 2.0 5 votes vote down vote up
public void append(GelfMessage gelfMessage) {
    try {
        transport.send(gelfMessage);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
 
Example #10
Source File: GelfMessageJsonEncoderTest.java    From gelfclient with Apache License 2.0 5 votes vote down vote up
@Test
public void testNullValue() throws Exception {
    channel = new EmbeddedChannel(new GelfMessageJsonEncoder());
    message = new GelfMessage("test");
    message.addAdditionalField("_null", null);

    assertTrue(channel.writeOutbound(message));
}
 
Example #11
Source File: GelfMessageJsonEncoderTest.java    From gelfclient with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = EncoderException.class)
public void testExceptionIsPassedThrough() throws Exception {
    final JsonFactory jsonFactory = mock(JsonFactory.class);
    when(jsonFactory.createGenerator(any(OutputStream.class), eq(JsonEncoding.UTF8))).thenThrow(new IOException());

    final EmbeddedChannel channel = new EmbeddedChannel(new GelfMessageJsonEncoder(jsonFactory));
    assertTrue(channel.writeOutbound(new GelfMessage("test")));
}
 
Example #12
Source File: AbstractGelfTransport.java    From gelfclient with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new GELF transport with the given configuration and {@link java.util.concurrent.BlockingQueue}.
 *
 * @param config the client configuration
 * @param queue  the {@link BlockingQueue} used to buffer GELF messages
 */
public AbstractGelfTransport(final GelfConfiguration config, final BlockingQueue<GelfMessage> queue) {
    this.config = config;
    this.queue = queue;
    this.workerGroup = new NioEventLoopGroup(config.getThreads(), new DefaultThreadFactory(getClass(), true));
    this.senderThreadReference = new AtomicReference<>();
    createBootstrap(workerGroup);
}
 
Example #13
Source File: GelfMessageJsonEncoderTest.java    From gelfclient with Apache License 2.0 4 votes vote down vote up
@Test
public void testOptionalFullMessage() throws Exception {
    final EmbeddedChannel channel = new EmbeddedChannel(new GelfMessageJsonEncoder());
    final GelfMessage message = new GelfMessageBuilder("test").build();
    assertTrue(channel.writeOutbound(message));
    assertTrue(channel.finish());

    final ByteBuf byteBuf = (ByteBuf) channel.readOutbound();
    final byte[] bytes = new byte[byteBuf.readableBytes()];
    byteBuf.getBytes(0, bytes).release();
    final JsonFactory json = new JsonFactory();
    final JsonParser parser = json.createParser(bytes);

    String version = null;
    Number timestamp = null;
    String host = null;
    String short_message = null;
    String full_message = null;
    Number level = null;

    while (parser.nextToken() != JsonToken.END_OBJECT) {
        String key = parser.getCurrentName();

        if (key == null) {
            continue;
        }

        parser.nextToken();

        switch (key) {
            case "version":
                version = parser.getText();
                break;
            case "timestamp":
                timestamp = parser.getNumberValue();
                break;
            case "host":
                host = parser.getText();
                break;
            case "short_message":
                short_message = parser.getText();
                break;
            case "full_message":
                full_message = parser.getText();
                break;
            case "level":
                level = parser.getNumberValue();
                break;
            default:
                throw new Exception("Found unexpected field in JSON payload: " + key);
        }
    }

    assertEquals(message.getVersion().toString(), version);
    assertEquals(message.getTimestamp(), timestamp);
    assertEquals(message.getHost(), host);
    assertEquals(message.getMessage(), short_message);
    assertNull(full_message);
    assertEquals(message.getLevel().getNumericLevel(), level);
}
 
Example #14
Source File: GelfMessageJsonEncoderTest.java    From gelfclient with Apache License 2.0 4 votes vote down vote up
@Test
public void testNullLevel() throws Exception {
    final EmbeddedChannel channel = new EmbeddedChannel(new GelfMessageJsonEncoder());
    final GelfMessage message = new GelfMessageBuilder("test").build();

    message.setLevel(null);

    assertTrue(channel.writeOutbound(message));
    assertTrue(channel.finish());

    final ByteBuf byteBuf = (ByteBuf) channel.readOutbound();
    final byte[] bytes = new byte[byteBuf.readableBytes()];
    byteBuf.getBytes(0, bytes).release();
    final JsonFactory json = new JsonFactory();
    final JsonParser parser = json.createParser(bytes);

    String version = null;
    Number timestamp = null;
    String host = null;
    String short_message = null;
    String full_message = null;
    Number level = null;

    while (parser.nextToken() != JsonToken.END_OBJECT) {
        String key = parser.getCurrentName();

        if (key == null) {
            continue;
        }

        parser.nextToken();

        switch (key) {
            case "version":
                version = parser.getText();
                break;
            case "timestamp":
                timestamp = parser.getNumberValue();
                break;
            case "host":
                host = parser.getText();
                break;
            case "short_message":
                short_message = parser.getText();
                break;
            case "full_message":
                full_message = parser.getText();
                break;
            case "level":
                level = parser.getNumberValue();
                break;
            default:
                throw new Exception("Found unexpected field in JSON payload: " + key);
        }
    }

    assertEquals(message.getVersion().toString(), version);
    assertEquals(message.getTimestamp(), timestamp);
    assertEquals(message.getHost(), host);
    assertEquals(message.getMessage(), short_message);
    assertNull(full_message);
    assertNull(level);
}
 
Example #15
Source File: GelfAppender.java    From log4j2-gelf with Apache License 2.0 4 votes vote down vote up
@Override
public void append(LogEvent event) {
    final Layout<? extends Serializable> layout = getLayout();
    final String formattedMessage;
    if (layout == null) {
        formattedMessage = event.getMessage().getFormattedMessage();
    } else {
        formattedMessage = new String(layout.toByteArray(event), StandardCharsets.UTF_8);
    }

    final GelfMessageBuilder builder = new GelfMessageBuilder(formattedMessage, hostName)
            .timestamp(event.getTimeMillis() / 1000d)
            .level(GelfMessageLevel.fromNumericLevel(Severity.getSeverity(event.getLevel()).getCode()))
            .additionalField("loggerName", event.getLoggerName())
            .additionalField("threadName", event.getThreadName());

    final Marker marker = event.getMarker();
    if (marker != null) {
        builder.additionalField("marker", marker.getName());
    }

    if (includeThreadContext) {
        for (Map.Entry<String, String> entry : event.getContextMap().entrySet()) {
            builder.additionalField(entry.getKey(), entry.getValue());
        }

        // Guard against https://issues.apache.org/jira/browse/LOG4J2-1530
        final ThreadContext.ContextStack contextStack = event.getContextStack();
        if (contextStack != null) {
            final List<String> contextStackItems = contextStack.asList();
            if (contextStackItems != null && !contextStackItems.isEmpty()) {
                builder.additionalField("contextStack", contextStackItems.toString());
            }
        }
    }

    if (includeSource) {
        final StackTraceElement source = event.getSource();
        if (source != null) {
            builder.additionalField("sourceFileName", source.getFileName());
            builder.additionalField("sourceMethodName", source.getMethodName());
            builder.additionalField("sourceClassName", source.getClassName());
            builder.additionalField("sourceLineNumber", source.getLineNumber());
        }
    }

    @SuppressWarnings("all")
    final Throwable thrown = event.getThrown();
    if (includeStackTrace && thrown != null) {
        String stackTrace;
        if (includeExceptionCause) {
            final StringWriter stringWriter = new StringWriter();
            final PrintWriter printWriter = new PrintWriter(stringWriter);
            thrown.printStackTrace(printWriter);
            stackTrace = stringWriter.toString();
        } else {
            stackTrace = getSimpleStacktraceAsString(thrown);
        }

        builder.additionalField("exceptionClass", thrown.getClass().getCanonicalName());
        builder.additionalField("exceptionMessage", thrown.getMessage());
        builder.additionalField("exceptionStackTrace", stackTrace);

        builder.fullMessage(formattedMessage);
    }

    if (!additionalFields.isEmpty()) {
        builder.additionalFields(additionalFields);
    }

    final GelfMessage gelfMessage = builder.build();
    try {
        final boolean sent = client.trySend(gelfMessage);
        if (!sent) {
            LOG.debug("Couldn't send message: {}", gelfMessage);
        }
    } catch (Exception e) {
        throw new AppenderLoggingException("failed to write log event to GELF server: " + e.getMessage(), e);
    }
}
 
Example #16
Source File: GelfAppenderThrowableTest.java    From log4j2-gelf with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
    when(mockedGelfTransport.trySend(any(GelfMessage.class))).thenReturn(true);
}
 
Example #17
Source File: GelfSenderThread.java    From gelfclient with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a new sender thread with the given {@link BlockingQueue} as source of messages.
 *
 * @param queue            the {@link BlockingQueue} used as source of {@link GelfMessage}s
 * @param maxInflightSends the maximum number of outstanding network writes/flushes before the sender spins
 */
public GelfSenderThread(final BlockingQueue<GelfMessage> queue, int maxInflightSends) {
    this.maxInflightSends = maxInflightSends;
    this.lock = new ReentrantLock();
    this.connectedCond = lock.newCondition();
    this.inflightSends = new AtomicInteger(0);
    this.queue = queue;

    if (maxInflightSends <= 0) {
        throw new IllegalArgumentException("maxInflightSends must be larger than 0");
    }

    this.senderThread = new Thread(new Runnable() {
        @Override
        public void run() {
            GelfMessage gelfMessage = null;
            final ChannelFutureListener inflightListener = new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    inflightSends.decrementAndGet();
                }
            };

            while (keepRunning.get()) {
                // wait until we are connected to the graylog2 server before polling log events from the queue
                lock.lock();
                try {
                    while (channel == null || !channel.isActive()) {
                        try {
                            connectedCond.await();
                        } catch (InterruptedException e) {
                            if (!keepRunning.get()) {
                                // bail out if we are awoken because the application is stopping
                                break;
                            }
                        }
                    }
                    // we are connected, let's start sending logs
                    try {
                        // if we have a lingering event already, try to send that instead of polling a new one.
                        if (gelfMessage == null) {
                            gelfMessage = queue.poll(100, TimeUnit.MILLISECONDS);
                        }
                        // if we are still connected, convert LoggingEvent to GELF and send it
                        // but if we aren't connected anymore, we'll have already pulled an event from the queue,
                        // which we keep hanging around in this thread and in the next loop iteration will block until we are connected again.
                        if (gelfMessage != null && channel != null && channel.isActive()) {
                            // Do not allow more than "maxInflightSends" concurrent writes in netty, to avoid having netty buffer
                            // excessively when faced with slower consumers
                            while (inflightSends.get() > GelfSenderThread.this.maxInflightSends) {
                                Uninterruptibles.sleepUninterruptibly(1, MICROSECONDS);
                            }
                            inflightSends.incrementAndGet();

                            // Write the GELF message to the pipeline. The protocol specific channel handler
                            // will take care of encoding.
                            channel.writeAndFlush(gelfMessage).addListener(inflightListener);
                            gelfMessage = null;
                        }
                    } catch (InterruptedException e) {
                        // ignore, when stopping keepRunning will be set to false outside
                    }
                } finally {
                    lock.unlock();
                }
            }

            LOG.debug("GelfSenderThread exiting!");
        }
    });

    this.senderThread.setDaemon(true);
    this.senderThread.setName("GelfSenderThread-" + senderThread.getId());
}
 
Example #18
Source File: GelfMessageJsonEncoder.java    From gelfclient with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected void encode(ChannelHandlerContext ctx, GelfMessage message, List<Object> out) throws Exception {
    out.add(Unpooled.wrappedBuffer(toJson(message)));
}
 
Example #19
Source File: AbstractGelfTransport.java    From gelfclient with Apache License 2.0 2 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>This implementation is backed by a {@link java.util.concurrent.BlockingQueue}. When this method returns the
 * message has been added to the {@link java.util.concurrent.BlockingQueue} but has not been sent to the remote
 * host yet.</p>
 *
 * @param message message to send to the remote host
 * @return true if the message could be dispatched, false otherwise
 */
@Override
public boolean trySend(final GelfMessage message) {
    LOG.debug("Trying to send message: {}", message);
    return queue.offer(message);
}
 
Example #20
Source File: AbstractGelfTransport.java    From gelfclient with Apache License 2.0 2 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>This implementation is backed by a {@link java.util.concurrent.BlockingQueue}. When this method returns the
 * message has been added to the {@link java.util.concurrent.BlockingQueue} but has not been sent to the remote
 * host yet.</p>
 *
 * @param message message to send to the remote host
 */
@Override
public void send(final GelfMessage message) throws InterruptedException {
    LOG.debug("Sending message: {}", message);
    queue.put(message);
}
 
Example #21
Source File: AbstractGelfTransport.java    From gelfclient with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new GELF transport with the given configuration.
 *
 * @param config the client configuration
 */
public AbstractGelfTransport(final GelfConfiguration config) {
    this(config, new LinkedBlockingQueue<GelfMessage>(config.getQueueSize()));

}
 
Example #22
Source File: GelfTransport.java    From gelfclient with Apache License 2.0 2 votes vote down vote up
/**
 * Tries to send the given message to the remote host. It does <strong>not block</strong> if there is not enough
 * capacity to process the message. It is not guaranteed that the message has been sent once the method call
 * returns because a queue might be used to dispatch the message.
 *
 * @param message message to send to the remote host
 * @return true if the message could be dispatched, false otherwise
 */
boolean trySend(GelfMessage message);
 
Example #23
Source File: GelfTransport.java    From gelfclient with Apache License 2.0 2 votes vote down vote up
/**
 * Sends the given message to the remote host. This <strong>blocks</strong> until there is sufficient capacity to
 * process the message. It is not guaranteed that the message has been sent once the method call returns because
 * a queue might be used to dispatch the message.
 *
 * @param message message to send to the remote host
 */
void send(GelfMessage message) throws InterruptedException;