use of org.spf4j.base.avro.LogRecord in project spf4j by zolyfarkas.
the class TextEntryPanel method displayActionPerformed.
@SuppressFBWarnings({ "UP_UNUSED_PARAMETER", "URLCONNECTION_SSRF_FD" })
private void displayActionPerformed(java.awt.event.ActionEvent evt) {
// GEN-FIRST:event_displayActionPerformed
try {
String text = jTextPane1.getText().trim();
if (text.startsWith("http")) {
URL url = new URL(text);
URLConnection conn = url.openConnection();
conn.setRequestProperty("Accept", "application/avro");
conn.connect();
String contentType = conn.getContentType();
if (!"application/avro".equals(contentType)) {
throw new IOException("Unsupported content type " + contentType);
}
try (InputStream is = new BufferedInputStream(conn.getInputStream())) {
List<LogRecord> recs = (List<LogRecord>) readAvroBin(is, Schema.createArray(LogRecord.SCHEMA$));
for (LogRecord rec : recs) {
List<StackSampleElement> stackSamples = rec.getStackSamples();
if (!stackSamples.isEmpty()) {
nodeConsumer.accept(rec.getMsg() + "; with trId=" + rec.getTrId(), Converter.convert(stackSamples.iterator()));
}
}
}
} else if (text.startsWith("[")) {
Schema schema = Schema.createArray(StackSampleElement.getClassSchema());
DatumReader reader = new SpecificDatumReader(schema);
List<StackSampleElement> samples;
try {
Decoder decoder = DecoderFactory.get().jsonDecoder(schema, text);
samples = (List<StackSampleElement>) reader.read(null, decoder);
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
nodeConsumer.accept("SampleNode Array", Converter.convert(samples.iterator()));
} else {
nodeConsumer.accept("SampleNode Tree", SampleNode.parse(new StringReader(text)).getSecond());
}
} catch (IOException | RuntimeException ex) {
errorConsumer.accept(ex);
}
}
use of org.spf4j.base.avro.LogRecord in project spf4j by zolyfarkas.
the class Slf4jLogRecord method toLogRecord.
@SuppressFBWarnings("WOC_WRITE_ONLY_COLLECTION_LOCAL")
default LogRecord toLogRecord(final String origin, final String ptraceId) {
java.lang.Throwable extraThrowable = this.getExtraThrowable();
Marker marker = this.getMarker();
Object[] extraArguments = this.getExtraArguments();
Map<String, Object> attribs = null;
List<Object> xArgs;
String traceId = ptraceId;
List<StackSampleElement> profiles = Collections.EMPTY_LIST;
if (extraArguments.length == 0) {
xArgs = Collections.emptyList();
} else {
int nrAttribs = 0;
for (Object obj : extraArguments) {
if (obj instanceof LogAttribute) {
LogAttribute la = (LogAttribute) obj;
String name = la.getName();
switch(name) {
case LogAttribute.ID_ATTR_NAME:
traceId = la.getValue().toString();
break;
case LogAttribute.PROFILE_SAMPLES_ATTR_NAME:
profiles = Converters.convert((StackSamples) la.getValue());
break;
default:
}
nrAttribs++;
}
}
if (nrAttribs == 0) {
xArgs = Arrays.asList(extraArguments);
} else {
if (nrAttribs == extraArguments.length) {
xArgs = Collections.EMPTY_LIST;
} else {
xArgs = new ArrayList<>(extraArguments.length - nrAttribs);
}
attribs = Maps.newHashMapWithExpectedSize(nrAttribs + (marker == null ? 0 : 1));
for (Object obj : extraArguments) {
if (obj instanceof LogAttribute) {
LogAttribute la = (LogAttribute) obj;
String aName = la.getName();
switch(aName) {
case LogAttribute.ID_ATTR_NAME:
case LogAttribute.PROFILE_SAMPLES_ATTR_NAME:
break;
default:
attribs.put(aName, la.getValue());
}
} else {
xArgs.add(obj);
}
}
if (marker != null) {
attribs.put(marker.getName(), marker);
}
}
}
int nrMsgArgs = this.getNrMessageArguments();
List<String> messageArgs;
if (nrMsgArgs == 0) {
messageArgs = Collections.emptyList();
} else {
Object[] args = this.getArguments();
String[] ma = new String[nrMsgArgs];
for (int i = 0; i < nrMsgArgs; i++) {
Object arg = args[i];
if (arg == null) {
ma[i] = "null";
} else {
ma[i] = ObjectAppenderSupplier.getDefaultToStringAppenderSupplier().get(arg.getClass()).toString(arg);
}
}
messageArgs = Arrays.asList(ma);
}
return new LogRecord(origin, traceId, this.getLevel().getAvroLevel(), Instant.ofEpochMilli(this.getTimeStamp()), this.getLoggerName(), this.getThreadName(), this.getMessageFormat(), messageArgs, xArgs, attribs == null ? Collections.emptyMap() : attribs, extraThrowable == null ? null : convert(extraThrowable), profiles);
}
Aggregations