about summary refs log tree commit diff stats
path: root/internal/log
diff options
context:
space:
mode:
Diffstat (limited to 'internal/log')
-rw-r--r--internal/log/log.go66
1 files changed, 45 insertions, 21 deletions
diff --git a/internal/log/log.go b/internal/log/log.go
index a0f5ba7..f644e6b 100644
--- a/internal/log/log.go
+++ b/internal/log/log.go
@@ -7,30 +7,43 @@ import (
 	prettyconsole "github.com/thessem/zap-prettyconsole"
 	"go.uber.org/zap"
 	"go.uber.org/zap/zapcore"
+	"moul.io/zapfilter"
 )
 
-var logger *zap.SugaredLogger
+type Logger struct {
+	logger *zap.SugaredLogger
+}
 
-func DPanic(msg string, rest ...any) {
-	logger.DPanicw(msg, rest...)
+func (l Logger) DPanic(msg string, rest ...any) {
+	l.logger.DPanicw(msg, rest...)
 }
-func Debug(msg string, rest ...any) {
-	logger.Debugw(msg, rest...)
+func (l Logger) Debug(msg string, rest ...any) {
+	l.logger.Debugw(msg, rest...)
 }
-func Info(msg string, rest ...any) {
-	logger.Infow(msg, rest...)
+func (l Logger) Info(msg string, rest ...any) {
+	l.logger.Infow(msg, rest...)
 }
-func Warn(msg string, rest ...any) {
-	logger.Warnw(msg, rest...)
+func (l Logger) Warn(msg string, rest ...any) {
+	l.logger.Warnw(msg, rest...)
 }
-func Error(msg string, rest ...any) {
-	logger.Errorw(msg, rest...)
+func (l Logger) Error(msg string, rest ...any) {
+	l.logger.Errorw(msg, rest...)
 }
-func Panic(msg string, rest ...any) {
-	logger.Panicw(msg, rest...)
+func (l Logger) Panic(msg string, rest ...any) {
+	l.logger.Panicw(msg, rest...)
 }
-func Fatal(msg string, rest ...any) {
-	logger.Fatalw(msg, rest...)
+func (l Logger) Fatal(msg string, rest ...any) {
+	l.logger.Fatalw(msg, rest...)
+}
+
+func (l Logger) Named(name string) *Logger {
+	return &Logger{
+		logger: l.logger.Named(name),
+	}
+}
+
+func (l Logger) GetLogger() *zap.Logger {
+	return l.logger.Desugar()
 }
 
 func getLevelFromEnv() (zapcore.Level, error) {
@@ -43,7 +56,7 @@ func getLevelFromEnv() (zapcore.Level, error) {
 	return zap.InfoLevel, nil
 }
 
-func Configure(isProduction bool) {
+func Configure(isProduction bool) *Logger {
 	var l *zap.Logger
 	level, err := getLevelFromEnv()
 	if err != nil {
@@ -54,10 +67,21 @@ func Configure(isProduction bool) {
 		cfg.TimeKey = ""
 		l = zap.New(zapcore.NewCore(zaplogfmt.NewEncoder(cfg), os.Stderr, level))
 	} else {
-		cfg := prettyconsole.NewConfig()
-		cfg.EncoderConfig.TimeKey = ""
-		cfg.Level.SetLevel(level)
-		l = zap.Must(cfg.Build())
+		cfg := prettyconsole.NewEncoderConfig()
+		cfg.TimeKey = ""
+		var filter zapfilter.FilterFunc
+		if debug := os.Getenv("DEBUG"); debug != "" {
+			filter = zapfilter.Any(zapfilter.MinimumLevel(level), zapfilter.ByNamespaces(debug))
+		} else {
+			filter = zapfilter.MinimumLevel(level)
+		}
+		l = zap.New(zapfilter.NewFilteringCore(
+			zapcore.NewCore(prettyconsole.NewEncoder(cfg), os.Stderr, zap.DebugLevel),
+			filter,
+		))
+	}
+
+	return &Logger{
+		logger: l.WithOptions(zap.AddCallerSkip(1)).Sugar(),
 	}
-	logger = l.WithOptions(zap.AddCallerSkip(1)).Sugar()
 }