level

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2022 License: MIT Imports: 6 Imported by: 4

README

level

Build

level is an opinionated, no-frills level logger library. It is intended to be simple and quick to use, but not guaranteed to be enough for large projects.

Installing

This library can be installed with:

go get -u github.com/mattmeyers/level

Usage

This library exposes the simple Logger interface that describes five basic logging levels. The interface is defined as

type Logger interface {
	Debug(format string, args ...interface{})
	Info(format string, args ...interface{})
	Warn(format string, args ...interface{})
	Error(format string, args ...interface{})
	Fatal(format string, args ...interface{})
}

The minimum level at which a logger logs is defined by the following values. The lower the value, the more that gets logged.

const (
	Debug Level = iota
	Info
	Warn
	Error
	Fatal
)

This library provides two implementations of the interface:

  1. BasicLogger
  2. NullLogger

The BasicLogger implementation logs messages to the provided writer. For example:

// In main.go
package main

import (
	"os"
	
	"github.com/mattmeyers/level"
)

func main() {
	logger, _ := level.NewBasicLogger(level.Info, os.Stdout)
	
	// This will not be printed.
	logger.Debug("Starting...")

	value, err := doThing()
	if err != nil {
		// The message will be printed, then the process will exit.
		logger.Fatal("Something bad happened: %v", err)
	}

	// This message will get printed.
	logger.Info("Got some value: %v", value)
}

func doThing() (int, error) {
	return 1, nil
}

Running this will produce

$ go run main.go
2022-03-29T23:17:18-04:00 [INFO]: Got some value: 1

The NullLogger is an implementation that does nothing. All log statements will be thrown away. Note that calling Fatal will still cause the process to exit though.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BasicLogger

type BasicLogger struct {
	// contains filtered or unexported fields
}

BasicLogger implements the Logger interface using the defined Level constants. The provided level is treated as the minimum. Any messages passed to a level that is at least the defined level will be printed.

Every log message is treated as a single line. If there is no newline at the end of the message, then one will be added.

func NewBasicLogger

func NewBasicLogger(level Level, out io.Writer) (*BasicLogger, error)

NewBasicLogger constructs a new logger. An error will be returned if an invalid level is provided. If no output writer is provided, then os.Stdout will be used.

func (*BasicLogger) Debug

func (l *BasicLogger) Debug(format string, args ...interface{})

Logs at the Debug level.

func (*BasicLogger) Error

func (l *BasicLogger) Error(format string, args ...interface{})

Logs at the Error level.

func (*BasicLogger) Fatal

func (l *BasicLogger) Fatal(format string, args ...interface{})

Logs at the Fatal level then calls os.Exit(1).

func (*BasicLogger) Info

func (l *BasicLogger) Info(format string, args ...interface{})

Logs at the Info level.

func (*BasicLogger) Warn

func (l *BasicLogger) Warn(format string, args ...interface{})

Logs at the Warn level.

type Level

type Level int

Level represents a logging level. This restricts the logger to print only messages with at least this level.

const (
	Debug Level = iota
	Info
	Warn
	Error
	Fatal
)

The available logging levels.

func ParseLevel

func ParseLevel(l string) (Level, error)

ParseLevel converts a string to the corresponding Level. Comparisons are case insensitive. If an unknown level is provided, then an error will be returned.

func (Level) String

func (l Level) String() string

type Logger

type Logger interface {
	// Logs at the Debug level.
	Debug(format string, args ...interface{})
	// Logs at the Info level.
	Info(format string, args ...interface{})
	// Logs at the Warn level.
	Warn(format string, args ...interface{})
	// Logs at the Error level.
	Error(format string, args ...interface{})
	// Logs at the Fatal level then calls os.Exit(1).
	Fatal(format string, args ...interface{})
}

Logger represents a standard level logging interface. Every method logs the provided message using fmt.Printf with a timestamp and level prefix. Fatal logs the message like the other methods, but calls os.Exit(1) afterwards.

type NullLogger added in v0.2.0

type NullLogger struct{}

NullLogger is a logger that does nothing. All log statements, regardless of level, will be ignored. Note that calling Fatal will still cause the process to exit.

func NewNullLogger added in v0.2.0

func NewNullLogger() NullLogger

func (NullLogger) Debug added in v0.2.0

func (NullLogger) Debug(format string, args ...interface{})

Logs at the Debug level.

func (NullLogger) Error added in v0.2.0

func (NullLogger) Error(format string, args ...interface{})

Logs at the Error level.

func (NullLogger) Fatal added in v0.2.0

func (NullLogger) Fatal(format string, args ...interface{})

Logs at the Fatal level then calls os.Exit(1).

func (NullLogger) Info added in v0.2.0

func (NullLogger) Info(format string, args ...interface{})

Logs at the Info level.

func (NullLogger) Warn added in v0.2.0

func (NullLogger) Warn(format string, args ...interface{})

Logs at the Warn level.

Jump to

Keyboard shortcuts

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