diff --git a/log/ctxlog/ctxlog.go b/log/ctxlog/ctxlog.go deleted file mode 100644 index c3dfc21..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/nanocmd/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 9721ba7..0000000 --- a/log/stdlogfmt/stdlog.go +++ /dev/null @@ -1,116 +0,0 @@ -package stdlogfmt - -import ( - "fmt" - stdlog "log" - "os" - "path/filepath" - "runtime" - "strings" - "time" - - "github.com/micromdm/nanocmd/log" -) - -// Logger wraps a standard library logger and adapts it to pkg/log. -type Logger struct { - logger *stdlog.Logger - context []interface{} - debug bool - depth int - ts bool -} - -type Option func(*Logger) - -// WithLogger sets the Go standard logger to use. -func WithLogger(logger *stdlog.Logger) Option { - return func(l *Logger) { - l.logger = logger - } -} - -// WithDebug turns on debug logging. -func WithDebug() Option { - return func(l *Logger) { - l.debug = true - } -} - -// WithDebugFlag sets debug logging on or off. -func WithDebugFlag(flag bool) Option { - return func(l *Logger) { - l.debug = flag - } -} - -// WithCallerDepth sets the call depth of the logger for filename and line -// logging. Set depth to 0 to disable filename and line logging. -func WithCallerDepth(depth int) Option { - return func(l *Logger) { - l.depth = depth - } -} - -// WithoutTimestamp disables outputting an RFC3339 timestamp. -func WithoutTimestamp() Option { - return func(l *Logger) { - l.ts = false - } -} - -// New creates a new logger that adapts the Go standard log package to Logger. -func New(opts ...Option) *Logger { - l := &Logger{ - logger: stdlog.New(os.Stderr, "", 0), - depth: 1, - ts: true, - } - for _, opt := range opts { - opt(l) - } - return l -} - -func (l *Logger) print(args ...interface{}) { - if l.ts { - args = append([]interface{}{"ts", time.Now().Format(time.RFC3339)}, args...) - } - if l.depth > 0 { - _, filename, line, ok := runtime.Caller(l.depth + 1) - if ok { - caller := fmt.Sprintf("%s:%d", filepath.Base(filename), line) - args = append(args, "caller", caller) - } - } - f := strings.Repeat(" %s=%v", len(args)/2)[1:] - if len(args)%2 == 1 { - f += " UNKNOWN=%v" - } - l.logger.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.debug { - 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 { - l2 := *l - l2.context = append(l2.context, args...) - return &l2 -} diff --git a/workflow/inventory/workflow.go b/workflow/inventory/workflow.go index de4b1a6..83c5a2c 100644 --- a/workflow/inventory/workflow.go +++ b/workflow/inventory/workflow.go @@ -7,10 +7,11 @@ import ( "time" "github.com/jessepeterson/mdmcommands" - "github.com/micromdm/nanocmd/log" "github.com/micromdm/nanocmd/subsystem/inventory/storage" "github.com/micromdm/nanocmd/utils/uuid" "github.com/micromdm/nanocmd/workflow" + + "github.com/micromdm/nanolib/log" ) const WorkflowName = "io.micromdm.wf.inventory.v1"