| 1 | package logger |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "maps" |
| 6 | "net/url" |
| 7 | "strings" |
| 8 | |
| 9 | "github.com/nicholas-fedor/shoutrrr/pkg/services/standard" |
| 10 | "github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 11 | ) |
| 12 | |
| 13 | // Service is the Logger service struct. |
| 14 | type Service struct { |
| 15 | standard.Standard |
| 16 | |
| 17 | Config *Config |
| 18 | } |
| 19 | |
| 20 | // GetID returns the service identifier. |
| 21 | func (s *Service) GetID() string { |
| 22 | return Scheme |
| 23 | } |
| 24 | |
| 25 | // Initialize loads ServiceConfig from serviceURL and sets logger for this Service. |
| 26 | func (s *Service) Initialize(_ *url.URL, logger types.StdLogger) error { |
| 27 | s.SetLogger(logger) |
| 28 | s.Config = &Config{} |
| 29 | |
| 30 | return nil |
| 31 | } |
| 32 | |
| 33 | // Send a notification message to log. |
| 34 | func (s *Service) Send(message string, params *types.Params) error { |
| 35 | data := types.Params{} |
| 36 | |
| 37 | if params != nil { |
| 38 | maps.Copy(data, *params) |
| 39 | } |
| 40 | |
| 41 | data["message"] = message |
| 42 | |
| 43 | return s.doSend(data) |
| 44 | } |
| 45 | |
| 46 | func (s *Service) doSend(data types.Params) error { |
| 47 | msg := data["message"] |
| 48 | |
| 49 | if tpl, found := s.GetTemplate("message"); found { |
| 50 | wc := &strings.Builder{} |
| 51 | if err := tpl.Execute(wc, data); err != nil { |
| 52 | return fmt.Errorf("failed to write template to log: %w", err) |
| 53 | } |
| 54 | |
| 55 | msg = wc.String() |
| 56 | } |
| 57 | |
| 58 | s.Log(msg) |
| 59 | |
| 60 | return nil |
| 61 | } |