From 6f9a8200f49471d95612a96e34dfdf3c0b4047b9 Mon Sep 17 00:00:00 2001 From: John Roesler Date: Thu, 9 Nov 2023 09:49:21 -0600 Subject: [PATCH] support go 1.20+ (#608) --- .github/workflows/go_test.yml | 1 + example_test.go | 3 +- go.mod | 2 +- job.go | 2 +- logger.go | 82 ++++++++++++++++++++--------------- scheduler.go | 2 +- scheduler_test.go | 3 +- util.go | 2 +- 8 files changed, 53 insertions(+), 44 deletions(-) diff --git a/.github/workflows/go_test.yml b/.github/workflows/go_test.yml index 0dcc90a..3a84f21 100644 --- a/.github/workflows/go_test.yml +++ b/.github/workflows/go_test.yml @@ -12,6 +12,7 @@ jobs: strategy: matrix: go-version: + - "1.20" - "1.21" name: lint and test runs-on: ubuntu-latest diff --git a/example_test.go b/example_test.go index e94efbc..7398625 100644 --- a/example_test.go +++ b/example_test.go @@ -2,7 +2,6 @@ package gocron_test import ( "fmt" - "log/slog" "sync" "time" @@ -550,7 +549,7 @@ func ExampleWithLocation() { func ExampleWithLogger() { _, _ = NewScheduler( WithLogger( - NewJSONSlogLogger(slog.LevelInfo), + NewLogger(LogLevelDebug), ), ) } diff --git a/go.mod b/go.mod index 2f41e00..e365b58 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/go-co-op/gocron/v2 -go 1.21 +go 1.20 require ( github.com/google/uuid v1.4.0 diff --git a/job.go b/job.go index 47aece8..4c68244 100644 --- a/job.go +++ b/job.go @@ -5,13 +5,13 @@ import ( "errors" "fmt" "math/rand" - "slices" "strings" "time" "github.com/google/uuid" "github.com/jonboulle/clockwork" "github.com/robfig/cron/v3" + "golang.org/x/exp/slices" ) // internalJob stores the information needed by the scheduler diff --git a/logger.go b/logger.go index 39982c9..c03e03c 100644 --- a/logger.go +++ b/logger.go @@ -1,8 +1,7 @@ package gocron import ( - "log/slog" - "os" + "log" ) // Logger is the interface that wraps the basic logging methods @@ -12,56 +11,67 @@ import ( // or implement your own Logger. The actual level of Log that is logged // is handled by the implementation. type Logger interface { - Debug(msg string, args ...any) - Error(msg string, args ...any) - Info(msg string, args ...any) - Warn(msg string, args ...any) + Debug(msg string, args ...interface{}) + Error(msg string, args ...interface{}) + Info(msg string, args ...interface{}) + Warn(msg string, args ...interface{}) } var _ Logger = (*noOpLogger)(nil) type noOpLogger struct{} -func (l noOpLogger) Debug(_ string, _ ...any) {} -func (l noOpLogger) Error(_ string, _ ...any) {} -func (l noOpLogger) Info(_ string, _ ...any) {} -func (l noOpLogger) Warn(_ string, _ ...any) {} +func (l noOpLogger) Debug(_ string, _ ...interface{}) {} +func (l noOpLogger) Error(_ string, _ ...interface{}) {} +func (l noOpLogger) Info(_ string, _ ...interface{}) {} +func (l noOpLogger) Warn(_ string, _ ...interface{}) {} -var _ Logger = (*slogLogger)(nil) +var _ Logger = (*logger)(nil) -type slogLogger struct { - sl *slog.Logger +// LogLevel is the level of logging that should be logged +// when using the basic NewLogger. +type LogLevel int + +const ( + LogLevelError LogLevel = iota + LogLevelWarn + LogLevelInfo + LogLevelDebug +) + +type logger struct { + level LogLevel } -func NewJSONSlogLogger(level slog.Level) Logger { - return NewSlogLogger( - slog.New( - slog.NewJSONHandler( - os.Stdout, - &slog.HandlerOptions{ - Level: level, - }, - ), - ), - ) +// NewLogger returns a new Logger that logs at the given level. +func NewLogger(level LogLevel) Logger { + return &logger{level: level} } -func NewSlogLogger(sl *slog.Logger) Logger { - return &slogLogger{sl: sl} +func (l *logger) Debug(msg string, args ...interface{}) { + if l.level < LogLevelDebug { + return + } + log.Printf("DEBUG: %s, %v\n", msg, args) } -func (l *slogLogger) Debug(msg string, args ...any) { - l.sl.Debug(msg, args...) +func (l *logger) Error(msg string, args ...interface{}) { + if l.level < LogLevelError { + return + } + log.Printf("ERROR: %s, %v\n", msg, args) } -func (l *slogLogger) Error(msg string, args ...any) { - l.sl.Error(msg, args...) +func (l *logger) Info(msg string, args ...interface{}) { + if l.level < LogLevelInfo { + return + } + log.Printf("INFO: %s, %v\n", msg, args) } -func (l *slogLogger) Info(msg string, args ...any) { - l.sl.Info(msg, args...) -} - -func (l *slogLogger) Warn(msg string, args ...any) { - l.sl.Warn(msg, args...) +func (l *logger) Warn(msg string, args ...interface{}) { + if l.level < LogLevelWarn { + return + } + log.Printf("WARN: %s, %v\n", msg, args) } diff --git a/scheduler.go b/scheduler.go index 9ec670e..5909100 100644 --- a/scheduler.go +++ b/scheduler.go @@ -3,11 +3,11 @@ package gocron import ( "context" "reflect" - "slices" "time" "github.com/google/uuid" "github.com/jonboulle/clockwork" + "golang.org/x/exp/slices" ) var _ Scheduler = (*scheduler)(nil) diff --git a/scheduler_test.go b/scheduler_test.go index 509461e..d5cb9c9 100644 --- a/scheduler_test.go +++ b/scheduler_test.go @@ -3,7 +3,6 @@ package gocron import ( "context" "fmt" - "log/slog" "sync" "testing" "time" @@ -16,7 +15,7 @@ import ( func newTestScheduler(options ...SchedulerOption) (Scheduler, error) { // default test options out := []SchedulerOption{ - WithLogger(NewJSONSlogLogger(slog.LevelDebug)), + WithLogger(NewLogger(LogLevelDebug)), WithStopTimeout(time.Second), } diff --git a/util.go b/util.go index f539078..8d9cfc7 100644 --- a/util.go +++ b/util.go @@ -3,12 +3,12 @@ package gocron import ( "context" "reflect" - "slices" "sync" "time" "github.com/google/uuid" "golang.org/x/exp/maps" + "golang.org/x/exp/slices" ) func callJobFuncWithParams(jobFunc any, params ...any) error {