From 639325f81ab8f8610fc69be7b810769c1d99f500 Mon Sep 17 00:00:00 2001 From: Marianne Feng Date: Mon, 6 Nov 2017 16:19:47 -0800 Subject: [PATCH] added pretty print option for json logs --- README.md | 2 ++ json_formatter.go | 13 ++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5f656c3..f13335e 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # Logrus :walrus: [![Build Status](https://travis-ci.org/sirupsen/logrus.svg?branch=master)](https://travis-ci.org/sirupsen/logrus) [![GoDoc](https://godoc.org/github.com/sirupsen/logrus?status.svg)](https://godoc.org/github.com/sirupsen/logrus) +**this is an exact clone of the Logrus repo [here](https://github.com/sirupsen/logrus), the only change that has been made is json formatter takes in a setting to pretty print json logs for easier debugging** + Logrus is a structured logger for Go (golang), completely API compatible with the standard library logger. diff --git a/json_formatter.go b/json_formatter.go index fb01c1b..e52ab17 100644 --- a/json_formatter.go +++ b/json_formatter.go @@ -43,6 +43,9 @@ type JSONFormatter struct { // }, // } FieldMap FieldMap + + // PrettyPrint will indent all json logs + PrettyPrint bool } // Format renders a single log entry @@ -71,7 +74,15 @@ func (f *JSONFormatter) Format(entry *Entry) ([]byte, error) { data[f.FieldMap.resolve(FieldKeyMsg)] = entry.Message data[f.FieldMap.resolve(FieldKeyLevel)] = entry.Level.String() - serialized, err := json.Marshal(data) + var serialized []byte + var err error + + if f.PrettyPrint { + serialized, err = json.MarshalIndent(data, "", "\t") + } else { + serialized, err = json.Marshal(data) + } + if err != nil { return nil, fmt.Errorf("Failed to marshal fields to JSON, %v", err) }