本文整理汇总了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);
}
}
示例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();
}
}
示例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();
}
示例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());
}
示例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");
}
}
示例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);
}
示例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);
}
示例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);
}
}
示例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());
}