Permalink
Browse files

fix proto json marshalling, fix kit logger

Signed-off-by: Peter Edge <peter.edge@gmail.com>
  • Loading branch information...
peter-edge committed Jan 27, 2016
1 parent 10b3cad commit 7c1a27e31774edc3f535749fee930a527cbb05d3
Showing with 110 additions and 35 deletions.
  1. +1 −1 kit/kit.go
  2. +37 −3 lion.go
  3. +11 −1 proto/protolion.go
  4. +36 −15 testing/testing.pb.go
  5. +4 −0 testing/testing.proto
  6. +10 −2 testing/testing_test.go
  7. +11 −13 text_marshaller.go
View
@@ -30,6 +30,6 @@ func newLogger(l lion.Logger) *logger {
}
func (l *logger) Log(keyvals ...interface{}) error {
l.l.WithKeyValues(keyvals).Println()
l.l.WithKeyValues(keyvals...).Println()
return nil
}
View
40 lion.go
@@ -4,6 +4,7 @@ Package lion defines the main lion logging functionality.
package lion // import "go.pedge.io/lion"
import (
"encoding/json"
"io"
"log"
"os"
@@ -20,6 +21,15 @@ var (
DefaultTimer = &timer{}
// DefaultErrorHandler is the default ErrorHandler.
DefaultErrorHandler = &errorHandler{}
// DefaultJSONMarshalFunc is the default JSONMarshalFunc.
DefaultJSONMarshalFunc = func(writer io.Writer, value interface{}) error {
data, err := json.Marshal(value)
if err != nil {
return err
}
_, err = writer.Write(data)
return err
}
// DiscardPusher is a Pusher that discards all logs.
DiscardPusher = discardPusherInstance
@@ -31,9 +41,10 @@ var (
// DefaultLogger is the default Logger.
DefaultLogger = NewLogger(DefaultPusher)
globalLogger = DefaultLogger
globalHooks = make([]GlobalHook, 0)
globalLock = &sync.Mutex{}
globalLogger = DefaultLogger
globalJSONMarshalFunc = DefaultJSONMarshalFunc
globalHooks = make([]GlobalHook, 0)
globalLock = &sync.Mutex{}
)
// GlobalHook is a function that handles a change in the global Logger instance.
@@ -64,6 +75,16 @@ func SetLevel(level Level) {
}
}
// SetJSONMarshalFunc sets the global JSONMarshalFunc to be used by default.
func SetJSONMarshalFunc(jsonMarshalFunc JSONMarshalFunc) {
globalLock.Lock()
defer globalLock.Unlock()
globalJSONMarshalFunc = jsonMarshalFunc
for _, globalHook := range globalHooks {
globalHook(globalLogger)
}
}
// AddGlobalHook adds a GlobalHook that will be called any time SetLogger or SetLevel is called.
// It will also be called when added.
func AddGlobalHook(globalHook GlobalHook) {
@@ -374,6 +395,19 @@ func TextMarshallerDisableNewlines() TextMarshallerOption {
}
}
// JSONMarshalFunc marshals JSON for a TextMarshaller.
//
// It is used internally in a TextMarshaller, and is not a Marshaller itself.
type JSONMarshalFunc func(writer io.Writer, value interface{}) error
// TextMarshallerWithJSONMarshalFunc uses the given JSONMarshalFunc for JSON marshalling. The default
// behavior is to use the global JSONMarshalFunc.
func TextMarshallerWithJSONMarshalFunc(jsonMarshalFunc JSONMarshalFunc) TextMarshallerOption {
return func(textMarshaller *textMarshaller) {
textMarshaller.jsonMarshalFunc = jsonMarshalFunc
}
}
// NewTextMarshaller constructs a new Marshaller that produces human-readable
// marshalled Entry objects. This Marshaller is currently inefficient.
func NewTextMarshaller(options ...TextMarshallerOption) TextMarshaller {
View
@@ -9,6 +9,7 @@ import (
"go.pedge.io/lion"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
)
@@ -20,18 +21,27 @@ var (
DelimitedMarshaller = &delimitedMarshaller{}
// DelimitedUnmarshaller is an Unmarshaller that uses the protocol buffers write delimited scheme.
DelimitedUnmarshaller = &delimitedUnmarshaller{}
// DefaultJSONMarshalFunc is the default protocol buffers JSONMarshalFunc.
DefaultJSONMarshalFunc = func(writer io.Writer, data interface{}) error {
if message, ok := data.(proto.Message); ok {
return globalJSONMarshaler.Marshal(writer, message)
}
return lion.DefaultJSONMarshalFunc(writer, data)
}
globalPrimaryPackage = "golang"
globalSecondaryPackage = "gogo"
globalOnlyPrimaryPackage = true
globalLogger = NewLogger(lion.GlobalLogger())
globalLogger Logger
globalLock = &sync.Mutex{}
globalJSONMarshaler = &jsonpb.Marshaler{}
)
func init() {
if err := lion.RegisterEncoderDecoder(Encoding, newEncoderDecoder()); err != nil {
panic(err.Error())
}
lion.SetJSONMarshalFunc(DefaultJSONMarshalFunc)
lion.AddGlobalHook(setGlobalLogger)
}
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -31,3 +31,7 @@ message Baz {
}
message Empty {}
message NoStdJson {
map<uint64, string> one = 1;
}
View
@@ -45,6 +45,12 @@ func TestRoundtripAndTextMarshaller(t *testing.T) {
},
)
logger.Info(&Empty{})
logger.Info(&NoStdJson{
One: map[uint64]string{
1: "one",
2: "two",
},
})
writer := logger.InfoWriter()
for _, s := range []string{
"hello",
@@ -78,9 +84,10 @@ func TestRoundtripAndTextMarshaller(t *testing.T) {
}
require.Equal(
t,
`DEBUG lion.testing.Foo {"string_field":"one","int32_field":2}
`DEBUG lion.testing.Foo {"one":"","two":0,"string_field":"one","int32_field":2}
INFO lion.testing.Baz {"bat":{"ban":{"string_field":"one","int32_field":2}}}
INFO lion.testing.Empty {}
INFO lion.testing.NoStdJson {"one":{"1":"one","2":"two"}}
INFO hello
INFO world
INFO writing
@@ -100,7 +107,7 @@ func TestPrintSomeStuff(t *testing.T) {
}
func testPrintSomeStuff(t *testing.T, logger protolion.Logger) {
logger.Debug(
logger.Info(
&Foo{
StringField: "one",
Int32Field: 2,
@@ -141,6 +148,7 @@ func testPrintSomeStuff(t *testing.T, logger protolion.Logger) {
},
},
)
logger.WithKeyValues("someKey", "someValue", "someOtherKey", 1).Infoln()
_ = kitlion.NewLogger(logger.LionLogger()).Log("someKey", "someValue", "someOtherKey", 1)
}
View
@@ -2,7 +2,6 @@ package lion
import (
"bytes"
"encoding/json"
"time"
"unicode"
@@ -33,6 +32,7 @@ type textMarshaller struct {
disableContexts bool
disableNewlines bool
colorize bool
jsonMarshalFunc JSONMarshalFunc
}
func newTextMarshaller(options ...TextMarshallerOption) *textMarshaller {
@@ -42,6 +42,7 @@ func newTextMarshaller(options ...TextMarshallerOption) *textMarshaller {
false,
false,
false,
globalJSONMarshalFunc,
}
for _, option := range options {
option(textMarshaller)
@@ -56,6 +57,7 @@ func (t *textMarshaller) WithColors() TextMarshaller {
t.disableContexts,
t.disableNewlines,
true,
t.jsonMarshalFunc,
}
}
@@ -66,6 +68,7 @@ func (t *textMarshaller) WithoutColors() TextMarshaller {
t.disableContexts,
t.disableNewlines,
false,
t.jsonMarshalFunc,
}
}
@@ -77,6 +80,7 @@ func (t *textMarshaller) Marshal(entry *Entry) ([]byte, error) {
t.disableContexts,
t.disableNewlines,
t.colorize,
t.jsonMarshalFunc,
)
}
@@ -87,6 +91,7 @@ func textMarshalEntry(
disableContexts bool,
disableNewlines bool,
colorize bool,
jsonMarshalFunc JSONMarshalFunc,
) ([]byte, error) {
buffer := bytes.NewBuffer(nil)
if entry.ID != "" {
@@ -114,7 +119,7 @@ func textMarshalEntry(
// TODO(pedge): verify only one of Event, Message, WriterOutput?
if entry.Event != nil {
eventSeen = true
if err := textMarshalMessage(buffer, entry.Event); err != nil {
if err := textMarshalMessage(jsonMarshalFunc, buffer, entry.Event); err != nil {
return nil, err
}
}
@@ -133,7 +138,7 @@ func textMarshalEntry(
eventSeen = true
lenContexts := len(entry.Contexts)
for i, context := range entry.Contexts {
if err := textMarshalMessage(buffer, context); err != nil {
if err := textMarshalMessage(jsonMarshalFunc, buffer, context); err != nil {
return nil, err
}
if i != lenContexts-1 {
@@ -145,11 +150,9 @@ func textMarshalEntry(
if eventSeen {
_ = buffer.WriteByte(' ')
}
data, err := json.Marshal(entry.Fields)
if err != nil {
if err := jsonMarshalFunc(buffer, entry.Fields); err != nil {
return nil, err
}
_, _ = buffer.Write(data)
}
data := trimRightSpaceBytes(buffer.Bytes())
if !disableNewlines {
@@ -160,7 +163,7 @@ func textMarshalEntry(
return data, nil
}
func textMarshalMessage(buffer *bytes.Buffer, message *EntryMessage) error {
func textMarshalMessage(jsonMarshalFunc JSONMarshalFunc, buffer *bytes.Buffer, message *EntryMessage) error {
if message == nil {
return nil
}
@@ -170,12 +173,7 @@ func textMarshalMessage(buffer *bytes.Buffer, message *EntryMessage) error {
}
_, _ = buffer.WriteString(name)
_ = buffer.WriteByte(' ')
data, err := json.Marshal(message.Value)
if err != nil {
return err
}
_, err = buffer.Write(data)
return err
return jsonMarshalFunc(buffer, message.Value)
}
func trimRightSpaceBytes(b []byte) []byte {

0 comments on commit 7c1a27e

Please sign in to comment.