Skip to content

Commit fa2603d

Browse files
authored
Merge pull request #16 from fortio/simpler_color
Simplified color by relying on log.Colors to be cleared when color mode is off
2 parents afa8311 + 82b8725 commit fa2603d

File tree

5 files changed

+28
-29
lines changed

5 files changed

+28
-29
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@ manual-check:
88
go run ./levelsDemo 2>&1 | TZ=UTC go run -race .
99
@echo "=========== Without Timestamp nor go Routine ID: ==========="
1010
go run ./levelsDemo -logger-timestamp=false -logger-goroutine=false 2>&1 | go run -race .
11+
@echo "=========== Without Color: ==========="
12+
go run ./levelsDemo 2>&1 | go run -race . -no-color
1113

1214
.PHONY: tests manual-check

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ module fortio.org/logc
33
go 1.18
44

55
require (
6-
fortio.org/cli v1.2.0
7-
fortio.org/log v1.8.1
8-
fortio.org/scli v1.9.0
6+
fortio.org/cli v1.3.0
7+
fortio.org/log v1.9.1
8+
fortio.org/scli v1.10.0
99
)
1010

1111
require (
1212
fortio.org/dflag v1.5.2 // indirect
1313
fortio.org/sets v1.0.3 // indirect
1414
fortio.org/version v1.0.2 // indirect
1515
github.com/fsnotify/fsnotify v1.6.0 // indirect
16-
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 // indirect
16+
golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b // indirect
1717
golang.org/x/sys v0.10.0 // indirect
1818
)

go.sum

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
fortio.org/assert v1.1.4 h1:Za1RaG+OjsTMpQS3J3UCvTF6wc4+IOHCz+jAOU37Y4o=
2-
fortio.org/cli v1.2.0 h1:MXmbYnbNHcclf0xYLZjeCnTINby7FpooJxR01+hnBmM=
3-
fortio.org/cli v1.2.0/go.mod h1:zJwHjRnvVqneF7ES+REfElc33xMjhbRDJslSrqIK9Kk=
2+
fortio.org/cli v1.3.0 h1:OsM2P/OIjHFa47mUN914v32ZmgQKJdVrvyiKIYQSaVw=
3+
fortio.org/cli v1.3.0/go.mod h1:eg30mF10yx+T9z6fr1ES87KCfmGaebnhnhbqc8t3IBo=
44
fortio.org/dflag v1.5.2 h1:F9XVRj4Qr2IbJP7BMj7XZc9wB0Q/RZ61Ool+4YPVad8=
55
fortio.org/dflag v1.5.2/go.mod h1:ppb/A8u+KKg+qUUYZNYuvRnXuVb8IsdHb/XGzsmjkN8=
6-
fortio.org/log v1.8.1 h1:Ybtjp+qDXb124ESnZ0XVuXJ6khDrWcom/C55YPupoKg=
7-
fortio.org/log v1.8.1/go.mod h1:u/8/2lyczXq52aT5Nw6reD+3cR6m/EbS2jBiIYhgiTU=
8-
fortio.org/scli v1.9.0 h1:laTn/mACr1+TRKH3ZKaQWw58kTzQ1ntpg2sH02KUJAA=
9-
fortio.org/scli v1.9.0/go.mod h1:Ox3nhp3eJpF2rQsZ8fBKXqdJGQPTQHWEZpLikDnkzjY=
6+
fortio.org/log v1.9.1 h1:yNxHqTL6g9VdT38g/BehUZkNOuczxnl2YsdTyENn5YM=
7+
fortio.org/log v1.9.1/go.mod h1:u/8/2lyczXq52aT5Nw6reD+3cR6m/EbS2jBiIYhgiTU=
8+
fortio.org/scli v1.10.0 h1:0OGr8g7mcfQ0UXgTODwiw4XoWdOgkbzU+8GIH9Qo4P8=
9+
fortio.org/scli v1.10.0/go.mod h1:T7M7THNaLG58ryqCc7E87dwBPXfraz3k+7HngNlVwWA=
1010
fortio.org/sets v1.0.3 h1:HzewdGjH69YmyW06yzplL35lGr+X4OcqQt0qS6jbaO4=
1111
fortio.org/sets v1.0.3/go.mod h1:QZVj0r6KP/ZD9ebySW9SgxVNy/NjghUfyHW9NN+WU+4=
1212
fortio.org/version v1.0.2 h1:8NwxdX58aoeKx7T5xAPO0xlUu1Hpk42nRz5s6e6eKZ0=
1313
fortio.org/version v1.0.2/go.mod h1:2JQp9Ax+tm6QKiGuzR5nJY63kFeANcgrZ0osoQFDVm0=
1414
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
1515
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
16-
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 h1:MGwJjxBy0HJshjDNfLsYO8xppfqWlA5ZT9OhtUUhTNw=
17-
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
16+
golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b h1:r+vk0EmXNmekl0S0BascoeeoHk/L7wmaW2QF90K+kYI=
17+
golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
1818
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1919
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
2020
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

logc.go

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,25 @@ func GetAttributes(line string) string {
5555
}
5656

5757
func main() {
58-
noColorFlag := flag.Bool("no-color", false, "Do not colorize output")
58+
noColorFlag := flag.Bool("no-color", false, "Do not colorize output (same as -logger-no-color)")
5959
cli.ArgsHelp = " < log.json\nor for instance\n\tfortio server 2>&1 | logc\n" +
6060
"to convert JSON fortio logger lines from stdin to (ansi) colorized text"
6161
cli.Main()
6262
// read stdin line by line
6363
scanner := bufio.NewScanner(os.Stdin)
6464
prevDate := time.UnixMilli(0)
65-
noColor := *noColorFlag
65+
if *noColorFlag {
66+
log.Debugf("Disabling color - was %v", log.Color)
67+
log.Config.ConsoleColor = false
68+
log.SetColorMode()
69+
}
6670
for scanner.Scan() {
6771
line := scanner.Bytes()
68-
ProcessLogLine(os.Stdout, &prevDate, noColor, line)
72+
ProcessLogLine(os.Stdout, &prevDate, line)
6973
}
7074
}
7175

72-
func ProcessLogLine(w io.Writer, prevDate *time.Time, noColor bool, line []byte) {
76+
func ProcessLogLine(w io.Writer, prevDate *time.Time, line []byte) {
7377
// json deserialize
7478
e := log.JSONEntry{}
7579
err := json.Unmarshal(line, &e)
@@ -79,17 +83,8 @@ func ProcessLogLine(w io.Writer, prevDate *time.Time, noColor bool, line []byte)
7983
return
8084
}
8185
tsStr := ""
82-
reset := log.Colors.Reset
83-
darkGrey := log.Colors.DarkGray
84-
grey := log.Colors.Gray
8586
// uppercase single letter level + color extraction
8687
lvl, color := LevelToColor(e.Level)
87-
if noColor {
88-
color = ""
89-
reset = ""
90-
darkGrey = ""
91-
grey = ""
92-
}
9388
if e.TS != 0 {
9489
ts := e.Time()
9590
// Each time the day changes we print a header
@@ -98,15 +93,15 @@ func ProcessLogLine(w io.Writer, prevDate *time.Time, noColor bool, line []byte)
9893
*prevDate = ts
9994
}
10095
// Use full microseconds resolution unlike the log built in color version which stops at millis.
101-
tsStr = ts.Format(darkGrey + "15:04:05.000000 ")
96+
tsStr = ts.Format(log.Colors.DarkGray + "15:04:05.000000 ")
10297
}
10398
if e.R > 0 {
104-
tsStr += fmt.Sprintf(grey+"[%d] ", e.R)
99+
tsStr += fmt.Sprintf(log.Colors.Gray+"[%d] ", e.R)
105100
}
106101
fileLine := ""
107102
if e.Line != 0 {
108103
fileLine = fmt.Sprintf("%s:%d> ", e.File, e.Line)
109104
}
110105
// Msg can be multi line.
111-
fmt.Fprintf(w, "%s%s%s %s%s%s%s\n", tsStr, color, lvl, fileLine, e.Msg, GetAttributes(string(line)), reset)
106+
fmt.Fprintf(w, "%s%s%s %s%s%s%s\n", tsStr, color, lvl, fileLine, e.Msg, GetAttributes(string(line)), log.Colors.Reset)
112107
}

logc_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ func TestGetAttributes(t *testing.T) {
2929

3030
func TestLevels(t *testing.T) {
3131
var zeroTime time.Time
32+
log.Config.ForceColor = true
33+
log.SetColorMode()
3234
for _, tc := range []struct {
3335
in string
3436
want string
@@ -38,7 +40,7 @@ func TestLevels(t *testing.T) {
3840
} {
3941
buf := &strings.Builder{}
4042
w := bufio.NewWriter(buf)
41-
ProcessLogLine(w, &zeroTime, false, []byte(tc.in))
43+
ProcessLogLine(w, &zeroTime, []byte(tc.in))
4244
w.Flush()
4345
got := buf.String()
4446
if got != tc.want {

0 commit comments

Comments
 (0)