• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Java GelfMessageBuilder类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Java中org.graylog2.gelfclient.GelfMessageBuilder的典型用法代码示例。如果您正苦于以下问题:Java GelfMessageBuilder类的具体用法?Java GelfMessageBuilder怎么用?Java GelfMessageBuilder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



GelfMessageBuilder类属于org.graylog2.gelfclient包,在下文中一共展示了GelfMessageBuilder类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。

示例1: write

import org.graylog2.gelfclient.GelfMessageBuilder; //导入依赖的package包/类
@Override
public void write(Message message) {
    Uninterruptibles.awaitUninterruptibly(transportInitialized);

    LOG.debug("Sending message: {}", message);

    try {
        final GelfMessageBuilder messageBuilder = new GelfMessageBuilder(message.getMessage(), message.getSource())
                .timestamp(message.getTimestamp().getMillis() / 1000.0)
                .additionalFields(message.getFields().asMap());

        if (message.getLevel() != null) {
            messageBuilder.level(GelfMessageLevel.valueOf(message.getLevel().toString()));
        } else {
            messageBuilder.level(null);
        }

        transport.send(messageBuilder.build());
    } catch (InterruptedException e) {
        LOG.error("Failed to send message", e);
    }
}
 
开发者ID:DevOpsStudio,项目名称:Re-Collector,代码行数:23,代码来源:GelfOutput.java


示例2: append

import org.graylog2.gelfclient.GelfMessageBuilder; //导入依赖的package包/类
public void append(
        String shortMessage, AccessLogMessage accessLogMessage,
        Map<String, Object> additionalFields) {

    GelfMessage gelfMessage = new GelfMessageBuilder(shortMessage, hostname)
            .additionalFields(accessLogMessage.getFields())
            .additionalFields(additionalFields)
            .level(GelfMessageLevel.INFO)
            .build();
    try {
        transport.send(gelfMessage);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

}
 
开发者ID:tochkak,项目名称:play-graylog2,代码行数:17,代码来源:GelfClientAppender.java


示例3: convertToGelfMessage

import org.graylog2.gelfclient.GelfMessageBuilder; //导入依赖的package包/类
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();
}
 
开发者ID:tochkak,项目名称:play-graylog2,代码行数:9,代码来源:GelfClientAppender.java


示例4: setup

import org.graylog2.gelfclient.GelfMessageBuilder; //导入依赖的package包/类
@BeforeMethod
public void setup() {
    channel = new EmbeddedChannel(new GelfMessageJsonEncoder());
    message = new GelfMessageBuilder("test")
            .fullMessage("The full message!")
            .level(GelfMessageLevel.INFO)
            .additionalField("_foo", 1.0)
            .additionalField("_bar", 128)
            .additionalField("_baz", "a value")
            .build();

    assertTrue(channel.writeOutbound(message));
    assertTrue(channel.finish());
}
 
开发者ID:Graylog2,项目名称:gelfclient,代码行数:15,代码来源:GelfMessageJsonEncoderTest.java


示例5: append

import org.graylog2.gelfclient.GelfMessageBuilder; //导入依赖的package包/类
@Override
protected void append(ILoggingEvent event) {

    if (event == null) {
        return;
    }

    // create a copy of the logging event to avoid passing exception stacktraces to GELF's short_message field
    LoggingEvent copy = new LoggingEvent();
    copy.setMessage(event.getMessage());
    copy.setLevel(event.getLevel());
    copy.setArgumentArray(event.getArgumentArray());

    final GelfMessageBuilder builder = new GelfMessageBuilder(this.layout.doLayout(copy), hostName()).timestamp(
                event.getTimeStamp() / 1000d)
            .level(GelfMessageLevel.fromNumericLevel(toGelfNumericValue(event.getLevel())))
            .additionalField("loggerName", event.getLoggerName())
            .additionalField("threadName", event.getThreadName());

    final Marker marker = event.getMarker();

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

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

    StackTraceElement[] callerData = event.getCallerData();

    if (includeSource && event.hasCallerData() && callerData.length > 0) {
        StackTraceElement source = callerData[0];

        builder.additionalField("sourceFileName", source.getFileName());
        builder.additionalField("sourceMethodName", source.getMethodName());
        builder.additionalField("sourceClassName", source.getClassName());
        builder.additionalField("sourceLineNumber", source.getLineNumber());
    }

    IThrowableProxy thrown = event.getThrowableProxy();

    if (includeStackTrace && thrown != null) {
        String convertedThrowable = throwableConverter.convert(event);

        builder.additionalField("exceptionClass", thrown.getClassName());
        builder.additionalField("exceptionMessage", thrown.getMessage());
        builder.additionalField("exceptionStackTrace", convertedThrowable);

        builder.fullMessage(event.getFormattedMessage() + "\n\n" + convertedThrowable);
    } else {
        builder.fullMessage(event.getFormattedMessage());
    }

    if (includeLevelName) {
        builder.additionalField("levelName", event.getLevel().levelStr);
    }

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

    if(!client.trySend(builder.build())) {
        addError("Failed to write log event to the GELF server using trySend");
    }
}
 
开发者ID:rkcpi,项目名称:logback-gelf-appender,代码行数:69,代码来源:GelfAppender.java


示例6: testOptionalFullMessage

import org.graylog2.gelfclient.GelfMessageBuilder; //导入依赖的package包/类
@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);
}
 
开发者ID:Graylog2,项目名称:gelfclient,代码行数:61,代码来源:GelfMessageJsonEncoderTest.java


示例7: testNullLevel

import org.graylog2.gelfclient.GelfMessageBuilder; //导入依赖的package包/类
@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);
}
 
开发者ID:Graylog2,项目名称:gelfclient,代码行数:64,代码来源:GelfMessageJsonEncoderTest.java


示例8: append

import org.graylog2.gelfclient.GelfMessageBuilder; //导入依赖的package包/类
@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);
    }
}
 
开发者ID:graylog-labs,项目名称:log4j2-gelf,代码行数:81,代码来源:GelfAppender.java


示例9: write

import org.graylog2.gelfclient.GelfMessageBuilder; //导入依赖的package包/类
void write(final GelfTransport gelfClient, final LogEntry logEntry) throws Exception {
    final String message = logEntry.getRenderedLogEntry() == null ? logEntry.getMessage() : logEntry.getRenderedLogEntry();
    final GelfMessageBuilder messageBuilder = new GelfMessageBuilder(message, hostname)
            .timestamp(logEntry.getDate().getTime() / 1000d)
            .level(toGelfMessageLevel(logEntry.getLevel()))
            .additionalFields(staticFields);

    final String processId = logEntry.getProcessId();
    if (null != processId) {
        messageBuilder.additionalField("processId", processId);
    }

    final Thread thread = logEntry.getThread();
    if (null != thread) {
        messageBuilder.additionalField("threadName", thread.getName());
        messageBuilder.additionalField("threadGroup", thread.getThreadGroup().getName());
        messageBuilder.additionalField("threadPriority", thread.getPriority());
    }

    final String className = logEntry.getClassName();
    if (null != className) {
        messageBuilder.additionalField("sourceClassName", className);
    }

    final String methodName = logEntry.getMethodName();
    if (null != methodName) {
        messageBuilder.additionalField("sourceMethodName", methodName);
    }

    final String fileName = logEntry.getFilename();
    if (null != fileName) {
        messageBuilder.additionalField("sourceFileName", fileName);
    }

    final int lineNumber = logEntry.getLineNumber();
    if (lineNumber != -1) {
        messageBuilder.additionalField("sourceLineNumber", lineNumber);
    }

    @SuppressWarnings("all")
    final Throwable throwable = logEntry.getException();
    if (null != throwable) {
        final StringBuilder stackTraceBuilder = new StringBuilder();
        for (StackTraceElement stackTraceElement : throwable.getStackTrace()) {
            new Formatter(stackTraceBuilder)
                    .format("%s.%s(%s:%d)%n",
                            stackTraceElement.getClassName(), stackTraceElement.getMethodName(),
                            stackTraceElement.getFileName(), stackTraceElement.getLineNumber());
        }

        messageBuilder.additionalField("exceptionClass", throwable.getClass().getCanonicalName());
        messageBuilder.additionalField("exceptionMessage", throwable.getMessage());
        messageBuilder.additionalField("exceptionStackTrace", stackTraceBuilder.toString());
        messageBuilder.fullMessage(message + "\n\n" + stackTraceBuilder.toString());
    }

    gelfClient.send(messageBuilder.build());
}
 
开发者ID:joschi,项目名称:tinylog-gelf,代码行数:59,代码来源:GelfWriter.java



注:本文中的org.graylog2.gelfclient.GelfMessageBuilder类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java StringUtil类代码示例发布时间:2022-05-22
下一篇:
Java Type类代码示例发布时间:2022-05-22
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap