Skip to content

Commit

Permalink
log: Add red colour to CmdLogger error messages (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
NeilLuno authored Jan 31, 2025
1 parent 673a166 commit 38218cb
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 9 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.23.4

require (
github.com/dave/dst v0.27.3
github.com/fatih/color v1.18.0
github.com/go-stack/stack v1.8.1
github.com/golang/protobuf v1.5.4
github.com/sebdah/goldie/v2 v2.5.5
Expand All @@ -18,6 +19,8 @@ require (
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.8.1 // indirect
github.com/sergi/go-diff v1.2.0 // indirect
Expand Down
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/dave/jennifer v1.5.0/go.mod h1:4MnyiFIlZS3l5tSDn8VnzE6ffAhYBMB2SZntBs
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
Expand All @@ -26,6 +28,11 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down Expand Up @@ -59,6 +66,8 @@ golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
Expand Down
4 changes: 3 additions & 1 deletion log/cmdlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"log"
"strings"

"github.com/fatih/color"

"github.com/luno/jettison/models"
)

Expand Down Expand Up @@ -93,7 +95,7 @@ func conciseSource(source string) string {

func writeError(w io.Writer, err ErrorObject) {
ps := parameterString(err.Parameters)
_, _ = fmt.Fprintf(w, " %s%s", err.Message, ps)
_, _ = fmt.Fprintf(w, " 🚨 %s%s", color.HiRedString(err.Message), ps)
if len(err.StackTrace.Content()) == 0 {
_, _ = fmt.Fprint(w, "(error without stack trace)")
}
Expand Down
2 changes: 2 additions & 0 deletions log/cmdlogger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"github.com/luno/jettison/log"
)

//go:generate go test . -run TestCmdLogger -update

func TestCmdLogger(t *testing.T) {
var buf bytes.Buffer
log.SetCmdLoggerForTesting(t, &buf)
Expand Down
16 changes: 8 additions & 8 deletions log/testdata/cmd_logger.golden
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
I 00:00:00.000 g/l/j/log/cmdlogger_test.go:23: this is an info message[ctx_key=ctx_val,info_key=info_val]
E 00:00:00.000 g/l/j/log/cmdlogger_test.go:24: error(s) [ctx_key=ctx_val]
EOF(error without stack trace)
E 00:00:00.000 g/l/j/log/cmdlogger_test.go:25: error(s) [ctx_key=ctx_val,error_key=error_val]
example error[error_key=error_val]
I 00:00:00.000 g/l/j/log/cmdlogger_test.go:25: this is an info message[ctx_key=ctx_val,info_key=info_val]
E 00:00:00.000 g/l/j/log/cmdlogger_test.go:26: error(s) [ctx_key=ctx_val]
🚨 EOF(error without stack trace)
E 00:00:00.000 g/l/j/log/cmdlogger_test.go:27: error(s) [ctx_key=ctx_val,error_key=error_val]
🚨 example error[error_key=error_val]
- cmdlogger_test.go TestCmdLogger
E 00:00:00.000 g/l/j/log/cmdlogger_test.go:31: error(s) [ctx_key=ctx_val]
error one
E 00:00:00.000 g/l/j/log/cmdlogger_test.go:33: error(s) [ctx_key=ctx_val]
🚨 error one
- cmdlogger_test.go TestCmdLogger
error two
🚨 error two
- cmdlogger_test.go TestCmdLogger

0 comments on commit 38218cb

Please sign in to comment.