Jelajahi Sumber

Update logger

Dima 6 bulan lalu
induk
melakukan
cdc751c144
5 mengubah file dengan 105 tambahan dan 83 penghapusan
  1. 1 1
      go.mod
  2. 35 1
      logger/email_writer.go
  3. 21 2
      logger/logger.go
  4. 48 48
      logger/logger_config.go
  5. 0 31
      main.go

+ 1 - 1
go.mod

@@ -1,3 +1,3 @@
 module git.dmitriygnatenko.ru/dima/go-common
 
-go 1.22
+go 1.21.4

+ 35 - 1
logger/email_writer.go

@@ -1,8 +1,42 @@
 package logger
 
+import (
+	"bytes"
+	"encoding/json"
+	"errors"
+
+	"git.dmitriygnatenko.ru/dima/go-common/smtp"
+)
+
 type EmailWriter struct {
+	recipient string
+	subject   string
+	smtp      *smtp.SMTP
+}
+
+func NewEmailWriter(smtp *smtp.SMTP, recipient string, subject string) (*EmailWriter, error) {
+	if len(recipient) == 0 {
+		return nil, errors.New("empty recipient")
+	}
+
+	if smtp == nil {
+		return nil, errors.New("empty smtp client")
+	}
+
+	return &EmailWriter{
+		recipient: recipient,
+		subject:   subject,
+		smtp:      smtp,
+	}, nil
 }
 
-func (w EmailWriter) Write(p []byte) (n int, err error) {
+func (w EmailWriter) Write(p []byte) (int, error) {
+	var out bytes.Buffer
+
+	if err := json.Indent(&out, p, "", "    "); err != nil {
+		return 0, err
+	}
+
+	err := w.smtp.Send(w.recipient, w.subject, out.String(), false)
 	return 0, err
 }

+ 21 - 2
logger/logger.go

@@ -6,6 +6,8 @@ import (
 	"log/slog"
 	"os"
 	"sync"
+
+	"git.dmitriygnatenko.ru/dima/go-common/smtp"
 )
 
 type CtxAttrKey struct{}
@@ -39,7 +41,7 @@ func Init(c Config) error {
 		}
 
 		if c.fileLogEnabled {
-			logger.logFile, err = os.OpenFile(c.fileLogFilepath, os.O_APPEND|os.O_RDWR|os.O_CREATE, 0644)
+			logger.logFile, err = os.OpenFile(c.filepath, os.O_APPEND|os.O_RDWR|os.O_CREATE, 0644)
 			if err != nil {
 				return
 			}
@@ -51,8 +53,25 @@ func Init(c Config) error {
 		}
 
 		if c.emailLogEnabled {
+			smtpClient, smtpErr := smtp.NewSMTP(
+				smtp.NewConfig(
+					smtp.WithHost(c.smtpHost),
+					smtp.WithUsername(c.smtpUsername),
+					smtp.WithPassword(c.smtpPassword),
+					smtp.WithPort(c.smtpPort),
+				),
+			)
+
+			if smtpErr != nil {
+				err = smtpErr
+				return
+			}
 
-			ew := EmailWriter{}
+			ew, ewErr := NewEmailWriter(smtpClient, c.emailRecipient, c.emailSubject)
+			if ewErr != nil {
+				err = ewErr
+				return
+			}
 
 			logger.emailLogger = slog.New(slog.NewJSONHandler(ew, &slog.HandlerOptions{
 				AddSource: c.emailLogAddSource,

+ 48 - 48
logger/logger_config.go

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

+ 0 - 31
main.go

@@ -1,31 +0,0 @@
-package main
-
-import (
-	"context"
-	"log/slog"
-
-	"git.dmitriygnatenko.ru/dima/go-common/logger"
-)
-
-func main() {
-
-	err := logger.Init(logger.NewConfig(
-		logger.WithStdoutLogAddSource(true),
-		logger.WithStdoutLogEnabled(true),
-		logger.WithFileLogLevel(slog.LevelError),
-		logger.WithEmailLogEnabled(true),
-		logger.WithFileLogEnabled(true),
-		logger.WithFileLogFilepath("test_log.txt"),
-	))
-
-	_ = err
-
-	ctx := context.Background()
-
-	ctx = logger.With(ctx, "test111", 74658743)
-
-	logger.InfoKV(ctx, "dfgdgdfgdssg", "dsfsd", "val333", "dfgdf", 11)
-
-	// logger.Error(ctx, "dsfdsf dsf dsfs")
-
-}