Skip to content

Commit 77e4ce1

Browse files
authored
fix/improve: custom dev loglevel (#107)
1 parent e167195 commit 77e4ce1

File tree

3 files changed

+82
-25
lines changed

3 files changed

+82
-25
lines changed

main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ func main() {
3232

3333
log.Info("Initialized Logger with Level of ", log.Level())
3434

35+
if log.Level() == "dev" {
36+
log.Dev("Welcome back Developer!")
37+
log.Dev("CTRL+S config to Print to Console")
38+
}
39+
3540
proxy = reverseProxy.Create(ENV.API_URL)
3641

3742
handler := proxy.Init()

utils/logger/levels.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package logger
2+
3+
import (
4+
"image/color"
5+
"strconv"
6+
"strings"
7+
8+
"go.uber.org/zap/zapcore"
9+
)
10+
11+
const DeveloperLevel zapcore.Level = -2
12+
13+
func ParseLevel(s string) zapcore.Level {
14+
switch strings.ToLower(s) {
15+
case "dev":
16+
return DeveloperLevel
17+
case "debug":
18+
return zapcore.DebugLevel
19+
case "info":
20+
return zapcore.InfoLevel
21+
case "warn":
22+
return zapcore.WarnLevel
23+
case "error":
24+
return zapcore.ErrorLevel
25+
case "fatal":
26+
return zapcore.FatalLevel
27+
default:
28+
return zapcore.InfoLevel
29+
}
30+
}
31+
32+
func ColorCode(str string, color color.RGBA) string {
33+
r, g, b := color.R, color.G, color.B
34+
35+
red, green, blue := int(r), int(g), int(b)
36+
37+
colorStr := strconv.Itoa(red) + ";" + strconv.Itoa(green) + ";" + strconv.Itoa(blue)
38+
39+
return "\x1b[38;2;" + colorStr + "m" + str + "\x1b[0m"
40+
}
41+
42+
func LevelString(l zapcore.Level) string {
43+
switch l {
44+
case DeveloperLevel:
45+
return "dev"
46+
default:
47+
return l.CapitalString()
48+
}
49+
}
50+
51+
func CapitalLevel(l zapcore.Level) string {
52+
switch l {
53+
case DeveloperLevel:
54+
return ColorCode("DEV ", color.RGBA{
55+
R: 95, G: 175, B: 135,
56+
})
57+
default:
58+
return l.CapitalString()
59+
}
60+
}
61+
62+
func CustomEncodeLevel(l zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
63+
switch l {
64+
case DeveloperLevel:
65+
enc.AppendString(CapitalLevel(l))
66+
default:
67+
zapcore.CapitalColorLevelEncoder(l, enc)
68+
}
69+
}

utils/logger/logger.go

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var _logLevel = ""
1414
func Init(level string) {
1515
_logLevel = strings.ToLower(level)
1616

17-
logLevel := getLogLevel(_logLevel)
17+
logLevel := ParseLevel(_logLevel)
1818

1919
cfg := zap.Config{
2020
Level: zap.NewAtomicLevelAt(logLevel),
@@ -29,7 +29,7 @@ func Init(level string) {
2929
MessageKey: "msg",
3030
StacktraceKey: "stacktrace",
3131
LineEnding: zapcore.DefaultLineEnding,
32-
EncodeLevel: zapcore.CapitalColorLevelEncoder,
32+
EncodeLevel: CustomEncodeLevel,
3333
EncodeTime: zapcore.TimeEncoderOfLayout("02.01 15:04"),
3434
EncodeDuration: zapcore.StringDurationEncoder,
3535
EncodeCaller: zapcore.ShortCallerEncoder,
@@ -43,31 +43,12 @@ func Init(level string) {
4343
_log, err = cfg.Build(zap.AddCaller(), zap.AddCallerSkip(1))
4444

4545
if err != nil {
46-
fmt.Println("Encountered Error during Log.Init(): err.Error()")
47-
}
48-
}
49-
50-
func getLogLevel(level string) zapcore.Level {
51-
switch level {
52-
case "info":
53-
return zapcore.InfoLevel
54-
case "debug":
55-
return zapcore.DebugLevel
56-
case "dev":
57-
return zapcore.DebugLevel
58-
case "warn":
59-
return zapcore.WarnLevel
60-
case "error":
61-
return zapcore.ErrorLevel
62-
case "fatal":
63-
return zapcore.FatalLevel
64-
default:
65-
return zapcore.InfoLevel
46+
fmt.Println("Encountered Error during Log.Init(): ", err.Error())
6647
}
6748
}
6849

6950
func Level() string {
70-
return _log.Level().String()
51+
return LevelString(_log.Level())
7152
}
7253

7354
func Info(msg ...string) {
@@ -79,8 +60,10 @@ func Debug(msg ...string) {
7960
}
8061

8162
func Dev(msg ...string) {
82-
if _logLevel == "dev" {
83-
_log.Debug(strings.Join(msg, ""))
63+
ok := _log.Check(DeveloperLevel, strings.Join(msg, ""))
64+
65+
if ok != nil {
66+
ok.Write()
8467
}
8568
}
8669

0 commit comments

Comments
 (0)