Permalink
Browse files

json marshaller implementation

Signed-off-by: Peter Edge <peter.edge@gmail.com>
  • Loading branch information...
peter-edge committed Feb 3, 2016
1 parent 1749aff commit 3f34c5dc4e735814b7191d5201e65ec732d9b72a
Showing with 124 additions and 11 deletions.
  1. +94 −8 json_marshaller.go
  2. +25 −2 lion.go
  3. +5 −1 testing/testing_test.go
View
@@ -1,18 +1,104 @@
package lion
type jsonMarshaller struct{}
import (
"bytes"
"time"
)
func newJSONMarshaller() *jsonMarshaller {
return &jsonMarshaller{}
type jsonMarshaller struct {
disableNewlines bool
}
func newJSONMarshaller(options ...JSONMarshallerOption) *jsonMarshaller {
jsonMarshaller := &jsonMarshaller{false}
for _, option := range options {
option(jsonMarshaller)
}
return jsonMarshaller
}
func (t *jsonMarshaller) Marshal(entry *Entry) ([]byte, error) {
return jsonMarshalEntry(entry)
return jsonMarshalEntry(entry, t.disableNewlines)
}
func jsonMarshalEntry(entry *Entry) ([]byte, error) {
return nil, nil
func jsonMarshalEntry(entry *Entry, disableNewlines bool) ([]byte, error) {
jsonEntry, err := entryToJSONEntry(entry)
if err != nil {
return nil, err
}
if jsonEntry == nil {
return nil, nil
}
buffer := bytes.NewBuffer(nil)
if err := globalJSONMarshalFunc(buffer, jsonEntry); err != nil {
return nil, err
}
if !disableNewlines {
_ = buffer.WriteByte('\n')
}
return buffer.Bytes(), nil
}
//func jsonMarshalMessage(buffer *bytes.Buffer, entry *Entry) ([]byte, error) {
//}
type jsonEntry struct {
ID string `json:"id,omitempty"`
Level string `json:"level,omitempty"`
Time string `json:"time,omitempty"`
Contexts []interface{} `json:"contexts,omitempty"`
Fields map[string]string `json:"fields,omitempty"`
Event interface{} `json:"event,omitempty"`
Message string `json:"message,omitempty"`
WriterOutput string `json:"writer_output,omitempty"`
}
type jsonEntryMessage struct {
Name string `json:"name,omitempty"`
Value interface{} `json:"value,omitempty"`
}
func entryToJSONEntry(entry *Entry) (*jsonEntry, error) {
if entry == nil {
return nil, nil
}
jsonEntry := &jsonEntry{
ID: entry.ID,
Level: entry.Level.String(),
Time: entry.Time.Format(time.RFC3339),
Fields: entry.Fields,
Message: entry.Message,
WriterOutput: string(entry.WriterOutput),
}
if len(entry.Contexts) > 0 {
jsonEntry.Contexts = make([]interface{}, 0)
for _, context := range entry.Contexts {
jsonContext, err := entryMessageToJSONEntryMessage(context)
if err != nil {
return nil, err
}
if jsonContext != nil {
jsonEntry.Contexts = append(jsonEntry.Contexts, jsonContext)
}
}
}
if entry.Event != nil {
jsonEvent, err := entryMessageToJSONEntryMessage(entry.Event)
if err != nil {
return nil, err
}
jsonEntry.Event = jsonEvent
}
return jsonEntry, nil
}
func entryMessageToJSONEntryMessage(entryMessage *EntryMessage) (*jsonEntryMessage, error) {
if entryMessage == nil {
return nil, nil
}
name, err := entryMessage.Name()
if err != nil {
return nil, nil
}
return &jsonEntryMessage{
Name: name,
Value: entryMessage.Value,
}, nil
}
View
27 lion.go
@@ -31,8 +31,8 @@ var (
return err
}
// JSONMarshaller is a Marshaller that marshals to JSON.
JSONMarshaller = newJSONMarshaller()
// DefaultJSONMarshaller is the default JSON Marshaller.
DefaultJSONMarshaller = NewJSONMarshaller()
// DiscardPusher is a Pusher that discards all logs.
DiscardPusher = discardPusherInstance
@@ -388,6 +388,14 @@ func NewTextWritePusher(writer io.Writer, textMarshallerOptions ...TextMarshalle
)
}
// NewJSONWritePusher constructs a new Pusher using a JSON Marshaller.
func NewJSONWritePusher(writer io.Writer, jsonMarshallerOptions ...JSONMarshallerOption) Pusher {
return NewWritePusher(
writer,
NewJSONMarshaller(jsonMarshallerOptions...),
)
}
// Puller pulls EncodedEntry objects from a persistent store.
type Puller interface {
Pull() (*EncodedEntry, error)
@@ -453,6 +461,21 @@ func NewTextMarshaller(options ...TextMarshallerOption) TextMarshaller {
return newTextMarshaller(options...)
}
// JSONMarshallerDisableNewlines disables newlines after each marshalled Entry.
func JSONMarshallerDisableNewlines() JSONMarshallerOption {
return func(jsonMarshaller *jsonMarshaller) {
jsonMarshaller.disableNewlines = true
}
}
// JSONMarshallerOption is an option for creating Marshallers.
type JSONMarshallerOption func(*jsonMarshaller)
// NewJSONMarshaller constructs a new Marshaller for JSON.
func NewJSONMarshaller(options ...JSONMarshallerOption) Marshaller {
return newJSONMarshaller(options...)
}
// NewMultiPusher constructs a new Pusher that calls all the given Pushers.
func NewMultiPusher(pushers ...Pusher) Pusher {
return newMultiPusher(pushers)
View
@@ -69,7 +69,7 @@ func TestRoundtripAndTextMarshaller(t *testing.T) {
logger.Infoln("a normal line")
logger.WithField("someKey", "someValue").Warnln("a warning line")
},
`INFO lion.testing.Foo {"one":"","two":0,"string_field":"one","int32_field":2}
`INFO lion.testing.Foo {"string_field":"one","int32_field":2}
INFO hello2 {"3":"4"}
INFO lion.testing.Baz {"bat":{"ban":{"string_field":"one","int32_field":2}}}
INFO lion.testing.Empty {}
@@ -196,6 +196,10 @@ func TestPrintSomeStuff(t *testing.T) {
testPrintSomeStuff(t, protolion.NewLogger(lion.DefaultLogger))
}
func TestPrintSomeStuffJSON(t *testing.T) {
testPrintSomeStuff(t, protolion.NewLogger(lion.NewLogger(lion.NewJSONWritePusher(os.Stderr))))
}
func testPrintSomeStuff(t *testing.T, logger protolion.Logger) {
logger.Info(
&Foo{

0 comments on commit 3f34c5d

Please sign in to comment.