Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ func main() {

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

if log.Level() == "dev" {
log.Dev("Welcome back Developer!")
log.Dev("CTRL+S config to Print to Console")
}

proxy = reverseProxy.Create(ENV.API_URL)

handler := proxy.Init()
Expand Down
69 changes: 69 additions & 0 deletions utils/logger/levels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package logger

import (
"image/color"
"strconv"
"strings"

"go.uber.org/zap/zapcore"
)

const DeveloperLevel zapcore.Level = -2

func ParseLevel(s string) zapcore.Level {
switch strings.ToLower(s) {
case "dev":
return DeveloperLevel
case "debug":
return zapcore.DebugLevel
case "info":
return zapcore.InfoLevel
case "warn":
return zapcore.WarnLevel
case "error":
return zapcore.ErrorLevel
case "fatal":
return zapcore.FatalLevel
default:
return zapcore.InfoLevel
}
}

func ColorCode(str string, color color.RGBA) string {
r, g, b := color.R, color.G, color.B

red, green, blue := int(r), int(g), int(b)

colorStr := strconv.Itoa(red) + ";" + strconv.Itoa(green) + ";" + strconv.Itoa(blue)

return "\x1b[38;2;" + colorStr + "m" + str + "\x1b[0m"
}

func LevelString(l zapcore.Level) string {
switch l {
case DeveloperLevel:
return "dev"
default:
return l.CapitalString()
}
}

func CapitalLevel(l zapcore.Level) string {
switch l {
case DeveloperLevel:
return ColorCode("DEV ", color.RGBA{
R: 95, G: 175, B: 135,
})
default:
return l.CapitalString()
}
}

func CustomEncodeLevel(l zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
switch l {
case DeveloperLevel:
enc.AppendString(CapitalLevel(l))
default:
zapcore.CapitalColorLevelEncoder(l, enc)
}
}
33 changes: 8 additions & 25 deletions utils/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var _logLevel = ""
func Init(level string) {
_logLevel = strings.ToLower(level)

logLevel := getLogLevel(_logLevel)
logLevel := ParseLevel(_logLevel)

cfg := zap.Config{
Level: zap.NewAtomicLevelAt(logLevel),
Expand All @@ -29,7 +29,7 @@ func Init(level string) {
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.CapitalColorLevelEncoder,
EncodeLevel: CustomEncodeLevel,
EncodeTime: zapcore.TimeEncoderOfLayout("02.01 15:04"),
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
Expand All @@ -43,31 +43,12 @@ func Init(level string) {
_log, err = cfg.Build(zap.AddCaller(), zap.AddCallerSkip(1))

if err != nil {
fmt.Println("Encountered Error during Log.Init(): err.Error()")
}
}

func getLogLevel(level string) zapcore.Level {
switch level {
case "info":
return zapcore.InfoLevel
case "debug":
return zapcore.DebugLevel
case "dev":
return zapcore.DebugLevel
case "warn":
return zapcore.WarnLevel
case "error":
return zapcore.ErrorLevel
case "fatal":
return zapcore.FatalLevel
default:
return zapcore.InfoLevel
fmt.Println("Encountered Error during Log.Init(): ", err.Error())
}
}

func Level() string {
return _log.Level().String()
return LevelString(_log.Level())
}

func Info(msg ...string) {
Expand All @@ -79,8 +60,10 @@ func Debug(msg ...string) {
}

func Dev(msg ...string) {
if _logLevel == "dev" {
_log.Debug(strings.Join(msg, ""))
ok := _log.Check(DeveloperLevel, strings.Join(msg, ""))

if ok != nil {
ok.Write()
}
}

Expand Down