log: json output is now colored on when tty is available
This commit is contained in:
parent
ee2e307f6a
commit
0d849ec172
2
Makefile
2
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
|
||||
|
|
1
go.mod
1
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
|
||||
|
|
2
go.sum
2
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=
|
||||
|
|
|
@ -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')
|
||||
|
||||
|
|
Loading…
Reference in a new issue