From 0d849ec17227cc02520b97280c617c96ea931ed2 Mon Sep 17 00:00:00 2001 From: Tigor Hutasuhut Date: Tue, 14 May 2024 18:49:06 +0700 Subject: [PATCH] log: json output is now colored on when tty is available --- Makefile | 2 +- go.mod | 1 + go.sum | 2 ++ pkg/log/pretty_handler.go | 26 ++++++++++++++++++++++++-- 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 9906ba8..1348de7 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ export REDMAGE_WEB_DEPENDENCIES_ALPINEJS_VERSION=$(shell echo "$${REDMAGE_WEB_DE export REDMAGE_RUNTIME_VERSION=$(shell echo "$${REDMAGE_RUNTIME_VERSION:-unknown}") start: dev-dependencies web-dependencies migrate-up - air + REDMAGE_RUNTIME_VERSION=$(shell git describe --tags --abbrev=0) air dev-dependencies: build-dependencies @if ! command -v air > /dev/null; then diff --git a/go.mod b/go.mod index b56ae3a..001607d 100644 --- a/go.mod +++ b/go.mod @@ -49,6 +49,7 @@ require ( github.com/qdm12/reprint v0.0.0-20200326205758-722754a53494 // indirect github.com/samber/lo v1.39.0 // indirect github.com/stephenafamo/scan v0.5.0 // indirect + github.com/tidwall/pretty v1.2.1 // indirect go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.26.0 // indirect go.opentelemetry.io/proto/otlp v1.2.0 // indirect golang.org/x/image v0.15.0 // indirect diff --git a/go.sum b/go.sum index e662de4..1380d04 100644 --- a/go.sum +++ b/go.sum @@ -374,6 +374,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/teivah/broadcast v0.1.0 h1:UMs1tn8w20Xlnod+VbLbwH3dzEH2zfJy4lxdzZjQLL0= github.com/teivah/broadcast v0.1.0/go.mod h1:mXEgvXdYz2xUkQFARxI+jyX1MfCBwMDiGjIKSAsEq1g= +github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4= +github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= github.com/volatiletech/inflect v0.0.1 h1:2a6FcMQyhmPZcLa+uet3VJ8gLn/9svWhJxJYwvE8KsU= github.com/volatiletech/inflect v0.0.1/go.mod h1:IBti31tG6phkHitLlr5j7shC5SOo//x0AjDzaJU1PLA= github.com/volatiletech/strmangle v0.0.4 h1:CxrEPhobZL/PCZOTDSH1aq7s4Kv76hQpRoTVVlUOim4= diff --git a/pkg/log/pretty_handler.go b/pkg/log/pretty_handler.go index 5da8f5c..8f10b9a 100644 --- a/pkg/log/pretty_handler.go +++ b/pkg/log/pretty_handler.go @@ -3,7 +3,6 @@ package log import ( "bytes" "context" - "encoding/json" "io" "log/slog" "os" @@ -11,6 +10,7 @@ import ( "sync" "github.com/fatih/color" + "github.com/tidwall/pretty" "github.com/tigorlazuardi/redmage/pkg/caller" ) @@ -69,6 +69,25 @@ func putBuffer(buf *bytes.Buffer) { } } +var jsonColorStyle = &pretty.Style{ + Key: [2]string{"\x1B[95m", "\x1B[0m"}, + String: [2]string{"\x1B[32m", "\x1B[0m"}, + Number: [2]string{"\x1B[33m", "\x1B[0m"}, + True: [2]string{"\x1B[36m", "\x1B[0m"}, + False: [2]string{"\x1B[36m", "\x1B[0m"}, + Null: [2]string{"\x1B[2m", "\x1B[0m"}, + Escape: [2]string{"\x1B[35m", "\x1B[0m"}, + Brackets: [2]string{"\x1B[0m", "\x1B[0m"}, + Append: pretty.TerminalStyle.Append, +} + +var jsonPrettyOpts = &pretty.Options{ + Width: 80, + Prefix: "", + Indent: " ", + SortKeys: false, +} + // Handle implements slog.Handler interface. func (pr *PrettyHandler) Handle(ctx context.Context, record slog.Record) error { var levelColor *color.Color @@ -117,7 +136,10 @@ func (pr *PrettyHandler) Handle(ctx context.Context, record slog.Record) error { serializer := pr.createSerializer(jsonBuf) _ = serializer.Handle(ctx, record) if jsonBuf.Len() > 3 { // Ignore empty json like "{}\n" - _ = json.Indent(buf, jsonBuf.Bytes(), "", " ") + jsonData := jsonBuf.Bytes() + jsonData = pretty.PrettyOptions(jsonData, jsonPrettyOpts) + jsonData = pretty.Color(jsonData, jsonColorStyle) + buf.Write(jsonData) } buf.WriteByte('\n')