Skip to content

Commit

Permalink
add logger constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
ezekg committed Jul 15, 2024
1 parent 59eef38 commit 5049fa5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
2 changes: 1 addition & 1 deletion keygen.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var (

// Logger is a leveled logger implementation used for printing debug,
// informational, warning, and error messages.
Logger LeveledLogger = &logger{Level: LogLevelError}
Logger LeveledLogger = NewLogger(LogLevelError)

// Program is the name of the current program, used when installing
// upgrades. Defaults to the current program name.
Expand Down
41 changes: 32 additions & 9 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keygen

import (
"fmt"
"io"
"os"
)

Expand All @@ -15,54 +16,76 @@ const (
LogLevelDebug
)

// LoggerOptions stores config options used for the logger e.g. log streams.
type LoggerOptions struct {
Stdout io.Writer
Stderr io.Writer
}

type logger struct {
Level LogLevel
LoggerOptions
}

func (l *logger) Errorf(format string, v ...interface{}) {
if l.Level < LogLevelError {
return
}

fmt.Fprintf(os.Stderr, "[ERROR] "+format+"\n", v...)
fmt.Fprintf(l.Stderr, "[ERROR] "+format+"\n", v...)
}

func (l *logger) Warnf(format string, v ...interface{}) {
if l.Level < LogLevelWarn {
return
}

fmt.Fprintf(os.Stderr, "[WARN] "+format+"\n", v...)
fmt.Fprintf(l.Stderr, "[WARN] "+format+"\n", v...)
}

func (l *logger) Infof(format string, v ...interface{}) {
if l.Level < LogLevelInfo {
return
}

fmt.Fprintf(os.Stdout, "[INFO] "+format+"\n", v...)
fmt.Fprintf(l.Stdout, "[INFO] "+format+"\n", v...)
}

func (l *logger) Debugf(format string, v ...interface{}) {
if l.Level < LogLevelDebug {
return
}

fmt.Fprintf(os.Stdout, "[DEBUG] "+format+"\n", v...)
fmt.Fprintf(l.Stdout, "[DEBUG] "+format+"\n", v...)
}

// LeveledLogger provides a basic leveled logging interface for
// printing debug, informational, warning, and error messages.
type LeveledLogger interface {
// Debugf logs a debug message using Printf conventions.
Debugf(format string, v ...interface{})

// Errorf logs a warning message using Printf conventions.
Errorf(format string, v ...interface{})

// Warnf logs a warning message using Printf conventions.
Warnf(format string, v ...interface{})

// Infof logs an informational message using Printf conventions.
Infof(format string, v ...interface{})

// Warnf logs a warning message using Printf conventions.
Warnf(format string, v ...interface{})
// Debugf logs a debug message using Printf conventions.
Debugf(format string, v ...interface{})
}

// NewClient creates a new leveled logger with default log streams to stdout and stderr.
func NewLogger(level LogLevel) LeveledLogger {
return &logger{level, LoggerOptions{os.Stdout, os.Stderr}}
}

// NewLoggerWithOptions creates a new leveled logger with custom log streams.
func NewLoggerWithOptions(level LogLevel, options *LoggerOptions) LeveledLogger {
return &logger{level, *options}
}

// NewNilLogger creates a new leveled logger with discarded log steams.
func NewNilLogger() LeveledLogger {
return &logger{LogLevelNone, LoggerOptions{io.Discard, io.Discard}}
}

0 comments on commit 5049fa5

Please sign in to comment.