From 7873106d3cd7952528ac06bf8caa33c2bbedf1e9 Mon Sep 17 00:00:00 2001 From: Jesse Peterson Date: Thu, 13 Jun 2024 12:24:21 -0700 Subject: [PATCH] switch to nanolib log --- cmd/depserver/main.go | 8 +++-- cmd/depsyncer/main.go | 8 +++-- go.mod | 1 + go.sum | 4 +++ http/api/assigner.go | 5 +-- http/api/config.go | 5 +-- http/api/tokenpki.go | 5 +-- http/api/tokens.go | 5 +-- http/http.go | 4 +-- log/ctxlog/ctxlog.go | 72 ----------------------------------------- log/logger.go | 17 ---------- log/nop.go | 21 ------------ log/stdlogfmt/stdlog.go | 60 ---------------------------------- proxy/proxy.go | 5 +-- sync/assigner.go | 5 +-- sync/syncer.go | 5 +-- 16 files changed, 40 insertions(+), 190 deletions(-) delete mode 100644 log/ctxlog/ctxlog.go delete mode 100644 log/logger.go delete mode 100644 log/nop.go delete mode 100644 log/stdlogfmt/stdlog.go diff --git a/cmd/depserver/main.go b/cmd/depserver/main.go index 985d03a..6f65461 100644 --- a/cmd/depserver/main.go +++ b/cmd/depserver/main.go @@ -13,8 +13,9 @@ import ( "github.com/micromdm/nanodep/client" dephttp "github.com/micromdm/nanodep/http" "github.com/micromdm/nanodep/http/api" - "github.com/micromdm/nanodep/log/stdlogfmt" "github.com/micromdm/nanodep/proxy" + + "github.com/micromdm/nanolib/log/stdlogfmt" ) // overridden by -ldflags -X @@ -53,7 +54,10 @@ func main() { os.Exit(1) } - logger := stdlogfmt.New(stdlog.Default(), *flDebug) + logger := stdlogfmt.New( + stdlogfmt.WithLogger(stdlog.Default()), + stdlogfmt.WithDebugFlag(*flDebug), + ) storage, err := cli.Storage(*flStorage, *flDSN) if err != nil { diff --git a/cmd/depsyncer/main.go b/cmd/depsyncer/main.go index b1ae9ff..1e70eeb 100644 --- a/cmd/depsyncer/main.go +++ b/cmd/depsyncer/main.go @@ -13,8 +13,9 @@ import ( "github.com/micromdm/nanodep/cli" "github.com/micromdm/nanodep/godep" - "github.com/micromdm/nanodep/log/stdlogfmt" depsync "github.com/micromdm/nanodep/sync" + + "github.com/micromdm/nanolib/log/stdlogfmt" ) // overridden by -ldflags -X @@ -52,7 +53,10 @@ func main() { os.Exit(1) } - logger := stdlogfmt.New(stdlog.Default(), *flDebug) + logger := stdlogfmt.New( + stdlogfmt.WithLogger(stdlog.Default()), + stdlogfmt.WithDebugFlag(*flDebug), + ) storage, err := cli.Storage(*flStorage, *flDSN) if err != nil { diff --git a/go.mod b/go.mod index e5f47bd..0952d55 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.17 require ( github.com/go-sql-driver/mysql v1.8.1 github.com/gomodule/oauth1 v0.2.0 + github.com/micromdm/nanolib v0.1.1 github.com/smallstep/pkcs7 v0.0.0-20231107075624-be1870d87d13 ) diff --git a/go.sum b/go.sum index dd64ff9..e70a060 100644 --- a/go.sum +++ b/go.sum @@ -4,5 +4,9 @@ github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpv github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg= github.com/gomodule/oauth1 v0.2.0 h1:/nNHAD99yipOEspQFbAnNmwGTZ1UNXiD/+JLxwx79fo= github.com/gomodule/oauth1 v0.2.0/go.mod h1:4r/a8/3RkhMBxJQWL5qzbOEcaQmNPIkNoI7P8sXeI08= +github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= +github.com/micromdm/nanolib v0.1.1 h1:nNwY2xLBTHSpwEJsW5xGjkW9MdskAbeo/e6+ZYwr2mE= +github.com/micromdm/nanolib v0.1.1/go.mod h1:FwBKCvvphgYvbdUZ+qw5kay7NHJcg6zPi8W7kXNajmE= +github.com/peterbourgon/diskv/v3 v3.0.1/go.mod h1:kJ5Ny7vLdARGU3WUuy6uzO6T0nb/2gWcT1JiBvRmb5o= github.com/smallstep/pkcs7 v0.0.0-20231107075624-be1870d87d13 h1:qRxEt9ESQhAg1kjmgJ8oyyzlc9zkAjOooe7bcKjKORQ= github.com/smallstep/pkcs7 v0.0.0-20231107075624-be1870d87d13/go.mod h1:SoUAr/4M46rZ3WaLstHxGhLEgoYIDRqxQEXLOmOEB0Y= diff --git a/http/api/assigner.go b/http/api/assigner.go index e3ddbe9..62990ab 100644 --- a/http/api/assigner.go +++ b/http/api/assigner.go @@ -5,9 +5,10 @@ import ( "encoding/json" "net/http" - "github.com/micromdm/nanodep/log" - "github.com/micromdm/nanodep/log/ctxlog" "github.com/micromdm/nanodep/sync" + + "github.com/micromdm/nanolib/log" + "github.com/micromdm/nanolib/log/ctxlog" ) // RetrieveAssignerProfileHandler returns the assigner profile UUID for the diff --git a/http/api/config.go b/http/api/config.go index 06ef26d..d865614 100644 --- a/http/api/config.go +++ b/http/api/config.go @@ -7,8 +7,9 @@ import ( "net/http" "github.com/micromdm/nanodep/client" - "github.com/micromdm/nanodep/log" - "github.com/micromdm/nanodep/log/ctxlog" + + "github.com/micromdm/nanolib/log" + "github.com/micromdm/nanolib/log/ctxlog" ) // RetrieveConfigHandler returns the DEP server config for the DEP diff --git a/http/api/tokenpki.go b/http/api/tokenpki.go index eac8086..777f668 100644 --- a/http/api/tokenpki.go +++ b/http/api/tokenpki.go @@ -11,9 +11,10 @@ import ( "strconv" "github.com/micromdm/nanodep/client" - "github.com/micromdm/nanodep/log" - "github.com/micromdm/nanodep/log/ctxlog" "github.com/micromdm/nanodep/tokenpki" + + "github.com/micromdm/nanolib/log" + "github.com/micromdm/nanolib/log/ctxlog" ) type TokenPKIStagingRetriever interface { diff --git a/http/api/tokens.go b/http/api/tokens.go index b32650f..34f96e1 100644 --- a/http/api/tokens.go +++ b/http/api/tokens.go @@ -7,8 +7,9 @@ import ( "net/http" "github.com/micromdm/nanodep/client" - "github.com/micromdm/nanodep/log" - "github.com/micromdm/nanodep/log/ctxlog" + + "github.com/micromdm/nanolib/log" + "github.com/micromdm/nanolib/log/ctxlog" ) var CKMismatch = errors.New("mismatched consumer key") diff --git a/http/http.go b/http/http.go index 3b17d7d..6f6bfee 100644 --- a/http/http.go +++ b/http/http.go @@ -10,8 +10,8 @@ import ( "net" "net/http" - "github.com/micromdm/nanodep/log" - "github.com/micromdm/nanodep/log/ctxlog" + "github.com/micromdm/nanolib/log" + "github.com/micromdm/nanolib/log/ctxlog" ) // ReadAllAndReplaceBody reads all of r.Body and replaces it with a new byte buffer. diff --git a/log/ctxlog/ctxlog.go b/log/ctxlog/ctxlog.go deleted file mode 100644 index de28438..0000000 --- a/log/ctxlog/ctxlog.go +++ /dev/null @@ -1,72 +0,0 @@ -// Package ctxlog allows logging data stored with a context. -package ctxlog - -import ( - "context" - "sync" - - "github.com/micromdm/nanodep/log" -) - -// CtxKVFunc creates logger key-value pairs from a context. -// CtxKVFuncs should aim to be be as efficient as possible—ideally only -// doing the minimum to read context values and generate KV pairs. Each -// associated CtxKVFunc is called every time we adapt a logger with -// Logger. -type CtxKVFunc func(context.Context) []interface{} - -// ctxKeyFuncs is the context key for storing and retriveing -// a funcs{} struct on a context. -type ctxKeyFuncs struct{} - -// funcs holds the associated CtxKVFunc functions to run. -type funcs struct { - sync.RWMutex - funcs []CtxKVFunc -} - -// AddFunc associates a new CtxKVFunc function to a context. -func AddFunc(ctx context.Context, f CtxKVFunc) context.Context { - if ctx == nil { - return ctx - } - ctxFuncs, ok := ctx.Value(ctxKeyFuncs{}).(*funcs) - if !ok || ctxFuncs == nil { - ctxFuncs = &funcs{} - } - ctxFuncs.Lock() - ctxFuncs.funcs = append(ctxFuncs.funcs, f) - ctxFuncs.Unlock() - return context.WithValue(ctx, ctxKeyFuncs{}, ctxFuncs) -} - -// Logger runs the associated CtxKVFunc functions and returns a new -// logger with the results. -func Logger(ctx context.Context, logger log.Logger) log.Logger { - if ctx == nil { - return logger - } - ctxFuncs, ok := ctx.Value(ctxKeyFuncs{}).(*funcs) - if !ok || ctxFuncs == nil { - return logger - } - var acc []interface{} - ctxFuncs.RLock() - for _, f := range ctxFuncs.funcs { - acc = append(acc, f(ctx)...) - } - ctxFuncs.RUnlock() - return logger.With(acc...) -} - -// SimpleStringFunc is a helper that generates a simple CtxKVFunc that -// returns a key-value pair if found on the context. -func SimpleStringFunc(logKey string, ctxKey interface{}) CtxKVFunc { - return func(ctx context.Context) (out []interface{}) { - v, _ := ctx.Value(ctxKey).(string) - if v != "" { - out = []interface{}{logKey, v} - } - return - } -} diff --git a/log/logger.go b/log/logger.go deleted file mode 100644 index 2775e82..0000000 --- a/log/logger.go +++ /dev/null @@ -1,17 +0,0 @@ -package log - -// Pacakge log is embedded (not imported) from: -// https://github.com/jessepeterson/go-log - -// Logger is a generic logging interface to a structured, leveled, nest-able logger -type Logger interface { - // Info logs using the info level - Info(...interface{}) - - // Debug logs using the debug level - Debug(...interface{}) - - // With nests the Logger - // Usually for adding logging context to a sub-logger - With(...interface{}) Logger -} diff --git a/log/nop.go b/log/nop.go deleted file mode 100644 index c0800c5..0000000 --- a/log/nop.go +++ /dev/null @@ -1,21 +0,0 @@ -package log - -// Pacakge log is embedded (not imported) from: -// https://github.com/jessepeterson/go-log - -// nopLogger does nothing -type nopLogger struct{} - -// Info does nothing -func (*nopLogger) Info(_ ...interface{}) {} - -// Debug does nothing -func (*nopLogger) Debug(_ ...interface{}) {} - -// With returns (the same) logger -func (logger *nopLogger) With(_ ...interface{}) Logger { - return logger -} - -// NopLogger is a Logger that does nothing -var NopLogger = &nopLogger{} diff --git a/log/stdlogfmt/stdlog.go b/log/stdlogfmt/stdlog.go deleted file mode 100644 index 36ec78c..0000000 --- a/log/stdlogfmt/stdlog.go +++ /dev/null @@ -1,60 +0,0 @@ -package stdlogfmt - -import ( - stdlog "log" - "strings" - - "github.com/micromdm/nanodep/log" -) - -// Logger wraps a standard library logger and adapts it to pkg/log. -type Logger struct { - stdLogger *stdlog.Logger - context []interface{} - logDebug bool -} - -// New creates a new logger that adapts the standard log package to pkg/log. -func New(logger *stdlog.Logger, logDebug bool) *Logger { - return &Logger{ - stdLogger: logger, - logDebug: logDebug, - } -} - -func (l *Logger) print(args ...interface{}) { - f := strings.Repeat(" %s=%v", len(args)/2)[1:] - if len(args)%2 == 1 { - f += " UNKNOWN=%v" - } - l.stdLogger.Printf(f, args...) -} - -// Info logs using the "info" level -func (l *Logger) Info(args ...interface{}) { - logs := []interface{}{"level", "info"} - logs = append(logs, l.context...) - logs = append(logs, args...) - l.print(logs...) -} - -// Info logs using the "debug" level -func (l *Logger) Debug(args ...interface{}) { - if l.logDebug { - logs := []interface{}{"level", "debug"} - logs = append(logs, l.context...) - logs = append(logs, args...) - l.print(logs...) - } -} - -// With creates a new logger using args as context -func (l *Logger) With(args ...interface{}) log.Logger { - newLogger := &Logger{ - stdLogger: l.stdLogger, - context: l.context, - logDebug: l.logDebug, - } - newLogger.context = append(newLogger.context, args...) - return newLogger -} diff --git a/proxy/proxy.go b/proxy/proxy.go index 76f65a4..70486d5 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -11,8 +11,9 @@ import ( "sync" "github.com/micromdm/nanodep/client" - "github.com/micromdm/nanodep/log" - "github.com/micromdm/nanodep/log/ctxlog" + + "github.com/micromdm/nanolib/log" + "github.com/micromdm/nanolib/log/ctxlog" ) // New creates new NanoDEP ReverseProxy. It dispatches requests using transport diff --git a/sync/assigner.go b/sync/assigner.go index ec02665..e82627f 100644 --- a/sync/assigner.go +++ b/sync/assigner.go @@ -7,8 +7,9 @@ import ( "time" "github.com/micromdm/nanodep/godep" - "github.com/micromdm/nanodep/log" - "github.com/micromdm/nanodep/log/ctxlog" + + "github.com/micromdm/nanolib/log" + "github.com/micromdm/nanolib/log/ctxlog" ) type AssignerProfileRetriever interface { diff --git a/sync/syncer.go b/sync/syncer.go index df352be..7dc13e0 100644 --- a/sync/syncer.go +++ b/sync/syncer.go @@ -8,8 +8,9 @@ import ( "time" "github.com/micromdm/nanodep/godep" - "github.com/micromdm/nanodep/log" - "github.com/micromdm/nanodep/log/ctxlog" + + "github.com/micromdm/nanolib/log" + "github.com/micromdm/nanolib/log/ctxlog" ) // CursorStorage is where the device fetch and sync cursor can be stored and