log

package module
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2022 License: MIT Imports: 5 Imported by: 6

README

log - Simple leveled logging based on standard log package

logo

Documentation

Build codecov

Benefits

  • 😻 Leveled logging
  • 😚 Simple API
  • 🤝 fmt friendly
  • 👌 Zero dependencies
  • 😮‍💨 No global logger
  • 👏 No structured logging bullshit

Installation

go get github.com/heartwilltell/log

Leveled logging

The StdLog implements a simple interface:

// Logger formats the message according to standard format specifiers from the fmt package
// and writes the message to writer specified by the concrete interface implementation.
type Logger interface {
	// Error formats and writes the error level message.
	Error(format string, v ...any)
	// Warning formats and writes the warning level message.
	Warning(format string, v ...any)
	// Info formats and writes the information level message.
	Info(format string, v ...any)
	// Debug formats and writes the debug level message.
	Debug(format string, v ...any)
}

Usage

👇 The usage is pretty simple. Just create a logger instance and call any of leveled methods.

logger := log.New()
logger.Info("Listen on port: %d", 8080)

👇 Sets the logging level to debug level.

logger := log.New(log.WithLevel(log.DBG))

👇 Parses string to level and creates logger with warning level.

level, levelErr := log.ParseLevel("warning")
if levelErr != nil {
	// handle error here
}

logger := log.New(log.WithLevel(level))

👇 Creates logger with different io.Writer.

var w bytes.Buffer 

logger := log.New(log.WithWriter(w))

👇 Disables the colorful output.

logger := log.New(log.WithNoColor())

👇 Sets the UTC time format.

logger := log.New(log.WithUTC())

👇 Enables printing the code line number.

// Short format:
// INF: 2022/07/08 11:22:30 server.go:111: message
logger := log.New(log.WithLineNum(log.ShortFmt))

OR

// Long format:
// INF: 2022/07/08 11:22:30 /Users/heartwilltell/Go/app/server.go:111: message
logger := log.New(log.WithLineNum(log.LongFmt))

👇 Sets the level mark at the end of log prefix.

logger := log.New(log.WithLevelAtPrefixEnd())

Will produce this 👇

// 2022/07/08 11:22:30 INF: message

Instead of this 👇

// INF: 2022/07/08 11:22:30: message

👇 Creates nop logger which implements log.Logger interface.

logger := log.NewNopLog()

💡 Useful for tests or places where logger should be disabled by default

License

MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error string

Error represents package level error related to logging work.

const (
	// ErrParseLevel indicates that string given to function ParseLevel can't be parsed to Level.
	ErrParseLevel Error = "string can't be parsed as Level, use: `error`, `warning`, `info`, `debug`"
)

func (Error) Error

func (e Error) Error() string

type Level

type Level byte

Level represents an enumeration of logging levels.

const (
	// ERR represents error logging level.
	ERR Level = iota
	// WRN represents warning logging level.
	WRN
	// INF represents info logging level.
	INF
	// DBG represents debug logging level.
	DBG
)

func ParseLevel

func ParseLevel(lvl string) (Level, error)

ParseLevel takes the string and tries to parse it to the Level.

func (Level) String

func (l Level) String() string

type LineNumFmt added in v1.0.0

type LineNumFmt byte

LineNumFmt represents an enumeration of formats for printing the code line number in log output.

const (
	// ShortFmt represents format of the code line number.
	// Contains resulting file name and line number.
	ShortFmt LineNumFmt = iota

	// LongFmt represents format of the code line number.
	// Contains full file path, name and line number.
	LongFmt
)

type Logger

type Logger interface {
	// Error formats and writes the error level message.
	Error(format string, v ...any)
	// Warning formats and writes the warning level message.
	Warning(format string, v ...any)
	// Info formats and writes the information level message.
	Info(format string, v ...any)
	// Debug formats and writes the debug level message.
	Debug(format string, v ...any)
}

Logger formats the message according to standard format specifiers from the fmt package and writes the message to writer specified by the concrete interface implementation.

type NopLog

type NopLog struct{}

NopLog represents empty/disabled implementation of Logger interface.

func NewNopLog

func NewNopLog() NopLog

NewNopLog returns a new instance of NopLog.

func (NopLog) Debug

func (l NopLog) Debug(string, ...any)

func (NopLog) Error

func (l NopLog) Error(string, ...any)

func (NopLog) Info

func (l NopLog) Info(string, ...any)

func (NopLog) Warning

func (l NopLog) Warning(string, ...any)

type Option

type Option func(l *StdLog)

Option represents a functional option type which can be passed to the NewStdLog function to change its underlying properties.

func WithLevel

func WithLevel(level Level) Option

WithLevel changes the underlying logging level of StdLog to the given on.

func WithLevelAtPrefixEnd added in v1.1.0

func WithLevelAtPrefixEnd() Option

WithLevelAtPrefixEnd sets the level mark at the end of log prefix.

func WithLineNum added in v0.1.2

func WithLineNum(format LineNumFmt) Option

WithLineNum enables the printing of code line number in log output.

func WithNoColor added in v0.1.2

func WithNoColor() Option

WithNoColor disables the color output of StdLog.

func WithNoDateTime added in v1.1.3

func WithNoDateTime() Option

WithNoDateTime disables the output of date and time of StdLog.

func WithUTC added in v0.1.2

func WithUTC() Option

WithUTC sets the log time format to UTC.

func WithWriter

func WithWriter(w io.Writer) Option

WithWriter changes the writer for each leveled loggers of StdLog to the given on.

type StdLog

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

StdLog represents wrapper around standard library logger which implements Logger interface.

func New

func New(options ...Option) *StdLog

New returns a new instance of StdLog struct. Takes variadic options which will be applied to StdLog.

func NewStdLog

func NewStdLog(options ...Option) *StdLog

NewStdLog returns a new instance of StdLog struct. Takes variadic options which will be applied to StdLog. Deprecated: use New instead.

func (*StdLog) Debug

func (l *StdLog) Debug(format string, v ...any)

func (*StdLog) Error

func (l *StdLog) Error(format string, v ...any)

func (*StdLog) Info

func (l *StdLog) Info(format string, v ...any)

func (*StdLog) Warning

func (l *StdLog) Warning(format string, v ...any)

Jump to

Keyboard shortcuts

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