Dima il y a 6 mois
Parent
commit
efc856d9e9
2 fichiers modifiés avec 70 ajouts et 139 suppressions
  1. 0 111
      logger/handler.go
  2. 70 28
      logger/logger_config.go

+ 0 - 111
logger/handler.go

@@ -1,111 +0,0 @@
-package logger
-
-import (
-	"bytes"
-	"context"
-	"fmt"
-	"log/slog"
-	"sync"
-)
-
-type Handler struct {
-	config  HandlerConfig
-	handler slog.Handler
-	mu      *sync.Mutex
-	buf     *bytes.Buffer
-}
-
-type HandlerConfig struct {
-	// stdout config
-	stdoutLogEnabled bool
-	stdoutLogLevel   slog.Level // INFO by default
-
-	// file config
-	fileLogEnabled bool
-	fileLogLevel   slog.Level // INFO by default
-
-	// email config
-	emailLogEnabled bool
-	emailLogLevel   slog.Level // INFO by default
-
-	// common config
-	minLogLevel slog.Level
-	timeFormat  string
-}
-
-func NewHandler(c Config) Handler {
-	buf := &bytes.Buffer{}
-	minLogLevel := getMinLogLevel(c)
-
-	if len(c.timeFormat) == 0 {
-		c.timeFormat = defaultTimeFormat
-	}
-
-	return Handler{
-		buf: buf,
-		mu:  &sync.Mutex{},
-
-		handler: slog.NewJSONHandler(buf, &slog.HandlerOptions{
-			AddSource: c.addSource,
-			Level:     minLogLevel,
-		}),
-
-		config: HandlerConfig{
-			stdoutLogEnabled: c.stdoutLogEnabled,
-			fileLogEnabled:   c.fileLogEnabled,
-			emailLogEnabled:  c.emailLogEnabled,
-			stdoutLogLevel:   c.stdoutLogLevel,
-			fileLogLevel:     c.fileLogLevel,
-			emailLogLevel:    c.emailLogLevel,
-			minLogLevel:      minLogLevel,
-			timeFormat:       c.timeFormat,
-		},
-	}
-}
-
-func (h *Handler) Handle(ctx context.Context, r slog.Record) error {
-	message := r.Time.Format(h.config.timeFormat) + " " + r.Level.String() + " " + r.Message
-
-	var attrs map[string]any
-
-	r.Attrs(func(attr slog.Attr) bool {
-
-		return true
-	})
-
-	if h.config.stdoutLogEnabled {
-		fmt.Println(message)
-	}
-
-	return nil
-}
-
-func (h *Handler) Enabled(_ context.Context, l slog.Level) bool {
-	return l >= h.config.minLogLevel
-}
-
-func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler {
-	return &Handler{handler: h.handler.WithAttrs(attrs), buf: h.buf, mu: h.mu, config: h.config}
-}
-
-func (h *Handler) WithGroup(name string) slog.Handler {
-	return &Handler{handler: h.handler.WithGroup(name), buf: h.buf, mu: h.mu, config: h.config}
-}
-
-func getMinLogLevel(c Config) slog.Level {
-	minLogLevel := slog.LevelError
-
-	if c.stdoutLogEnabled && c.stdoutLogLevel < minLogLevel {
-		minLogLevel = c.stdoutLogLevel
-	}
-
-	if c.fileLogEnabled && c.fileLogLevel < minLogLevel {
-		minLogLevel = c.fileLogLevel
-	}
-
-	if c.emailLogEnabled && c.emailLogLevel < minLogLevel {
-		minLogLevel = c.fileLogLevel
-	}
-
-	return minLogLevel
-}

+ 70 - 28
logger/logger_config.go

@@ -2,7 +2,7 @@ package logger
 
 import "log/slog"
 
-type Config struct {
+type LoggerConfig struct {
 	// stdout config
 	stdoutLogEnabled   bool
 	stdoutLogLevel     slog.Level // INFO by default
@@ -12,24 +12,30 @@ type Config struct {
 	fileLogEnabled   bool
 	fileLogLevel     slog.Level // INFO by default
 	fileLogAddSource bool
-	fileLogFilepath  string
+	filepath         string
 
 	// email config
 	emailLogEnabled   bool
 	emailLogLevel     slog.Level // INFO by default
 	emailLogAddSource bool
+	smtpHost          string
+	smtpPort          uint
+	smtpUser          string
+	smtpPassword      string
+	email             string
+	subject           string
 }
 
-type ConfigOption func(*Config)
+type LoggerConfigOption func(*LoggerConfig)
 
-type ConfigOptions []ConfigOption
+type LoggerConfigOptions []LoggerConfigOption
 
-func (s *ConfigOptions) Add(option ConfigOption) {
+func (s *LoggerConfigOptions) Add(option LoggerConfigOption) {
 	*s = append(*s, option)
 }
 
-func NewConfig(opts ...ConfigOption) Config {
-	c := &Config{}
+func NewConfig(opts ...LoggerConfigOption) LoggerConfig {
+	c := &LoggerConfig{}
 	for _, opt := range opts {
 		opt(c)
 	}
@@ -39,65 +45,101 @@ func NewConfig(opts ...ConfigOption) Config {
 
 // stdout log
 
-func WithStdoutLogEnabled(enabled bool) ConfigOption {
-	return func(s *Config) {
+func WithStdoutLogEnabled(enabled bool) LoggerConfigOption {
+	return func(s *LoggerConfig) {
 		s.stdoutLogEnabled = enabled
 	}
 }
 
-func WithStdoutLogLevel(level slog.Level) ConfigOption {
-	return func(s *Config) {
+func WithStdoutLogLevel(level slog.Level) LoggerConfigOption {
+	return func(s *LoggerConfig) {
 		s.stdoutLogLevel = level
 	}
 }
-func WithStdoutLogAddSource(add bool) ConfigOption {
-	return func(s *Config) {
+func WithStdoutLogAddSource(add bool) LoggerConfigOption {
+	return func(s *LoggerConfig) {
 		s.stdoutLogAddSource = add
 	}
 }
 
 // file log
 
-func WithFileLogEnabled(enabled bool) ConfigOption {
-	return func(s *Config) {
+func WithFileLogEnabled(enabled bool) LoggerConfigOption {
+	return func(s *LoggerConfig) {
 		s.fileLogEnabled = enabled
 	}
 }
 
-func WithFileLogLevel(level slog.Level) ConfigOption {
-	return func(s *Config) {
+func WithFileLogLevel(level slog.Level) LoggerConfigOption {
+	return func(s *LoggerConfig) {
 		s.fileLogLevel = level
 	}
 }
 
-func WithFileLogAddSource(add bool) ConfigOption {
-	return func(s *Config) {
+func WithFileLogAddSource(add bool) LoggerConfigOption {
+	return func(s *LoggerConfig) {
 		s.fileLogAddSource = add
 	}
 }
 
-func WithFileLogFilepath(path string) ConfigOption {
-	return func(s *Config) {
-		s.fileLogFilepath = path
+func WithFilepath(path string) LoggerConfigOption {
+	return func(s *LoggerConfig) {
+		s.filepath = path
 	}
 }
 
 // email log
 
-func WithEmailLogEnabled(enabled bool) ConfigOption {
-	return func(s *Config) {
+func WithEmailLogEnabled(enabled bool) LoggerConfigOption {
+	return func(s *LoggerConfig) {
 		s.emailLogEnabled = enabled
 	}
 }
 
-func WithEmailLogLevel(level slog.Level) ConfigOption {
-	return func(s *Config) {
+func WithEmailLogLevel(level slog.Level) LoggerConfigOption {
+	return func(s *LoggerConfig) {
 		s.emailLogLevel = level
 	}
 }
 
-func WithEmailLogAddSource(add bool) ConfigOption {
-	return func(s *Config) {
+func WithEmailLogAddSource(add bool) LoggerConfigOption {
+	return func(s *LoggerConfig) {
 		s.emailLogAddSource = add
 	}
 }
+
+func WithEmailRecipient(email string) LoggerConfigOption {
+	return func(s *LoggerConfig) {
+		s.email = email
+	}
+}
+
+func WithEmailSubject(subject string) LoggerConfigOption {
+	return func(s *LoggerConfig) {
+		s.subject = subject
+	}
+}
+
+func WithSMTPHost(host string) LoggerConfigOption {
+	return func(s *LoggerConfig) {
+		s.smtpHost = host
+	}
+}
+
+func WithSMTPPort(port uint) LoggerConfigOption {
+	return func(s *LoggerConfig) {
+		s.smtpPort = port
+	}
+}
+
+func WithSMTPUser(user string) LoggerConfigOption {
+	return func(s *LoggerConfig) {
+		s.smtpUser = user
+	}
+}
+
+func WithSMTPPassword(password string) LoggerConfigOption {
+	return func(s *LoggerConfig) {
+		s.smtpPassword = password
+	}
+}