Skip to content

Commit

Permalink
add ansi color codes to logs (#12)
Browse files Browse the repository at this point in the history
* add ansi color codes to logs

* make ResetColor a constant

* change if/else to switch

* fix go tests

* add terminal check

* addressing feedback about naming conventions

* fix test

* adjust constants and variables for clarity

* go mod tidy

* add go.sum file

* change message test to look for substring

* updates from PR feedback

* update variable names
  • Loading branch information
dustinblack authored Jul 10, 2024
1 parent bdee9c2 commit d94e92b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 5 deletions.
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
module go.arcalot.io/log/v2

go 1.21

require (
go.arcalot.io/assert v1.8.0
golang.org/x/term v0.21.0
)

require golang.org/x/sys v0.21.0 // indirect
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
go.arcalot.io/assert v1.8.0 h1:hGcHMPncQXwQvjj7MbyOu2gg8VIBB00crUJZpeQOjxs=
go.arcalot.io/assert v1.8.0/go.mod h1:nNmWPoNUHFyrPkNrD2aASm5yPuAfiWdB/4X7Lw3ykHk=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.21.0 h1:WVXCp+/EBEHOj53Rvu+7KiT/iElMrO8ACK16SMZ3jaA=
golang.org/x/term v0.21.0/go.mod h1:ooXLefLobQVslOqselCNF4SxFAaoS6KujMbsGzSDmX0=
37 changes: 36 additions & 1 deletion message.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,36 @@ package log

import (
"fmt"
"os"
"time"

"golang.org/x/term"
)

const (
AnsiReset = "\033[0m"
AnsiDim = "\033[0;2m"
AnsiYellow = "\033[1;33m"
AnsiRed = "\033[1;31m"
)

var (
SetColor = map[Level]string{
"debug": AnsiDim,
"info": AnsiReset,
"warning": AnsiYellow,
"error": AnsiRed,
}
ResetColor = AnsiReset
)

func init() {
if !term.IsTerminal(int(os.Stderr.Fd())) {
SetColor = map[Level]string{}
ResetColor = ""
}
}

// Message is a single log message.
type Message struct {
Timestamp time.Time `json:"timestamp"`
Expand All @@ -14,5 +41,13 @@ type Message struct {
}

func (m Message) String() string {
return fmt.Sprintf("%s\t%s\t%s\t%s", m.Timestamp.Format(time.RFC3339), m.Level, m.Labels, m.Message)
return fmt.Sprintf(
"%s%s\t%s\t%s\t%s%s",
SetColor[m.Level],
m.Timestamp.Format(time.RFC3339),
m.Level,
m.Labels,
m.Message,
ResetColor,
)
}
11 changes: 8 additions & 3 deletions message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"
"time"

"go.arcalot.io/assert"
"go.arcalot.io/log/v2"
)

Expand All @@ -19,7 +20,11 @@ func TestMessage(t *testing.T) {
Labels: map[string]string{"source": "test"},
Message: "Hello world!",
}
if m.String() != "2006-01-02T15:04:05Z\terror\tsource=test\tHello world!" {
t.Fatalf("Incorrect message string: %s", m.String())
}
assert.Equals(
t,
m.String(),
log.SetColor[m.Level]+
"2006-01-02T15:04:05Z\terror\tsource=test\tHello world!"+
log.ResetColor,
)
}
2 changes: 1 addition & 1 deletion writer_golog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestGoLogger(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if !strings.Contains(buf.String(), "Hello world!\n") {
if !strings.Contains(buf.String(), "Hello world!") {
t.Fatalf("failed to find written log message in output")
}
}

0 comments on commit d94e92b

Please sign in to comment.