Skip to content

Commit f8ceaa3

Browse files
feat : color logger
Signed-off-by: Archisman <[email protected]>
1 parent 0445dca commit f8ceaa3

File tree

6 files changed

+79
-4
lines changed

6 files changed

+79
-4
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,5 @@ If you want to delete the provisioned cluster, then execute : `make delete-provi
9191
- [How can you call a helm 'helper' template from a subchart with the correct context?](https://stackoverflow.com/questions/47791971/how-can-you-call-a-helm-helper-template-from-a-subchart-with-the-correct-conte)
9292

9393
- [IRSA for non EKS Clusters | PlatformCon 2023](https://www.youtube.com/watch?v=otmLHWW3Tos)
94+
95+
- [Logging in Go with Slog: The Ultimate Guide](https://betterstack.com/community/guides/logging/logging-in-go/)

flake.nix

-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@
8585
azure-cli
8686

8787
k9s
88-
go-task
8988
];
9089
};
9190
}

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ require (
1919
github.com/aws/aws-sdk-go-v2/service/s3 v1.78.2
2020
github.com/aws/aws-sdk-go-v2/service/sts v1.33.17
2121
github.com/creasty/defaults v1.8.0
22+
github.com/fatih/color v1.18.0
2223
github.com/go-git/go-git/v5 v5.14.0
2324
github.com/go-logr/logr v1.4.2
2425
github.com/go-playground/validator/v10 v10.25.0
@@ -114,7 +115,6 @@ require (
114115
github.com/evanphx/json-patch/v5 v5.9.11 // indirect
115116
github.com/exponent-io/jsonpath v0.0.0-20210407135951-1de76d718b3f // indirect
116117
github.com/fatih/camelcase v1.0.0 // indirect
117-
github.com/fatih/color v1.18.0 // indirect
118118
github.com/felixge/httpsnoop v1.0.4 // indirect
119119
github.com/fsnotify/fsnotify v1.8.0 // indirect
120120
github.com/fvbommel/sortorder v1.1.0 // indirect

pkg/utils/kubernetes/helm.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ type HelmInstallArgs struct {
2525
// We clone the KubeAid repository locally and then use absolute path to one of it's Helm chart
2626
// (like argo-cd / sealed-secrets), to install that corresponding Helm chart.
2727
func HelmInstall(ctx context.Context, args *HelmInstallArgs) {
28-
ctx = logger.AppendSlogAttributesToCtx(ctx, []slog.Attr{slog.String("release-name", args.ReleaseName)})
28+
ctx = logger.AppendSlogAttributesToCtx(ctx, []slog.Attr{
29+
slog.String("release-name", args.ReleaseName),
30+
})
2931

3032
settings := cli.New()
3133

pkg/utils/logger/logger.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,13 @@ func InitLogger(isDebugModeEnabled bool) {
3131
},
3232
})
3333

34-
logger := slog.New(withContextualSlogAttributesHandler(textHandler))
34+
logger := slog.New(
35+
withContextualSlogAttributesHandler(withColorHandler(
36+
os.Stderr,
37+
textHandler,
38+
isDebugModeEnabled,
39+
)),
40+
)
3541
slog.SetDefault(logger)
3642

3743
// Initialize controller-runtime's (or kubebuilder's) base logger with the default slog logger.
+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package logger
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"io"
7+
"log"
8+
"log/slog"
9+
10+
"github.com/fatih/color"
11+
)
12+
13+
type ColorHandler struct {
14+
slog.Handler
15+
logLogger *log.Logger
16+
isDebugModeEnabled bool
17+
}
18+
19+
func (c *ColorHandler) Handle(ctx context.Context, record slog.Record) error {
20+
logParts := []any{}
21+
22+
// Time (only shown in debug mode).
23+
if c.isDebugModeEnabled {
24+
logParts = append(logParts,
25+
fmt.Sprintf("(%s)", record.Time.Format("15:04")),
26+
)
27+
}
28+
29+
// Log level.
30+
logLevel := record.Level.String() + " :"
31+
switch record.Level {
32+
case slog.LevelDebug:
33+
logLevel = color.MagentaString(logLevel)
34+
case slog.LevelInfo:
35+
logLevel = color.GreenString(logLevel)
36+
case slog.LevelWarn:
37+
logLevel = color.YellowString(logLevel)
38+
case slog.LevelError:
39+
logLevel = color.RedString(logLevel)
40+
}
41+
logParts = append(logParts, logLevel)
42+
43+
// Message.
44+
message := color.WhiteString(record.Message)
45+
logParts = append(logParts, message)
46+
47+
// Attributes.
48+
record.Attrs(func(attribute slog.Attr) bool {
49+
logParts = append(logParts,
50+
fmt.Sprintf("%s=%s", color.CyanString(attribute.Key), attribute.Value.String()),
51+
)
52+
53+
return true
54+
})
55+
56+
c.logLogger.Println(logParts...)
57+
return nil
58+
}
59+
60+
func withColorHandler(out io.Writer, handler slog.Handler, isDebugModeEnabled bool) *ColorHandler {
61+
return &ColorHandler{
62+
Handler: handler,
63+
logLogger: log.New(out, "", 0),
64+
isDebugModeEnabled: isDebugModeEnabled,
65+
}
66+
}

0 commit comments

Comments
 (0)