llog

package
v0.0.0-...-d2acac8 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2024 License: BSD-3-Clause Imports: 9 Imported by: 6

Documentation

Overview

Package llog is a dirt-simple leveled text logger.

Example
package main

import (
	"flag"
	"log/slog"

	"github.com/u-root/uio/llog"
)

func someFunc(v llog.Printf) {
	v("logs at the given level %s", "foo")
}

func main() {
	l := llog.Default()
	// If -v is set, l.Level becomes slog.LevelDebug.
	l.RegisterDebugFlag(flag.CommandLine, "v")
	flag.Parse()

	someFunc(l.Debugf)
	someFunc(l.AtLevelFunc(slog.LevelWarn))
	someFunc(l.Infof)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Logger

type Logger struct {
	Sink  Sink
	Level slog.Level
}

Logger is a dirt-simple leveled logger.

If the log level is >= Level, it logs to the given Sink.

Logger or Sink may be nil in order to log nothing.

func Debug

func Debug() *Logger

Debug prints to log.Printf at the debug level.

func Default

func Default() *Logger

Default is the stdlib default log sink.

Example (Withtime)
package main

import (
	"log/slog"

	"github.com/u-root/uio/llog"
)

func main() {
	l := llog.Default()
	l.Infof("An INFO level string")
	l.Debugf("A DEBUG level that does not appear")

	l.Level = slog.LevelDebug
	l.Debugf("A DEBUG level that appears")
}
Output:

func New

func New(l slog.Level, p Printf) *Logger

New creates a logger from p which prepends the log level to the output and uses l as the default log level.

Logs with level >= l will be printed using p.

func Test

func Test(tb testing.TB) *Logger

Test is a logger that prints every level to t.Logf.

func (*Logger) AtLevel

func (l *Logger) AtLevel(level slog.Level) Printfer

AtLevel returns a Printfer that can be passed around to log at the given level.

AtLevel never returns nil.

func (*Logger) AtLevelFunc

func (l *Logger) AtLevelFunc(level slog.Level) Printf

AtLevelFunc returns a Printf that can be passed around to log at the given level.

AtLevelFunc never returns nil.

func (*Logger) Debugf

func (l *Logger) Debugf(fmt string, args ...any)

Debugf is a printf function that logs at the Debug level.

func (*Logger) Errorf

func (l *Logger) Errorf(fmt string, args ...any)

Errorf is a printf function that logs at the Error level.

func (*Logger) Infof

func (l *Logger) Infof(fmt string, args ...any)

Infof is a printf function that logs at the Info level.

func (*Logger) Logf

func (l *Logger) Logf(level slog.Level, fmt string, args ...any)

Logf logs at the given level.

func (*Logger) RegisterDebugFlag

func (l *Logger) RegisterDebugFlag(f *flag.FlagSet, flagName string)

RegisterDebugFlag registers a boolean flag that, if set, assigns LevelDebug as the level.

func (*Logger) RegisterLevelFlag

func (l *Logger) RegisterLevelFlag(f *flag.FlagSet, flagName string)

RegisterLevelFlag registers a flag that sets the given numeric level as the level.

func (*Logger) RegisterVerboseFlag

func (l *Logger) RegisterVerboseFlag(f *flag.FlagSet, flagName string, verboseLevel slog.Level)

RegisterVerboseFlag registers a boolean flag that, if set, assigns verboseLevel as the level.

func (*Logger) Warnf

func (l *Logger) Warnf(fmt string, args ...any)

Warnf is a printf function that logs at the Warn level.

type Printf

type Printf func(format string, v ...any)

Printf is a logger printf function.

func MultiPrintf

func MultiPrintf(p ...Printf) Printf

MultiPrintf is a Printf that prints to all given p.

func WritePrintf

func WritePrintf(w io.Writer) Printf

WritePrintf is a Printf that prints lines to w.

type Printfer

type Printfer interface {
	Printf(format string, v ...any)
}

Printfer is an interface implementing Printf.

type Sink

type Sink func(level slog.Level, format string, v ...any)

Sink is the output for Logger.

func SinkFor

func SinkFor(p Printf) Sink

SinkFor prepends the log with a log level and outputs to p.

Jump to

Keyboard shortcuts

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