Search in sources :

Example 1 with LogRecord

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);
    }
}
Also used : SpecificDatumReader(org.apache.avro.specific.SpecificDatumReader) DatumReader(org.apache.avro.io.DatumReader) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) Schema(org.apache.avro.Schema) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) Decoder(org.apache.avro.io.Decoder) URL(java.net.URL) URLConnection(java.net.URLConnection) BufferedInputStream(java.io.BufferedInputStream) LogRecord(org.spf4j.base.avro.LogRecord) StringReader(java.io.StringReader) List(java.util.List) SpecificDatumReader(org.apache.avro.specific.SpecificDatumReader) StackSampleElement(org.spf4j.base.avro.StackSampleElement) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 2 with LogRecord

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);
}
Also used : StackSamples(org.spf4j.base.StackSamples) Marker(org.slf4j.Marker) LogRecord(org.spf4j.base.avro.LogRecord) StackSampleElement(org.spf4j.base.avro.StackSampleElement) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)2 LogRecord (org.spf4j.base.avro.LogRecord)2 StackSampleElement (org.spf4j.base.avro.StackSampleElement)2 BufferedInputStream (java.io.BufferedInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 StringReader (java.io.StringReader)1 UncheckedIOException (java.io.UncheckedIOException)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 List (java.util.List)1 Schema (org.apache.avro.Schema)1 DatumReader (org.apache.avro.io.DatumReader)1 Decoder (org.apache.avro.io.Decoder)1 SpecificDatumReader (org.apache.avro.specific.SpecificDatumReader)1 Marker (org.slf4j.Marker)1 StackSamples (org.spf4j.base.StackSamples)1