support go 1.20+ (#608)

This commit is contained in:
John Roesler 2023-11-09 09:49:21 -06:00 committed by GitHub
parent 584cd8aeca
commit 6f9a8200f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 53 additions and 44 deletions

View File

@ -12,6 +12,7 @@ jobs:
strategy:
matrix:
go-version:
- "1.20"
- "1.21"
name: lint and test
runs-on: ubuntu-latest

View File

@ -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),
),
)
}

2
go.mod
View File

@ -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

2
job.go
View File

@ -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

View File

@ -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
func NewJSONSlogLogger(level slog.Level) Logger {
return NewSlogLogger(
slog.New(
slog.NewJSONHandler(
os.Stdout,
&slog.HandlerOptions{
Level: level,
},
),
),
const (
LogLevelError LogLevel = iota
LogLevelWarn
LogLevelInfo
LogLevelDebug
)
type logger struct {
level LogLevel
}
func NewSlogLogger(sl *slog.Logger) Logger {
return &slogLogger{sl: sl}
// NewLogger returns a new Logger that logs at the given level.
func NewLogger(level LogLevel) Logger {
return &logger{level: level}
}
func (l *slogLogger) Debug(msg string, args ...any) {
l.sl.Debug(msg, args...)
func (l *logger) Debug(msg string, args ...interface{}) {
if l.level < LogLevelDebug {
return
}
log.Printf("DEBUG: %s, %v\n", msg, args)
}
func (l *slogLogger) Error(msg string, args ...any) {
l.sl.Error(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) Info(msg string, args ...any) {
l.sl.Info(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) 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)
}

View File

@ -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)

View File

@ -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),
}

View File

@ -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 {