log

package
v0.0.0-...-752f67b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 10, 2022 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Cyan = []byte{'\033', '[', '3', '6', ';', '1', 'm'}
)
View Source
var Default = Logger{
	Out:   os.Stderr,
	Level: LevelInfo,
}

Default is a shared default logger that logs to os.Stderr at LevelInfo. It is used by the package level functions for their logger.

View Source
var IsTTY bool

Functions

func Debug

func Debug(args ...interface{})

func Error

func Error(args ...interface{})

func FormatRFC3339

func FormatRFC3339(t time.Time) string

func Info

func Info(args ...interface{})

func Middleware

func Middleware(next http.Handler) http.Handler

func Warn

func Warn(args ...interface{})

Types

type JSONFormatter

type JSONFormatter struct {
	Out io.Writer
}

func (JSONFormatter) Write

func (f JSONFormatter) Write(p []byte) (int, error)

type Level

type Level int

Level is the log level we're to be writing at. Using an int allows the levels to be compared, so we can check if the log message should be written with easy comparison.

const (
	LevelDebug Level = iota // debug
	LevelInfo               // info
	LevelWarn               // warn
	LevelError              // error
	LevelFatal              // fatal
)

func ParseLevel

func ParseLevel(in string) (Level, error)

ParseLevel parses a string, matching it to a Level. If the input fails to match any known level, it defaults to LevelInfo.

func (Level) Log

func (l Level) Log(w io.Writer, args ...interface{})

Log writes the log line to the provider io.Writer. The first in the args is assumed to be the message, all other pairs are assumed to be key/value pairs of additional detail. If there are an odd number of args (after the first becomes the message) it is dropped. When the args are empty, this function is a noop.

The key/value pairs are all written in alphabetical order. The time, level, and message keys are added to the output.

The time will always be in UTC, formated as RFC3339.

func (Level) String

func (i Level) String() string

type Logger

type Logger struct {
	Out   io.Writer
	Level Level
	// contains filtered or unexported fields
}

Logger is a simple logger that has three levels: Debug, Info, and Error. Other than the name of the levels (and subsequent conditional output) the levels are identical in function. A logger without configuration writes at DebugLevel to os.Stderr.

This may not be the most efficient or featureful logger out there -- it doesn't do colors, doesn't support multiple output formats, doesn't have much in the way of fancy hooks and such -- it just does the bare minimum that I need/want it to do. It *does* provide:

  • structured logging with fields
  • multiple log levels
  • a simple interface
  • no external dependencies

If fancier/more-powerful features are needed/wanted... use a different package.

func ForRequest

func ForRequest(r *http.Request, args ...interface{}) Logger

func WithError

func WithError(err error) Logger

func WithFields

func WithFields(args ...interface{}) Logger

func WithLevel

func WithLevel(lvl Level) Logger

func (Logger) Debug

func (l Logger) Debug(args ...interface{})

Debug emits logs when the set level is Debug or lower

func (Logger) Error

func (l Logger) Error(args ...interface{})

Error emits logs when the set level is Error or lower

func (Logger) Fatal

func (l Logger) Fatal(args ...interface{})

Fatal emits logs when the set level is Fatal or lower

func (Logger) Fatalf

func (l Logger) Fatalf(format string, args ...interface{})

func (Logger) Info

func (l Logger) Info(args ...interface{})

Info emits logs when the set level is Info or lower

func (Logger) Middleware

func (l Logger) Middleware(next http.Handler) http.Handler

func (Logger) Print

func (l Logger) Print(args ...interface{})

Print emits a log at the current level

func (Logger) Printf

func (l Logger) Printf(format string, args ...any)

Printf emits a formattable log at the current level

func (Logger) Println

func (l Logger) Println(args ...any)

Println emits a log at the current level. This is a shim to comply with the Log interface from the stdlib

func (Logger) Warn

func (l Logger) Warn(args ...interface{})

Warn emits logs when the set level is Warn or lower

func (Logger) WithError

func (l Logger) WithError(err error) Logger

func (Logger) WithFields

func (l Logger) WithFields(args ...interface{}) Logger

WithFields provides some sugar to build the fields up over time until calling one of the Level-based functions (Debug, et. al.) and all uses of the Logger returned share the same fields.

func (Logger) WithLevel

func (l Logger) WithLevel(lvl Level) Logger

type PrettyFormatter

type PrettyFormatter struct {
	Out           io.Writer
	DisableColors bool
}

func (PrettyFormatter) Write

func (f PrettyFormatter) Write(p []byte) (int, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL