✨ Pretty print JSON
This commit is contained in:
parent
4c0edcd1a5
commit
1962e1db50
2 changed files with 39 additions and 11 deletions
|
|
@ -1,30 +1,44 @@
|
|||
package formatters
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"codeberg.org/danjones000/my-log/config"
|
||||
"codeberg.org/danjones000/my-log/models"
|
||||
//"codeberg.org/danjones000/my-log/tools"
|
||||
)
|
||||
|
||||
func newJson(ff config.Formatters) (Formatter, error) {
|
||||
// @todo pretty print
|
||||
return &Json{ff.Json()}, nil
|
||||
return &Json{ff.Json().PrettyPrint}, nil
|
||||
}
|
||||
|
||||
type Json struct {
|
||||
jf config.JsonFormat
|
||||
prettPrint bool
|
||||
}
|
||||
|
||||
func (js *Json) Name() string {
|
||||
return "json"
|
||||
}
|
||||
|
||||
func (js *Json) marshal(v any) (o []byte, err error) {
|
||||
o, err = json.Marshal(v)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if js.prettPrint {
|
||||
buff := &bytes.Buffer{}
|
||||
err = json.Indent(buff, o, "", "\t")
|
||||
if err == nil {
|
||||
o = buff.Bytes()
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (js *Json) Meta(m models.Meta) ([]byte, error) {
|
||||
o := map[string]any{m.Key: m.Value}
|
||||
return json.Marshal(o)
|
||||
return js.marshal(o)
|
||||
}
|
||||
|
||||
func (js *Json) entryMap(e models.Entry) map[string]any {
|
||||
|
|
@ -39,7 +53,7 @@ func (js *Json) entryMap(e models.Entry) map[string]any {
|
|||
}
|
||||
|
||||
func (js *Json) Entry(e models.Entry) ([]byte, error) {
|
||||
return json.Marshal(js.entryMap(e))
|
||||
return js.marshal(js.entryMap(e))
|
||||
}
|
||||
|
||||
func (js *Json) Log(l models.Log) ([]byte, error) {
|
||||
|
|
@ -59,5 +73,5 @@ func (js *Json) Logs(logs []models.Log) (out []byte, err error) {
|
|||
}
|
||||
o[l.Name] = es
|
||||
}
|
||||
return json.Marshal(o)
|
||||
return js.marshal(o)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue