Search in sources :

Example 11 with StackSampleElement

use of org.spf4j.base.avro.StackSampleElement 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

StackSampleElement (org.spf4j.base.avro.StackSampleElement)11 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)7 SpecificDatumReader (org.apache.avro.specific.SpecificDatumReader)6 BufferedInputStream (java.io.BufferedInputStream)5 InputStream (java.io.InputStream)5 PushbackInputStream (java.io.PushbackInputStream)5 BinaryDecoder (org.apache.avro.io.BinaryDecoder)5 MemorizingBufferedInputStream (org.spf4j.io.MemorizingBufferedInputStream)5 SampleNode (org.spf4j.stackmonitor.SampleNode)5 TIntObjectHashMap (gnu.trove.map.hash.TIntObjectHashMap)4 IOException (java.io.IOException)4 UncheckedIOException (java.io.UncheckedIOException)4 GZIPInputStream (java.util.zip.GZIPInputStream)4 Nullable (javax.annotation.Nullable)4 BufferedOutputStream (java.io.BufferedOutputStream)2 OutputStream (java.io.OutputStream)2 HashMap (java.util.HashMap)2 NoSuchElementException (java.util.NoSuchElementException)2 GZIPOutputStream (java.util.zip.GZIPOutputStream)2 BinaryEncoder (org.apache.avro.io.BinaryEncoder)2