Skip to content

Commit ff681f4

Browse files
committed
feat: adding colorful logger
1 parent 202ea3a commit ff681f4

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

go.mod

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ module github.com/facktoreal/logger
22

33
go 1.23.2
44

5-
require github.com/labstack/echo/v4 v4.13.3
5+
require (
6+
github.com/labstack/echo/v4 v4.13.3
7+
github.com/labstack/gommon v0.4.2
8+
)
69

710
require (
8-
github.com/labstack/gommon v0.4.2 // indirect
911
github.com/mattn/go-colorable v0.1.13 // indirect
1012
github.com/mattn/go-isatty v0.0.20 // indirect
1113
github.com/valyala/bytebufferpool v1.0.0 // indirect

logger.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@ import (
55
"fmt"
66
"log/slog"
77
"math/rand"
8+
"os"
89
"strings"
910
"time"
1011

1112
"github.com/labstack/echo/v4"
13+
"github.com/labstack/gommon/log"
14+
)
15+
16+
var (
17+
env = os.Getenv("ENV")
1218
)
1319

1420
// ContextHandler is our base context handler, it will handle all requests
@@ -138,9 +144,37 @@ func Replacer(groups []string, a slog.Attr) slog.Attr {
138144
}
139145

140146
func Errorf(ctx context.Context, format string, args ...interface{}) {
147+
if env == "dev" {
148+
log.Errorf(format, args...)
149+
return
150+
}
151+
141152
slog.ErrorContext(ctx, fmt.Sprintf(format, args...))
142153
}
143154

144155
func Infof(ctx context.Context, format string, args ...interface{}) {
156+
if env == "dev" {
157+
log.Infof(format, args...)
158+
return
159+
}
160+
145161
slog.InfoContext(ctx, fmt.Sprintf(format, args...))
146162
}
163+
164+
func Warnf(ctx context.Context, format string, args ...interface{}) {
165+
if env == "dev" {
166+
log.Warnf(format, args...)
167+
return
168+
}
169+
170+
slog.WarnContext(ctx, fmt.Sprintf(format, args...))
171+
}
172+
173+
func Debugf(ctx context.Context, format string, args ...interface{}) {
174+
if env == "dev" {
175+
log.Debugf(format, args...)
176+
return
177+
}
178+
179+
slog.DebugContext(ctx, fmt.Sprintf(format, args...))
180+
}

0 commit comments

Comments
 (0)