Skip to content

Commit 7ef97dd

Browse files
committed
Convert sloghuman to key=value pair format
This format is consistent with Go's new official `slog`, `zerolog` and many others. It is also far more readable than JSON, and a growing number of libaries and tools are able to parse it.
1 parent 22f02e1 commit 7ef97dd

File tree

8 files changed

+165
-179
lines changed

8 files changed

+165
-179
lines changed

go.mod

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
11
module cdr.dev/slog
22

3-
go 1.13
3+
go 1.20
44

55
require (
66
cloud.google.com/go v0.26.0
7-
github.com/alecthomas/chroma v0.10.0
8-
github.com/fatih/color v1.13.0
7+
github.com/charmbracelet/lipgloss v0.7.1
98
github.com/google/go-cmp v0.5.3
9+
github.com/muesli/termenv v0.15.1
1010
go.opencensus.io v0.24.0
11-
go.uber.org/goleak v1.2.1 // indirect
11+
go.uber.org/goleak v1.2.1
1212
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
1313
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2
1414
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013
1515
)
16+
17+
require (
18+
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
19+
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
20+
github.com/golang/protobuf v1.4.3 // indirect
21+
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
22+
github.com/mattn/go-isatty v0.0.17 // indirect
23+
github.com/mattn/go-runewidth v0.0.14 // indirect
24+
github.com/muesli/reflow v0.3.0 // indirect
25+
github.com/rivo/uniseg v0.2.0 // indirect
26+
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b // indirect
27+
golang.org/x/sys v0.6.0 // indirect
28+
golang.org/x/text v0.3.3 // indirect
29+
google.golang.org/grpc v1.33.2 // indirect
30+
google.golang.org/protobuf v1.25.0 // indirect
31+
)

go.sum

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
cloud.google.com/go v0.26.0 h1:e0WKqKTd5BnrG8aKH3J3h+QvEIQtSUcf2n5UZ5ZgLtQ=
22
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
33
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
4-
github.com/alecthomas/chroma v0.10.0 h1:7XDcGkCQopCNKjZHfYrNLraA+M7e0fMiJ/Mfikbfjek=
5-
github.com/alecthomas/chroma v0.10.0/go.mod h1:jtJATyUxlIORhUOFNA9NZDWGAQ8wpxQQqNSB4rjA/1s=
4+
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
5+
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
66
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
7+
github.com/charmbracelet/lipgloss v0.7.1 h1:17WMwi7N1b1rVWOjMT+rCh7sQkvDU75B2hbZpc5Kc1E=
8+
github.com/charmbracelet/lipgloss v0.7.1/go.mod h1:yG0k3giv8Qj8edTCbbg6AlQ5e8KNWpFujkNawKNhE2c=
79
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
810
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
911
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1012
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1113
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
12-
github.com/dlclark/regexp2 v1.4.0 h1:F1rxgk7p4uKjwIQxBs9oAXe5CqrXlCduYEJvrF4u93E=
13-
github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
1414
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
1515
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
1616
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
1717
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
18-
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
19-
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
2018
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
2119
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e h1:1r7pUrabqp18hOBcwBwiTsbnFeTZHV9eER/QT5JVZxY=
2220
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
@@ -39,21 +37,26 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
3937
github.com/google/go-cmp v0.5.3 h1:x95R7cp+rSeeqAMI2knLtQ0DKlaBhv2NrtrOvafPHRo=
4038
github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
4139
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
42-
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
43-
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
44-
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
45-
github.com/mattn/go-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U=
46-
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
47-
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
48-
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
49-
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
40+
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
41+
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
42+
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
43+
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
44+
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
45+
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
46+
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
47+
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
48+
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
49+
github.com/muesli/termenv v0.15.1 h1:UzuTb/+hhlBugQz28rpzey4ZuKcZ03MeKsoG7IJZIxs=
50+
github.com/muesli/termenv v0.15.1/go.mod h1:HeAQPTzpfs016yGtA4g00CsdYnVLJvxsS4ANqrZs2sQ=
5051
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5152
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5253
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
54+
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
55+
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
56+
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
5357
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
5458
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
5559
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
56-
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
5760
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
5861
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
5962
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
@@ -83,11 +86,10 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ
8386
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
8487
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
8588
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
86-
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
87-
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
8889
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
89-
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
90-
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
90+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
91+
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
92+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
9193
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
9294
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
9395
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
@@ -121,9 +123,7 @@ google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2
121123
google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
122124
google.golang.org/protobuf v1.25.0 h1:Ejskq+SyPohKW+1uil0JJMtmHCgJPJ/qWTxr8qp+R4c=
123125
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
124-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
125126
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
126-
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
127127
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
128128
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
129129
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/entryhuman/entry.go

Lines changed: 57 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@ package entryhuman
44

55
import (
66
"bytes"
7-
"encoding/json"
87
"fmt"
98
"io"
109
"os"
11-
"path/filepath"
12-
"runtime/debug"
1310
"strconv"
1411
"strings"
1512
"time"
1613

17-
"github.com/fatih/color"
14+
"github.com/charmbracelet/lipgloss"
15+
"github.com/muesli/termenv"
1816
"go.opencensus.io/trace"
1917
"golang.org/x/crypto/ssh/terminal"
2018
"golang.org/x/xerrors"
@@ -34,13 +32,20 @@ func StripTimestamp(ent string) (time.Time, string, error) {
3432
// TimeFormat is a simplified RFC3339 format.
3533
const TimeFormat = "2006-01-02 15:04:05.000"
3634

37-
func c(w io.Writer, attrs ...color.Attribute) *color.Color {
38-
c := color.New(attrs...)
39-
c.DisableColor()
35+
var (
36+
renderer = lipgloss.NewRenderer(os.Stdout, termenv.WithUnsafe())
37+
38+
loggerNameStyle = renderer.NewStyle().Foreground(lipgloss.Color("#A47DFF"))
39+
keyStyle = renderer.NewStyle().Foreground(lipgloss.Color("#606366"))
40+
multiLineKeyStyle = renderer.NewStyle().Foreground(lipgloss.Color("#79b8ff"))
41+
)
42+
43+
func render(w io.Writer, st lipgloss.Style, s string) string {
4044
if shouldColor(w) {
41-
c.EnableColor()
45+
ss := st.Render(s)
46+
return ss
4247
}
43-
return c
48+
return s
4449
}
4550

4651
// Fmt returns a human readable format for ent.
@@ -50,36 +55,36 @@ func c(w io.Writer, attrs ...color.Attribute) *color.Color {
5055
// We also do not indent the fields as go's test does that automatically
5156
// for extra lines in a log so if we did it here, the fields would be indented
5257
// twice in test logs. So the Stderr logger indents all the fields itself.
53-
func Fmt(w io.Writer, ent slog.SinkEntry) string {
54-
ents := c(w, color.Reset).Sprint("")
58+
func Fmt(buf io.StringWriter, termW io.Writer, ent slog.SinkEntry,
59+
) {
5560
ts := ent.Time.Format(TimeFormat)
56-
ents += ts + " "
61+
buf.WriteString(ts + " ")
5762

58-
level := "[" + ent.Level.String() + "]"
59-
level = c(w, levelColor(ent.Level)).Sprint(level)
60-
ents += fmt.Sprintf("%v\t", level)
63+
level := ent.Level.String()
64+
if len(level) > 4 {
65+
level = level[:4]
66+
}
67+
level = "[" + level + "]"
68+
buf.WriteString(render(termW, levelStyle(ent.Level), level))
69+
buf.WriteString("\t")
6170

6271
if len(ent.LoggerNames) > 0 {
6372
loggerName := "(" + quoteKey(strings.Join(ent.LoggerNames, ".")) + ")"
64-
loggerName = c(w, color.FgMagenta).Sprint(loggerName)
65-
ents += fmt.Sprintf("%v\t", loggerName)
73+
buf.WriteString(render(termW, loggerNameStyle, loggerName))
74+
buf.WriteString("\t")
6675
}
6776

68-
hpath, hfn := humanPathAndFunc(ent.File, ent.Func)
69-
loc := fmt.Sprintf("<%v:%v>\t%v", hpath, ent.Line, hfn)
70-
loc = c(w, color.FgCyan).Sprint(loc)
71-
ents += fmt.Sprintf("%v\t", loc)
72-
7377
var multilineKey string
7478
var multilineVal string
7579
msg := strings.TrimSpace(ent.Message)
7680
if strings.Contains(msg, "\n") {
7781
multilineKey = "msg"
7882
multilineVal = msg
7983
msg = "..."
84+
msg = quote(msg)
85+
buf.WriteString(msg)
86+
8087
}
81-
msg = quote(msg)
82-
ents += msg
8388

8489
if ent.SpanContext != (trace.SpanContext{}) {
8590
ent.Fields = append(slog.M(
@@ -111,48 +116,56 @@ func Fmt(w io.Writer, ent slog.SinkEntry) string {
111116
multilineVal = s
112117
}
113118

114-
if len(ent.Fields) > 0 {
115-
// No error is guaranteed due to slog.Map handling errors itself.
116-
fields, _ := json.MarshalIndent(ent.Fields, "", "")
117-
fields = bytes.ReplaceAll(fields, []byte(",\n"), []byte(", "))
118-
fields = bytes.ReplaceAll(fields, []byte("\n"), []byte(""))
119-
fields = formatJSON(w, fields)
120-
ents += "\t" + string(fields)
119+
for i, f := range ent.Fields {
120+
if i < len(ent.Fields) {
121+
buf.WriteString("\t")
122+
}
123+
buf.WriteString(render(termW, keyStyle, f.Name+"="))
124+
valueStr := fmt.Sprintf("%+v", f.Value)
125+
buf.WriteString(quote(valueStr))
121126
}
122127

123128
if multilineVal != "" {
124129
if msg != "..." {
125-
ents += " ..."
130+
buf.WriteString(" ...")
126131
}
127132

128133
// Proper indentation.
129134
lines := strings.Split(multilineVal, "\n")
130135
for i, line := range lines[1:] {
131136
if line != "" {
132-
lines[i+1] = c(w, color.Reset).Sprint("") + strings.Repeat(" ", len(multilineKey)+4) + line
137+
lines[i+1] = strings.Repeat(" ", len(multilineKey)+4) + line
133138
}
134139
}
135140
multilineVal = strings.Join(lines, "\n")
136141

137-
multilineKey = c(w, color.FgBlue).Sprintf(`"%v"`, multilineKey)
138-
ents += fmt.Sprintf("\n%v: %v", multilineKey, multilineVal)
142+
multilineKey = render(termW, multiLineKeyStyle, multilineKey)
143+
buf.WriteString("\n")
144+
buf.WriteString(multilineKey)
145+
buf.WriteString("= ")
146+
buf.WriteString(multilineVal)
139147
}
140-
141-
return ents
142148
}
143149

144-
func levelColor(level slog.Level) color.Attribute {
150+
var (
151+
levelDebugStyle = renderer.NewStyle().Foreground(lipgloss.Color("#ffffff"))
152+
levelInfoStyle = renderer.NewStyle().Foreground(lipgloss.Color("#0091FF"))
153+
levelWarnStyle = renderer.NewStyle().Foreground(lipgloss.Color("#FFCF0D"))
154+
levelErrorStyle = renderer.NewStyle().Foreground(lipgloss.Color("#FF5A0D"))
155+
)
156+
157+
func levelStyle(level slog.Level) lipgloss.Style {
145158
switch level {
146159
case slog.LevelDebug:
147-
return color.Reset
160+
return levelDebugStyle
148161
case slog.LevelInfo:
149-
return color.FgBlue
162+
return levelInfoStyle
150163
case slog.LevelWarn:
151-
return color.FgYellow
152-
case slog.LevelError:
153-
return color.FgRed
164+
return levelWarnStyle
165+
case slog.LevelError, slog.LevelFatal, slog.LevelCritical:
166+
return levelErrorStyle
154167
default:
155-
return color.FgHiRed
168+
panic("unknown level")
156169
}
157170
}
158171

@@ -196,64 +209,3 @@ func quoteKey(key string) string {
196209
// Replace spaces in the map keys with underscores.
197210
return strings.ReplaceAll(key, " ", "_")
198211
}
199-
200-
var mainPackagePath string
201-
var mainModulePath string
202-
203-
func init() {
204-
// Unfortunately does not work for tests yet :(
205-
// See https://github.com/golang/go/issues/33976
206-
bi, ok := debug.ReadBuildInfo()
207-
if !ok {
208-
return
209-
}
210-
mainPackagePath = bi.Path
211-
mainModulePath = bi.Main.Path
212-
}
213-
214-
// humanPathAndFunc takes the absolute path to a file and an absolute module path to a
215-
// function in that file and returns the module path to the file. It also returns
216-
// the path to the function stripped of its module prefix.
217-
//
218-
// If the file is in the main Go module then its path is returned
219-
// relative to the main Go module's root.
220-
//
221-
// fn is from https://pkg.go.dev/runtime#Func.Name
222-
func humanPathAndFunc(filename, fn string) (hpath, hfn string) {
223-
// pkgDir is the dir of the pkg.
224-
// e.g. cdr.dev/slog/internal
225-
// base is the package name and the function name separated by a period.
226-
// e.g. entryhuman.humanPathAndFunc
227-
// There can be multiple periods when methods of types are involved.
228-
pkgDir, base := filepath.Split(fn)
229-
s := strings.Split(base, ".")
230-
pkg := s[0]
231-
hfn = strings.Join(s[1:], ".")
232-
233-
if pkg == "main" {
234-
// This happens with go build main.go
235-
if mainPackagePath == "command-line-arguments" {
236-
// Without a real mainPath, we can't find the path to the file
237-
// relative to the module. So we just return the base.
238-
return filepath.Base(filename), hfn
239-
}
240-
// Go doesn't return the full main path in runtime.Func.Name()
241-
// It just returns the path "main"
242-
// Only runtime.ReadBuildInfo returns it so we have to check and replace.
243-
pkgDir = mainPackagePath
244-
// pkg main isn't reflected on the disk so we should not add it
245-
// into the pkgpath.
246-
pkg = ""
247-
}
248-
249-
hpath = filepath.Join(pkgDir, pkg, filepath.Base(filename))
250-
251-
if mainModulePath != "" {
252-
relhpath, err := filepath.Rel(mainModulePath, hpath)
253-
if err == nil {
254-
hpath = "./" + relhpath
255-
}
256-
}
257-
258-
return hpath, hfn
259-
}

0 commit comments

Comments
 (0)