logger

package module
v0.0.0-...-bbf514f Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2023 License: MIT Imports: 14 Imported by: 5

README

Logger

Logger is a very simple and fast logger that can write both machine-readable and human-readable logs.

Why?

I made logger because I really liked zerolog, and especially its ConsoleLogger, as it looks really nice in the terminal. I use it in all my command-line applications, but there's an issue. Zerolog can only output in JSON or CBOR, depending on buid tags. It cannot produce human-readable output. Therefore, zerolog has to use reflection to unmarshal JSON generated by the logger and then make human-readable output out of it. This is, in my opinion, incredibly and unnecessarily wasteful, so I made logger. Logger can use any logging implementation that implements the logger.Logger interface. I currently have four implementations: JSONLogger, PrettyLogger, MultiLogger, and NopLogger. The names should be self-explanatory.

Who is logger for?

If you need a fast and simple logger, but don't need more advanced features such as context and hooks, logger is for you. It cuts out unnecessary features, providing a very simple codebase that is easy to test and has much less potential for bugs, and it's faster than zerolog while also allowing more flexibility and a similar API.

Who is logger not for?

If you need more advanced features, such as context and hooks, use either zerolog or the even faster zap library. Logger keeps it as simple as possible, only providing logging and nothing else.

Benchmarks

Logger is very fast. Here are its benchmarks, done on my laptop:

goos: linux
goarch: amd64
pkg: logger
cpu: 11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz
BenchmarkJSON/one-field-8                4216245               294.0 ns/op           160 B/op          3 allocs/op
BenchmarkJSON/two-field-8                1939634               594.3 ns/op           188 B/op          5 allocs/op
BenchmarkJSON/all-8                       310526              3955 ns/op             752 B/op         21 allocs/op
BenchmarkPretty/one-field-8              1603789               658.5 ns/op           168 B/op          4 allocs/op
BenchmarkPretty/two-field-8              1388920               864.5 ns/op           200 B/op          6 allocs/op
BenchmarkPretty/all-8                     285554              3726 ns/op             760 B/op         22 allocs/op

To run the benchmarks yourself, simply clone this repo and run go test -bench=.. Keep in mind that they will be different, depending on what your computer's specs are.

Example
package main

import (
    "strconv"
    "os"

    "go.arsenm.dev/logger/log"
    "go.arsenm.dev/logger"
)

func init() {
    // Default logger is JSONLogger going to stderr
    log.Logger = logger.NewPretty(os.Stdout)
}

func main() {
    s := "hello"
    i, err := strconv.Atoi(s)
    if err != nil {
        log.Error("Couldn't convert to integer").
            Str("input", s).
            Err(err).
            Send()
    }
    log.Info("Converted to integer").
        Int("output", i).
        Send()
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoSuchLevel = errors.New("no such log level")

ErrNoSuchLevel is returned when ParseLogLevel cannot find a log level corresponding to the provided string

Functions

This section is empty.

Types

type CLILogBuilder

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

CLILogBuilder implements the LogBuilder interface using human-readable output for log messages

func (*CLILogBuilder) Any

func (plb *CLILogBuilder) Any(key string, val any) LogBuilder

Any uses reflection to marshal any type and writes the result as a field to the output. This is much slower than the type-specific functions.

func (*CLILogBuilder) Bool

func (plb *CLILogBuilder) Bool(key string, val bool) LogBuilder

Bool adds a bool as a field to the output

func (*CLILogBuilder) Bytes

func (plb *CLILogBuilder) Bytes(key string, b []byte) LogBuilder

Bytes writes hex-encoded bytes as a field to the output

func (*CLILogBuilder) Err

func (plb *CLILogBuilder) Err(err error) LogBuilder

Err adds an error as a field to the output

func (*CLILogBuilder) Float32

func (plb *CLILogBuilder) Float32(key string, val float32) LogBuilder

Float32 adds a float32 field to the output

func (*CLILogBuilder) Float64

func (plb *CLILogBuilder) Float64(key string, val float64) LogBuilder

Float64 adds a float64 field to the output

func (*CLILogBuilder) Int

func (plb *CLILogBuilder) Int(key string, val int) LogBuilder

Int adds an int field to the output

func (*CLILogBuilder) Int16

func (plb *CLILogBuilder) Int16(key string, val int16) LogBuilder

Int16 adds an int16 field to the output

func (*CLILogBuilder) Int32

func (plb *CLILogBuilder) Int32(key string, val int32) LogBuilder

Int32 adds an int32 field to the output

func (*CLILogBuilder) Int64

func (plb *CLILogBuilder) Int64(key string, val int64) LogBuilder

Int64 adds an int64 field to the output

func (*CLILogBuilder) Int8

func (plb *CLILogBuilder) Int8(key string, val int8) LogBuilder

Int8 adds an int8 field to the output

func (*CLILogBuilder) Send

func (plb *CLILogBuilder) Send()

Send sends the event to the output.

After calling send, do not use the event again.

func (*CLILogBuilder) Str

func (plb *CLILogBuilder) Str(key, val string) LogBuilder

Str adds a string as a field to the output

func (*CLILogBuilder) Stringer

func (plb *CLILogBuilder) Stringer(key string, s fmt.Stringer) LogBuilder

Stringer calls the String method of an fmt.Stringer and adds the resulting string as a field to the output

func (*CLILogBuilder) Timestamp

func (plb *CLILogBuilder) Timestamp() LogBuilder

Timestamp adds the time formatted as RFC3339Nano as a field to the output

func (*CLILogBuilder) Uint

func (plb *CLILogBuilder) Uint(key string, val uint) LogBuilder

Uint adds a uint field to the output

func (*CLILogBuilder) Uint16

func (plb *CLILogBuilder) Uint16(key string, val uint16) LogBuilder

Uint16 adds a uint16 field to the output

func (*CLILogBuilder) Uint32

func (plb *CLILogBuilder) Uint32(key string, val uint32) LogBuilder

Uint32 adds a uint32 field to the output

func (*CLILogBuilder) Uint64

func (plb *CLILogBuilder) Uint64(key string, val uint64) LogBuilder

Uint64 adds a uint64 field to the output

func (*CLILogBuilder) Uint8

func (plb *CLILogBuilder) Uint8(key string, val uint8) LogBuilder

Uint8 adds a uint8 field to the output

type CLILogger

type CLILogger struct {
	Out      io.Writer
	Level    LogLevel
	UseColor bool

	MsgColor color.Color
	KeyColor color.Color

	DebugColor color.Color
	InfoColor  color.Color
	WarnColor  color.Color
	ErrColor   color.Color
	FatalColor color.Color
	PanicColor color.Color
	// contains filtered or unexported fields
}

CLILogger is a logger meant to be used for CLI tools where users will view and read the logs throughout the application's execution

func NewCLI

func NewCLI(out io.Writer) *CLILogger

NewCLI creates and returns a new CLILogger

func (*CLILogger) Debug

func (pl *CLILogger) Debug(msg string) LogBuilder

Debug creates a new debug event with the given message

func (*CLILogger) Debugf

func (pl *CLILogger) Debugf(format string, v ...any) LogBuilder

Debugf creates a new debug event with the formatted message

func (*CLILogger) Error

func (pl *CLILogger) Error(msg string) LogBuilder

Error creates a new error event with the given message

func (*CLILogger) Errorf

func (pl *CLILogger) Errorf(format string, v ...any) LogBuilder

Errorf creates a new error event with the formatted message

func (*CLILogger) Fatal

func (pl *CLILogger) Fatal(msg string) LogBuilder

Fatal creates a new fatal event with the given message

When sent, fatal events will cause a call to os.Exit(1)

func (*CLILogger) Fatalf

func (pl *CLILogger) Fatalf(format string, v ...any) LogBuilder

Fatalf creates a new fatal event with the formatted message

When sent, fatal events will cause a call to os.Exit(1)

func (*CLILogger) Info

func (pl *CLILogger) Info(msg string) LogBuilder

Info creates a new info event with the given message

func (*CLILogger) Infof

func (pl *CLILogger) Infof(format string, v ...any) LogBuilder

Infof creates a new info event with the formatted message

func (*CLILogger) NoExit

func (pl *CLILogger) NoExit()

NoExit prevents the logger from exiting on fatal events

func (*CLILogger) NoPanic

func (pl *CLILogger) NoPanic()

NoPanic prevents the logger from panicking on panic events

func (*CLILogger) Panic

func (pl *CLILogger) Panic(msg string) LogBuilder

Panic creates a new panic event with the given message

When sent, panic events will cause a panic

func (*CLILogger) Panicf

func (pl *CLILogger) Panicf(format string, v ...any) LogBuilder

Panicf creates a new panic event with the formatted message

When sent, panic events will cause a panic

func (*CLILogger) SetLevel

func (pl *CLILogger) SetLevel(l LogLevel)

SetLevel sets the log level of the logger

func (*CLILogger) Warn

func (pl *CLILogger) Warn(msg string) LogBuilder

Warn creates a new warn event with the given message

func (*CLILogger) Warnf

func (pl *CLILogger) Warnf(format string, v ...any) LogBuilder

Warnf creates a new warn event with the formatted message

type JSONLogBuilder

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

JSONLogBuilder implements the LogBuilder interface using JSON for log messages

func (*JSONLogBuilder) Any

func (jlb *JSONLogBuilder) Any(key string, val any) LogBuilder

Any uses reflection to marshal any type and writes the result as a field to the output. This is much slower than the type-specific functions.

func (*JSONLogBuilder) Bool

func (jlb *JSONLogBuilder) Bool(key string, val bool) LogBuilder

Bool adds a bool as a field to the output

func (*JSONLogBuilder) Bytes

func (jlb *JSONLogBuilder) Bytes(key string, b []byte) LogBuilder

Bytes writes base64-encoded bytes as a field to the output

func (*JSONLogBuilder) Err

func (jlb *JSONLogBuilder) Err(err error) LogBuilder

Err adds an error as a field to the output

func (*JSONLogBuilder) Float32

func (jlb *JSONLogBuilder) Float32(key string, val float32) LogBuilder

Float32 adds a float32 field to the output

func (*JSONLogBuilder) Float64

func (jlb *JSONLogBuilder) Float64(key string, val float64) LogBuilder

Float64 adds a float64 field to the output

func (*JSONLogBuilder) Int

func (jlb *JSONLogBuilder) Int(key string, val int) LogBuilder

Int adds an int field to the output

func (*JSONLogBuilder) Int16

func (jlb *JSONLogBuilder) Int16(key string, val int16) LogBuilder

Int16 adds an int16 field to the output

func (*JSONLogBuilder) Int32

func (jlb *JSONLogBuilder) Int32(key string, val int32) LogBuilder

Int32 adds an int32 field to the output

func (*JSONLogBuilder) Int64

func (jlb *JSONLogBuilder) Int64(key string, val int64) LogBuilder

Int64 adds an int64 field to the output

func (*JSONLogBuilder) Int8

func (jlb *JSONLogBuilder) Int8(key string, val int8) LogBuilder

Int8 adds an int8 field to the output

func (*JSONLogBuilder) Send

func (jlb *JSONLogBuilder) Send()

Send sends the event to the output.

After calling send, do not use the event again.

func (*JSONLogBuilder) Str

func (jlb *JSONLogBuilder) Str(key, val string) LogBuilder

Str adds a string as a field to the output

func (*JSONLogBuilder) Stringer

func (jlb *JSONLogBuilder) Stringer(key string, s fmt.Stringer) LogBuilder

Stringer calls the String method of an fmt.Stringer and adds the resulting string as a field to the output

func (*JSONLogBuilder) Timestamp

func (jlb *JSONLogBuilder) Timestamp() LogBuilder

Timestamp adds the time formatted as RFC3339Nano as a field to the output using the key "timestamp"

func (*JSONLogBuilder) Uint

func (jlb *JSONLogBuilder) Uint(key string, val uint) LogBuilder

Uint adds a uint field to the output

func (*JSONLogBuilder) Uint16

func (jlb *JSONLogBuilder) Uint16(key string, val uint16) LogBuilder

Uint16 adds a uint16 field to the output

func (*JSONLogBuilder) Uint32

func (jlb *JSONLogBuilder) Uint32(key string, val uint32) LogBuilder

Uint32 adds a uint32 field to the output

func (*JSONLogBuilder) Uint64

func (jlb *JSONLogBuilder) Uint64(key string, val uint64) LogBuilder

Uint64 adds a uint64 field to the output

func (*JSONLogBuilder) Uint8

func (jlb *JSONLogBuilder) Uint8(key string, val uint8) LogBuilder

Uint8 adds a uint8 field to the output

type JSONLogger

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

JSONLogger implements the Logger interface using JSON for log messages.

func NewJSON

func NewJSON(out io.Writer) *JSONLogger

NewJSON creates and returns a new JSONLogger If the input writer is io.Discard, NopLogger will be returned.

func (*JSONLogger) Debug

func (jl *JSONLogger) Debug(msg string) LogBuilder

Debug creates a new debug event with the given message

func (*JSONLogger) Debugf

func (jl *JSONLogger) Debugf(format string, v ...any) LogBuilder

Debugf creates a new debug event with the formatted message

func (*JSONLogger) Error

func (jl *JSONLogger) Error(msg string) LogBuilder

Error creates a new error event with the given message

func (*JSONLogger) Errorf

func (jl *JSONLogger) Errorf(format string, v ...any) LogBuilder

Errorf creates a new error event with the formatted message

func (*JSONLogger) Fatal

func (jl *JSONLogger) Fatal(msg string) LogBuilder

Fatal creates a new fatal event with the given message

When sent, fatal events will cause a call to os.Exit(1)

func (*JSONLogger) Fatalf

func (jl *JSONLogger) Fatalf(format string, v ...any) LogBuilder

Fatalf creates a new fatal event with the formatted message

When sent, fatal events will cause a call to os.Exit(1)

func (*JSONLogger) Info

func (jl *JSONLogger) Info(msg string) LogBuilder

Info creates a new info event with the given message

func (*JSONLogger) Infof

func (jl *JSONLogger) Infof(format string, v ...any) LogBuilder

Infof creates a new info event with the formatted message

func (*JSONLogger) NoExit

func (jl *JSONLogger) NoExit()

NoExit prevents the logger from exiting on fatal events

func (*JSONLogger) NoPanic

func (jl *JSONLogger) NoPanic()

NoPanic prevents the logger from panicking on panic events

func (*JSONLogger) Panic

func (jl *JSONLogger) Panic(msg string) LogBuilder

Panic creates a new panic event with the given message

When sent, panic events will cause a panic

func (*JSONLogger) Panicf

func (jl *JSONLogger) Panicf(format string, v ...any) LogBuilder

Panicf creates a new panic event with the formatted message

When sent, panic events will cause a panic

func (*JSONLogger) SetLevel

func (jl *JSONLogger) SetLevel(l LogLevel)

SetLevel sets the log level of the logger

func (*JSONLogger) Warn

func (jl *JSONLogger) Warn(msg string) LogBuilder

Warn creates a new warn event with the given message

func (*JSONLogger) Warnf

func (jl *JSONLogger) Warnf(format string, v ...any) LogBuilder

Warnf creates a new warn event with the formatted message

type LogBuilder

type LogBuilder interface {
	// Int adds an int field to the output
	Int(string, int) LogBuilder
	// Int8 adds an int8 field to the output
	Int8(string, int8) LogBuilder
	// Int16 adds an int16 field to the output
	Int16(string, int16) LogBuilder
	// Int32 adds an int32 field to the output
	Int32(string, int32) LogBuilder
	// Int64 adds an int64 field to the output
	Int64(string, int64) LogBuilder
	// Uint adds a uint field to the output
	Uint(string, uint) LogBuilder
	// Uint8 adds a uint8 field to the output
	Uint8(string, uint8) LogBuilder
	// Uint16 adds a uint16 field to the output
	Uint16(string, uint16) LogBuilder
	// Uint32 adds a uint32 field to the output
	Uint32(string, uint32) LogBuilder
	// Uint64 adds a uint64 field to the output
	Uint64(string, uint64) LogBuilder
	// Float32 adds a float32 field to the output
	Float32(string, float32) LogBuilder
	// Float64 adds a float64 field to the output
	Float64(string, float64) LogBuilder
	// Stringer calls the String method of an fmt.Stringer
	// and adds the resulting string as a field to the output
	Stringer(string, fmt.Stringer) LogBuilder
	// Bytes adds []byte as a field to the output
	Bytes(string, []byte) LogBuilder
	// Timestamp adds the time formatted as RFC3339Nano
	// as a field to the output using the key "timestamp"
	Timestamp() LogBuilder
	// Bool adds a bool as a field to the output
	Bool(string, bool) LogBuilder
	// Str adds a string as a field to the output
	Str(string, string) LogBuilder
	// Any uses reflection to marshal any type and writes
	// the result as a field to the output. This is much slower
	// than the type-specific functions.
	Any(string, any) LogBuilder
	// Err adds an error as a field to the output
	Err(error) LogBuilder
	// Send sends the event to the output.
	//
	// After calling send, do not use the event again.
	Send()
}

LogBuilder represents a log event builder

type LogLevel

type LogLevel uint8

LogLevel represents a log level

const (
	LogLevelDebug LogLevel = iota
	LogLevelInfo
	LogLevelWarn
	LogLevelError
	LogLevelFatal
	LogLevelPanic
)

Log levels

func ParseLogLevel

func ParseLogLevel(s string) (LogLevel, error)

ParseLogLevel parses a string representing a log level and returns the proper log level

type Logger

type Logger interface {
	// NoPanic prevents the logger from panicking on panic events
	NoPanic()

	// NoExit prevents the logger from exiting on fatal events
	NoExit()

	// SetLevel sets the log level of the logger
	SetLevel(LogLevel)

	// Debug creates a new debug event with the given message
	Debug(string) LogBuilder

	// Debugf creates a new debug event with the formatted message
	Debugf(string, ...any) LogBuilder

	// Info creates a new info event with the given message
	Info(string) LogBuilder

	// Infof creates a new info event with the formatted message
	Infof(string, ...any) LogBuilder

	// Warn creates a new warn event with the given message
	Warn(string) LogBuilder

	// Warnf creates a new warn event with the formatted message
	Warnf(string, ...any) LogBuilder

	// Error creates a new error event with the given message
	Error(string) LogBuilder

	// Errorf creates a new error event with the formatted message
	Errorf(string, ...any) LogBuilder

	// Fatal creates a new fatal event with the given message
	//
	// When sent, fatal events will cause a call to os.Exit(1)
	Fatal(string) LogBuilder

	// Fatalf creates a new fatal event with the formatted message
	//
	// When sent, fatal events will cause a call to os.Exit(1)
	Fatalf(string, ...any) LogBuilder

	// Panic creates a new panic event with the given message
	//
	// When sent, panic events will cause a panic
	Panic(string) LogBuilder

	// Panicf creates a new panic event with the formatted message
	//
	// When sent, panic events will cause a panic
	Panicf(string, ...any) LogBuilder
}

Logger represents a logger

type MultiLogBuilder

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

MultiLogBuilder implements the LogBuilder interface by writing to multiple underlying LogBuilders sequentially.

func (*MultiLogBuilder) Any

func (mlb *MultiLogBuilder) Any(key string, val any) LogBuilder

Any uses reflection to marshal any type and writes the result as a field to the output. This is much slower than the type-specific functions.

func (*MultiLogBuilder) Bool

func (mlb *MultiLogBuilder) Bool(key string, val bool) LogBuilder

Bool adds a bool as a field to the output

func (*MultiLogBuilder) Bytes

func (mlb *MultiLogBuilder) Bytes(key string, b []byte) LogBuilder

Bytes writes base64-encoded bytes as a field to the output

func (*MultiLogBuilder) Err

func (mlb *MultiLogBuilder) Err(err error) LogBuilder

Err adds an error as a field to the output

func (*MultiLogBuilder) Float32

func (mlb *MultiLogBuilder) Float32(key string, val float32) LogBuilder

Float32 adds a float32 field to the output

func (*MultiLogBuilder) Float64

func (mlb *MultiLogBuilder) Float64(key string, val float64) LogBuilder

Float64 adds a float64 field to the output

func (*MultiLogBuilder) Int

func (mlb *MultiLogBuilder) Int(key string, val int) LogBuilder

Int adds an int field to the output

func (*MultiLogBuilder) Int16

func (mlb *MultiLogBuilder) Int16(key string, val int16) LogBuilder

Int16 adds an int16 field to the output

func (*MultiLogBuilder) Int32

func (mlb *MultiLogBuilder) Int32(key string, val int32) LogBuilder

Int32 adds an int32 field to the output

func (*MultiLogBuilder) Int64

func (mlb *MultiLogBuilder) Int64(key string, val int64) LogBuilder

Int64 adds an int64 field to the output

func (*MultiLogBuilder) Int8

func (mlb *MultiLogBuilder) Int8(key string, val int8) LogBuilder

Int8 adds an int8 field to the output

func (*MultiLogBuilder) Send

func (mlb *MultiLogBuilder) Send()

Send sends the event to the output.

After calling send, do not use the event again.

func (*MultiLogBuilder) Str

func (mlb *MultiLogBuilder) Str(key, val string) LogBuilder

Str adds a string as a field to the output

func (*MultiLogBuilder) Stringer

func (mlb *MultiLogBuilder) Stringer(key string, s fmt.Stringer) LogBuilder

Stringer calls the String method of an fmt.Stringer and adds the resulting string as a field to the output

func (*MultiLogBuilder) Timestamp

func (mlb *MultiLogBuilder) Timestamp() LogBuilder

Timestamp adds the time formatted as RFC3339Nano as a field to the output using the key "timestamp"

func (*MultiLogBuilder) Uint

func (mlb *MultiLogBuilder) Uint(key string, val uint) LogBuilder

Uint adds a uint field to the output

func (*MultiLogBuilder) Uint16

func (mlb *MultiLogBuilder) Uint16(key string, val uint16) LogBuilder

Uint16 adds a uint16 field to the output

func (*MultiLogBuilder) Uint32

func (mlb *MultiLogBuilder) Uint32(key string, val uint32) LogBuilder

Uint32 adds a uint32 field to the output

func (*MultiLogBuilder) Uint64

func (mlb *MultiLogBuilder) Uint64(key string, val uint64) LogBuilder

Uint64 adds a uint64 field to the output

func (*MultiLogBuilder) Uint8

func (mlb *MultiLogBuilder) Uint8(key string, val uint8) LogBuilder

Uint8 adds a uint8 field to the output

type MultiLogger

type MultiLogger struct {
	Loggers []Logger
	// contains filtered or unexported fields
}

MultiLogger implements the Logger interface by writing to multiple underlying loggers sequentially.

func NewMulti

func NewMulti(l ...Logger) *MultiLogger

NewMulti creates and returns a new MultiLogger

func (*MultiLogger) Debug

func (ml *MultiLogger) Debug(msg string) LogBuilder

Debug creates a new debug event with the given message

func (*MultiLogger) Debugf

func (ml *MultiLogger) Debugf(format string, v ...any) LogBuilder

Debugf creates a new debug event with the formatted message

func (*MultiLogger) Error

func (ml *MultiLogger) Error(msg string) LogBuilder

Error creates a new error event with the given message

func (*MultiLogger) Errorf

func (ml *MultiLogger) Errorf(format string, v ...any) LogBuilder

Errorf creates a new error event with the formatted message

func (*MultiLogger) Fatal

func (ml *MultiLogger) Fatal(msg string) LogBuilder

Error creates a new error event with the given message

func (*MultiLogger) Fatalf

func (ml *MultiLogger) Fatalf(format string, v ...any) LogBuilder

Errorf creates a new error event with the formatted message

func (*MultiLogger) Info

func (ml *MultiLogger) Info(msg string) LogBuilder

Info creates a new info event with the given message

func (*MultiLogger) Infof

func (ml *MultiLogger) Infof(format string, v ...any) LogBuilder

Infof creates a new info event with the formatted message

func (*MultiLogger) NoExit

func (ml *MultiLogger) NoExit()

NoExit prevents the logger from exiting on fatal events

func (*MultiLogger) NoPanic

func (ml *MultiLogger) NoPanic()

NoPanic prevents the logger from panicking on panic events

func (*MultiLogger) Panic

func (ml *MultiLogger) Panic(msg string) LogBuilder

Error creates a new error event with the given message

func (*MultiLogger) Panicf

func (ml *MultiLogger) Panicf(format string, v ...any) LogBuilder

Errorf creates a new error event with the formatted message

func (*MultiLogger) SetLevel

func (ml *MultiLogger) SetLevel(l LogLevel)

SetLevel sets the log level of the logger

func (*MultiLogger) Warn

func (ml *MultiLogger) Warn(msg string) LogBuilder

Warn creates a new warn event with the given message

func (*MultiLogger) Warnf

func (ml *MultiLogger) Warnf(format string, v ...any) LogBuilder

Warnf creates a new warn event with the formatted message

type NopLogBuilder

type NopLogBuilder struct{}

NopLogBuilder implements the LogBuilder interface using human-readable output for log messages

func (NopLogBuilder) Any

func (nlb NopLogBuilder) Any(key string, val any) LogBuilder

Any uses reflection to marshal any type and writes the result as a field to the output. This is much slower than the type-specific functions.

func (NopLogBuilder) Bool

func (nlb NopLogBuilder) Bool(key string, val bool) LogBuilder

Bool adds a bool as a field to the output

func (NopLogBuilder) Bytes

func (nlb NopLogBuilder) Bytes(key string, b []byte) LogBuilder

Bytes writes hex-encoded bytes as a field to the output

func (NopLogBuilder) Err

func (nlb NopLogBuilder) Err(err error) LogBuilder

Err adds an error as a field to the output

func (NopLogBuilder) Float32

func (nlb NopLogBuilder) Float32(key string, val float32) LogBuilder

Float32 adds a float32 field to the output

func (NopLogBuilder) Float64

func (nlb NopLogBuilder) Float64(key string, val float64) LogBuilder

Float64 adds a float64 field to the output

func (NopLogBuilder) Int

func (nlb NopLogBuilder) Int(key string, val int) LogBuilder

Int adds an int field to the output

func (NopLogBuilder) Int16

func (nlb NopLogBuilder) Int16(key string, val int16) LogBuilder

Int16 adds an int16 field to the output

func (NopLogBuilder) Int32

func (nlb NopLogBuilder) Int32(key string, val int32) LogBuilder

Int32 adds an int32 field to the output

func (NopLogBuilder) Int64

func (nlb NopLogBuilder) Int64(key string, val int64) LogBuilder

Int64 adds an int64 field to the output

func (NopLogBuilder) Int8

func (nlb NopLogBuilder) Int8(key string, val int8) LogBuilder

Int8 adds an int8 field to the output

func (NopLogBuilder) Send

func (nlb NopLogBuilder) Send()

Send sends the event to the output.

After calling send, do not use the event again.

func (NopLogBuilder) Str

func (nlb NopLogBuilder) Str(key, val string) LogBuilder

Str adds a string as a field to the output

func (NopLogBuilder) Stringer

func (nlb NopLogBuilder) Stringer(key string, s fmt.Stringer) LogBuilder

Stringer calls the String method of an fmt.Stringer and adds the resulting string as a field to the output

func (NopLogBuilder) Timestamp

func (nlb NopLogBuilder) Timestamp() LogBuilder

Timestamp adds the time formatted as RFC3339Nano as a field to the output

func (NopLogBuilder) Uint

func (nlb NopLogBuilder) Uint(key string, val uint) LogBuilder

Uint adds a uint field to the output

func (NopLogBuilder) Uint16

func (nlb NopLogBuilder) Uint16(key string, val uint16) LogBuilder

Uint16 adds a uint16 field to the output

func (NopLogBuilder) Uint32

func (nlb NopLogBuilder) Uint32(key string, val uint32) LogBuilder

Uint32 adds a uint32 field to the output

func (NopLogBuilder) Uint64

func (nlb NopLogBuilder) Uint64(key string, val uint64) LogBuilder

Uint64 adds a uint64 field to the output

func (NopLogBuilder) Uint8

func (nlb NopLogBuilder) Uint8(key string, val uint8) LogBuilder

Uint8 adds a uint8 field to the output

type NopLogger

type NopLogger struct{}

NopLogger implements the Logger interface using human-readable output for log messages.

func NewNop

func NewNop() NopLogger

NewNop creates and returns a new NopLogger

func (NopLogger) Debug

func (nl NopLogger) Debug(msg string) LogBuilder

Debug creates a new debug event with the given message

func (NopLogger) Debugf

func (nl NopLogger) Debugf(format string, v ...any) LogBuilder

Debugf creates a new debug event with the formatted message

func (NopLogger) Error

func (nl NopLogger) Error(msg string) LogBuilder

Error creates a new error event with the given message

func (NopLogger) Errorf

func (nl NopLogger) Errorf(format string, v ...any) LogBuilder

Errorf creates a new error event with the formatted message

func (NopLogger) Fatal

func (nl NopLogger) Fatal(msg string) LogBuilder

Fatal creates a new fatal event with the given message

func (NopLogger) Fatalf

func (nl NopLogger) Fatalf(format string, v ...any) LogBuilder

Fatalf creates a new fatal event with the formatted message

func (NopLogger) Info

func (nl NopLogger) Info(msg string) LogBuilder

Info creates a new info event with the given message

func (NopLogger) Infof

func (nl NopLogger) Infof(format string, v ...any) LogBuilder

Infof creates a new info event with the formatted message

func (NopLogger) NoExit

func (nl NopLogger) NoExit()

NoExit prevents the logger from exiting on fatal events

func (NopLogger) NoPanic

func (nl NopLogger) NoPanic()

NoPanic prevents the logger from panicking on panic events

func (NopLogger) Panic

func (nl NopLogger) Panic(msg string) LogBuilder

Panic creates a new panic event with the given message

func (NopLogger) Panicf

func (nl NopLogger) Panicf(format string, v ...any) LogBuilder

Panicf creates a new panic event with the formatted message

func (NopLogger) SetLevel

func (nl NopLogger) SetLevel(LogLevel)

SetLevel sets the log level of the logger

func (NopLogger) Warn

func (nl NopLogger) Warn(msg string) LogBuilder

Warn creates a new warn event with the given message

func (NopLogger) Warnf

func (nl NopLogger) Warnf(format string, v ...any) LogBuilder

Warnf creates a new warn event with the formatted message

type PrettyLogBuilder

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

PrettyLogBuilder implements the LogBuilder interface using human-readable output for log messages

func (*PrettyLogBuilder) Any

func (plb *PrettyLogBuilder) Any(key string, val any) LogBuilder

Any uses reflection to marshal any type and writes the result as a field to the output. This is much slower than the type-specific functions.

func (*PrettyLogBuilder) Bool

func (plb *PrettyLogBuilder) Bool(key string, val bool) LogBuilder

Bool adds a bool as a field to the output

func (*PrettyLogBuilder) Bytes

func (plb *PrettyLogBuilder) Bytes(key string, b []byte) LogBuilder

Bytes writes hex-encoded bytes as a field to the output

func (*PrettyLogBuilder) Err

func (plb *PrettyLogBuilder) Err(err error) LogBuilder

Err adds an error as a field to the output

func (*PrettyLogBuilder) Float32

func (plb *PrettyLogBuilder) Float32(key string, val float32) LogBuilder

Float32 adds a float32 field to the output

func (*PrettyLogBuilder) Float64

func (plb *PrettyLogBuilder) Float64(key string, val float64) LogBuilder

Float64 adds a float64 field to the output

func (*PrettyLogBuilder) Int

func (plb *PrettyLogBuilder) Int(key string, val int) LogBuilder

Int adds an int field to the output

func (*PrettyLogBuilder) Int16

func (plb *PrettyLogBuilder) Int16(key string, val int16) LogBuilder

Int16 adds an int16 field to the output

func (*PrettyLogBuilder) Int32

func (plb *PrettyLogBuilder) Int32(key string, val int32) LogBuilder

Int32 adds an int32 field to the output

func (*PrettyLogBuilder) Int64

func (plb *PrettyLogBuilder) Int64(key string, val int64) LogBuilder

Int64 adds an int64 field to the output

func (*PrettyLogBuilder) Int8

func (plb *PrettyLogBuilder) Int8(key string, val int8) LogBuilder

Int8 adds an int8 field to the output

func (*PrettyLogBuilder) Send

func (plb *PrettyLogBuilder) Send()

Send sends the event to the output.

After calling send, do not use the event again.

func (*PrettyLogBuilder) Str

func (plb *PrettyLogBuilder) Str(key, val string) LogBuilder

Str adds a string as a field to the output

func (*PrettyLogBuilder) Stringer

func (plb *PrettyLogBuilder) Stringer(key string, s fmt.Stringer) LogBuilder

Stringer calls the String method of an fmt.Stringer and adds the resulting string as a field to the output

func (*PrettyLogBuilder) Timestamp

func (plb *PrettyLogBuilder) Timestamp() LogBuilder

Timestamp adds the time formatted as RFC3339Nano as a field to the output

func (*PrettyLogBuilder) Uint

func (plb *PrettyLogBuilder) Uint(key string, val uint) LogBuilder

Uint adds a uint field to the output

func (*PrettyLogBuilder) Uint16

func (plb *PrettyLogBuilder) Uint16(key string, val uint16) LogBuilder

Uint16 adds a uint16 field to the output

func (*PrettyLogBuilder) Uint32

func (plb *PrettyLogBuilder) Uint32(key string, val uint32) LogBuilder

Uint32 adds a uint32 field to the output

func (*PrettyLogBuilder) Uint64

func (plb *PrettyLogBuilder) Uint64(key string, val uint64) LogBuilder

Uint64 adds a uint64 field to the output

func (*PrettyLogBuilder) Uint8

func (plb *PrettyLogBuilder) Uint8(key string, val uint8) LogBuilder

Uint8 adds a uint8 field to the output

type PrettyLogger

type PrettyLogger struct {
	Out   io.Writer
	Level LogLevel

	TimeFormat string

	UseColor bool

	TimeColor color.Color
	MsgColor  color.Color
	KeyColor  color.Color

	DebugColor color.Color
	InfoColor  color.Color
	WarnColor  color.Color
	ErrColor   color.Color
	FatalColor color.Color
	PanicColor color.Color
	// contains filtered or unexported fields
}

PrettyLogger implements the Logger interface using human-readable output for log messages.

func NewPretty

func NewPretty(out io.Writer) *PrettyLogger

NewPretty creates and returns a new PrettyLogger. If the input writer is io.Discard, NopLogger will be returned.

func (*PrettyLogger) Debug

func (pl *PrettyLogger) Debug(msg string) LogBuilder

Debug creates a new debug event with the given message

func (*PrettyLogger) Debugf

func (pl *PrettyLogger) Debugf(format string, v ...any) LogBuilder

Debugf creates a new debug event with the formatted message

func (*PrettyLogger) Error

func (pl *PrettyLogger) Error(msg string) LogBuilder

Error creates a new error event with the given message

func (*PrettyLogger) Errorf

func (pl *PrettyLogger) Errorf(format string, v ...any) LogBuilder

Errorf creates a new error event with the formatted message

func (*PrettyLogger) Fatal

func (pl *PrettyLogger) Fatal(msg string) LogBuilder

Fatal creates a new fatal event with the given message

When sent, fatal events will cause a call to os.Exit(1)

func (*PrettyLogger) Fatalf

func (pl *PrettyLogger) Fatalf(format string, v ...any) LogBuilder

Fatalf creates a new fatal event with the formatted message

When sent, fatal events will cause a call to os.Exit(1)

func (*PrettyLogger) Info

func (pl *PrettyLogger) Info(msg string) LogBuilder

Info creates a new info event with the given message

func (*PrettyLogger) Infof

func (pl *PrettyLogger) Infof(format string, v ...any) LogBuilder

Infof creates a new info event with the formatted message

func (*PrettyLogger) NoExit

func (pl *PrettyLogger) NoExit()

NoExit prevents the logger from exiting on fatal events

func (*PrettyLogger) NoPanic

func (pl *PrettyLogger) NoPanic()

NoPanic prevents the logger from panicking on panic events

func (*PrettyLogger) Panic

func (pl *PrettyLogger) Panic(msg string) LogBuilder

Panic creates a new panic event with the given message

When sent, panic events will cause a panic

func (*PrettyLogger) Panicf

func (pl *PrettyLogger) Panicf(format string, v ...any) LogBuilder

Panicf creates a new panic event with the formatted message

When sent, panic events will cause a panic

func (*PrettyLogger) SetLevel

func (pl *PrettyLogger) SetLevel(l LogLevel)

SetLevel sets the log level of the logger

func (*PrettyLogger) Warn

func (pl *PrettyLogger) Warn(msg string) LogBuilder

Warn creates a new warn event with the given message

func (*PrettyLogger) Warnf

func (pl *PrettyLogger) Warnf(format string, v ...any) LogBuilder

Warnf creates a new warn event with the formatted message

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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