]> git.giacomolli.uk - onechat/blobdiff - schedule.go
Log daemon events at their slog levels
[onechat] / schedule.go
index d2f53e095887749949320f3af79252f556ba3db8..5da024d39b0e4442235b3329695416cb9ac132eb 100644 (file)
@@ -6,7 +6,7 @@ import (
        "encoding/json"
        "errors"
        "fmt"
        "encoding/json"
        "errors"
        "fmt"
-       "log"
+       "log/slog"
        "os"
        "slices"
        "strconv"
        "os"
        "slices"
        "strconv"
@@ -51,7 +51,7 @@ const (
 // openSchedules reads the schedules of the file at path. A missing file holds
 // none. The clock gives the time and the time zone of the schedules, and logger
 // gets one line for each run.
 // openSchedules reads the schedules of the file at path. A missing file holds
 // none. The clock gives the time and the time zone of the schedules, and logger
 // gets one line for each run.
-func openSchedules(path string, clock func() time.Time, logger *log.Logger) (*Schedules, error) {
+func openSchedules(path string, clock func() time.Time, logger *slog.Logger) (*Schedules, error) {
        s := &Schedules{
                path:    path,
                clock:   clock,
        s := &Schedules{
                path:    path,
                clock:   clock,
@@ -96,7 +96,7 @@ func openSchedules(path string, clock func() time.Time, logger *log.Logger) (*Sc
 type Schedules struct {
        path    string
        clock   func() time.Time
 type Schedules struct {
        path    string
        clock   func() time.Time
-       logger  *log.Logger
+       logger  *slog.Logger
        check   time.Duration
        changed chan struct{}
 
        check   time.Duration
        changed chan struct{}
 
@@ -281,12 +281,13 @@ func (s *Schedules) deliver(ctx context.Context, l *TurnLoop) error {
                        return err
                }
 
                        return err
                }
 
+               attrs := []any{"id", schedule.ID}
                if schedule.late(now) {
                if schedule.late(now) {
-                       s.logger.Printf("schedule %d ran %s late", schedule.ID, durationText(now.Sub(schedule.Due)))
-               } else {
-                       s.logger.Printf("schedule %d ran", schedule.ID)
+                       attrs = append(attrs, "late", now.Sub(schedule.Due))
                }
 
                }
 
+               s.logger.Info("schedule ran", attrs...)
+
                turns = append(turns, message)
        }
 
                turns = append(turns, message)
        }