Skip to content

Commit

Permalink
Merge pull request #45 from ipfs/feat/pretty-logging
Browse files Browse the repository at this point in the history
make logging prettier
  • Loading branch information
Stebalien authored Aug 24, 2018
2 parents 3e24213 + df9b564 commit f2bdd6b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
8 changes: 6 additions & 2 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,13 +336,17 @@ func (el *eventLogger) Event(ctx context.Context, event string, metadata ...Logg
accum["system"] = e.system
accum["time"] = FormatRFC3339(time.Now())

out, err := json.Marshal(accum)
var buf bytes.Buffer
encoder := json.NewEncoder(&buf)
encoder.SetEscapeHTML(false)
encoder.SetIndent("", " ")
err = encoder.Encode(accum)
if err != nil {
el.Errorf("ERROR FORMATTING EVENT ENTRY: %s", err)
return
}

writer.WriterGroup.Write(append(out, '\n'))
writer.WriterGroup.Write(buf.Bytes())
}

// DEPRECATED
Expand Down
24 changes: 21 additions & 3 deletions tracer/recorder.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package loggabletracer

import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"os"
Expand Down Expand Up @@ -65,22 +67,38 @@ func (r *LoggableSpanRecorder) RecordSpan(span RawSpan) {
}
}

tags := make(map[string]interface{}, len(span.Tags))
for k, v := range span.Tags {
switch vt := v.(type) {
case bool, string, int, int8, int16, int32, int64, uint, uint8, uint16, uint64:
tags[k] = v
case []byte:
base64.StdEncoding.EncodeToString(vt)
default:
tags[k] = fmt.Sprint(v)
}
}

spanlog := &LoggableSpan{
TraceID: span.Context.TraceID,
SpanID: span.Context.SpanID,
ParentSpanID: span.ParentSpanID,
Operation: span.Operation,
Start: span.Start,
Duration: span.Duration,
Tags: span.Tags,
Tags: tags,
Logs: sl,
}

out, err := json.Marshal(spanlog)
var buf bytes.Buffer
encoder := json.NewEncoder(&buf)
encoder.SetEscapeHTML(false)
encoder.SetIndent("", " ")
err := encoder.Encode(spanlog)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR FORMATTING SPAN ENTRY: %s\n", err)
return
}

writer.WriterGroup.Write(append(out, '\n'))
writer.WriterGroup.Write(buf.Bytes())
}

0 comments on commit f2bdd6b

Please sign in to comment.