1
0
فهرست منبع

Update logger

Dima 6 ماه پیش
والد
کامیت
76032b7ba4
4فایلهای تغییر یافته به همراه44 افزوده شده و 49 حذف شده
  1. 9 11
      logger/email_writer.go
  2. 4 14
      logger/logger.go
  3. 7 24
      logger/logger_config.go
  4. 24 0
      logger/readme.md

+ 9 - 11
logger/email_writer.go

@@ -4,29 +4,27 @@ import (
 	"bytes"
 	"encoding/json"
 	"errors"
-
-	"git.dmitriygnatenko.ru/dima/go-common/smtp"
 )
 
 type EmailWriter struct {
-	recipient string
-	subject   string
-	smtp      *smtp.SMTP
+	recipient  string
+	subject    string
+	smtpClient SMTPClient
 }
 
-func NewEmailWriter(smtp *smtp.SMTP, recipient string, subject string) (*EmailWriter, error) {
+func NewEmailWriter(smtpClient SMTPClient, recipient string, subject string) (*EmailWriter, error) {
 	if len(recipient) == 0 {
 		return nil, errors.New("empty recipient")
 	}
 
-	if smtp == nil {
+	if smtpClient == nil {
 		return nil, errors.New("empty smtp client")
 	}
 
 	return &EmailWriter{
-		recipient: recipient,
-		subject:   subject,
-		smtp:      smtp,
+		recipient:  recipient,
+		subject:    subject,
+		smtpClient: smtpClient,
 	}, nil
 }
 
@@ -37,6 +35,6 @@ func (w EmailWriter) Write(p []byte) (int, error) {
 		return 0, err
 	}
 
-	err := w.smtp.Send(w.recipient, w.subject, out.String(), false)
+	err := w.smtpClient.Send(w.recipient, w.subject, out.String(), false)
 	return 0, err
 }

+ 4 - 14
logger/logger.go

@@ -2,12 +2,11 @@ package logger
 
 import (
 	"context"
+	"errors"
 	"fmt"
 	"log/slog"
 	"os"
 	"sync"
-
-	"git.dmitriygnatenko.ru/dima/go-common/smtp"
 )
 
 type CtxAttrKey struct{}
@@ -53,21 +52,12 @@ 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
+			if c.smtpClient == nil {
+				err = errors.New("empty SMTP client")
 				return
 			}
 
-			ew, ewErr := NewEmailWriter(smtpClient, c.emailRecipient, c.emailSubject)
+			ew, ewErr := NewEmailWriter(c.smtpClient, c.emailRecipient, c.emailSubject)
 			if ewErr != nil {
 				err = ewErr
 				return

+ 7 - 24
logger/logger_config.go

@@ -2,6 +2,10 @@ package logger
 
 import "log/slog"
 
+type SMTPClient interface {
+	Send(recipient string, subject string, content string, html bool) error
+}
+
 type Config struct {
 	// stdout config
 	stdoutLogEnabled   bool
@@ -18,10 +22,7 @@ type Config struct {
 	emailLogEnabled   bool
 	emailLogLevel     slog.Level // INFO by default
 	emailLogAddSource bool
-	smtpHost          string
-	smtpPort          uint16
-	smtpUsername      string
-	smtpPassword      string
+	smtpClient        SMTPClient
 	emailRecipient    string
 	emailSubject      string
 }
@@ -120,26 +121,8 @@ func WithEmailSubject(subject string) ConfigOption {
 	}
 }
 
-func WithSMTPHost(host string) ConfigOption {
-	return func(s *Config) {
-		s.smtpHost = host
-	}
-}
-
-func WithSMTPPort(port uint16) ConfigOption {
-	return func(s *Config) {
-		s.smtpPort = port
-	}
-}
-
-func WithSMTPUsername(user string) ConfigOption {
-	return func(s *Config) {
-		s.smtpUsername = user
-	}
-}
-
-func WithSMTPPassword(password string) ConfigOption {
+func WithSMTPClient(c SMTPClient) ConfigOption {
 	return func(s *Config) {
-		s.smtpPassword = password
+		s.smtpClient = c
 	}
 }

+ 24 - 0
logger/readme.md

@@ -0,0 +1,24 @@
+
+## Usage example
+
+```
+err := logger.Init(logger.NewConfig(
+    logger.WithStdoutLogEnabled(true),
+    logger.WithStdoutLogLevel(slog.LevelWarn),
+    logger.WithFileLogEnabled(true),
+    logger.WithFileLogLevel(slog.LevelError),
+    logger.WithFileLogAddSource(true),
+    logger.WithFilepath("./errors.log"),
+))
+
+if err != nil {
+    // TODO
+}
+
+ctx := context.Background()
+
+ctx = logger.With(ctx, "key1", "value1")
+
+logger.ErrorKV(ctx, "error message", "key2", "value2")
+// {"time":"2024-04-19T20:19:17.274684+00:00","level":"ERROR","msg":"error message","key2":"value2","key1":"value1"}
+```