logger

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2024 License: Apache-2.0 Imports: 10 Imported by: 7

README

go-logger

Go Reference

This is a fork of the logger from gmkit.

A structured, leveled logger. Output is logfmt by default.

Usage

Minimal configuration
l := logger.New()

l.Info("some message", "anotherkey", "another value")

// Output would resemble:
// ts=2023-04-21T16:09:00.026753Z caller=github.com/jasonhancock/go-logger_test/example_test.go:43 src=go-logger.test level=info msg="some message" anotherkey="another value"
Customized config
l := logger.New(
	logger.WithDestination(os.Stdout),
	logger.WithName("myapp"),
	logger.WithLevel("info"),
	logger.WithFormat(logger.FormatLogFmt),
	logger.With("somekey", "someval"),
)

l.Info("some message", "anotherkey", "another value")

// Output would resemble:
// ts=2023-04-13T17:38:13.516398Z caller=github.com/jasonhancock/go-logger_test/example_test.go:11 somekey=someval src=myapp level=info msg="some message" anotherkey="another value"
JSON
l := logger.New(
	logger.WithFormat(logger.FormatJSON),
	logger.WithName("myapp"),
)

l.Info("some message", "anotherkey", "another value")

// Output would resemble:
// {"anotherkey":"another value","caller":"github.com/jasonhancock/go-logger_test/example_test.go:32","level":"info","msg":"some message","src":"myapp","ts":"2023-04-21T16:08:24.224597Z"}
Using the default logger
l := logger.Default()
l.Info("some message")

// Output would resemble:
// ts=2023-04-21T16:09:28.653472Z caller=github.com/jasonhancock/go-logger_test/example_test.go:51 src=default level=info msg="some message"

Documentation

Index

Examples

Constants

View Source
const (
	FormatLogFmt = "logfmt"
	FormatJSON   = "json"
)

Constants defining various output formats.

View Source
const (
	LevelAll   = slog.Level(-10)
	LevelFatal = slog.Level(12)
)

Variables

View Source
var AvailableFormats = []string{
	FormatLogFmt,
	FormatJSON,
}

AvailableFormats lists the available format types.

Functions

func ParseLevel

func ParseLevel(s string) slog.Leveler

ParseLevel parses the string into a Level.

Types

type L

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

L is the logger implementation

func Default

func Default() *L

Default returns a default logger implementation

Example
l := logger.Default()
l.Info("some message")

// Output would resemble:
// ts=2023-04-21T16:09:28.653472Z caller=github.com/jasonhancock/go-logger_test/example_test.go:51 src=default level=info msg="some message"
Output:

func New

func New(opts ...Option) *L

New initializes a new logger. If w is nil, logs will be sent to stdout.

Example (Customized)
l := logger.New(
	logger.WithDestination(os.Stdout),
	logger.WithName("myapp"),
	logger.WithLevel("info"),
	logger.WithFormat(logger.FormatLogFmt),
	logger.With("somekey", "someval"),
)

l.Info("some message", "anotherkey", "another value")

// Output would resemble:
// ts=2023-04-13T17:38:13.516398Z caller=github.com/jasonhancock/go-logger_test/example_test.go:11 somekey=someval src=myapp level=info msg="some message" anotherkey="another value"
Output:

Example (Json)
l := logger.New(
	logger.WithFormat(logger.FormatJSON),
	logger.WithName("myapp"),
)

l.Info("some message", "anotherkey", "another value")

// Output would resemble:
// {"anotherkey":"another value","caller":"github.com/jasonhancock/go-logger_test/example_test.go:32","level":"info","msg":"some message","src":"myapp","ts":"2023-04-21T16:08:24.224597Z"}
Output:

Example (Minimum)
l := logger.New()

l.Info("some message", "anotherkey", "another value")

// Output would resemble:
// ts=2023-04-21T16:09:00.026753Z caller=github.com/jasonhancock/go-logger_test/example_test.go:43 src=go-logger.test level=info msg="some message" anotherkey="another value"
Output:

func Silence

func Silence() *L

Silence returns a logger that writes everything to /dev/null. Useful for silencing log output from tests

func (*L) Debug

func (l *L) Debug(msg any, keyvals ...any)

Debug logs a message at the debug level

func (*L) Err

func (l *L) Err(msg any, keyvals ...any)

Err logs a message at the error level

func (*L) Fatal

func (l *L) Fatal(msg any, keyvals ...any)

Fatal logs a message at the fatal level and also exits the program by calling os.Exit

func (*L) Info

func (l *L) Info(msg any, keyvals ...any)

Info logs a message at the info level

func (*L) LogError

func (l *L) LogError(msg string, err error)

LogError logs an error. It automatically unwinds multi-errors (not recursively...yet).

func (*L) New

func (l *L) New(name string) *L

New returns a sub-logger with the name appended to the existing logger's source

func (*L) Warn

func (l *L) Warn(msg any, keyvals ...any)

Warn logs a message at the warning level

func (*L) With

func (l *L) With(keyvals ...any) *L

With returns a logger with the keyvals appended to the existing logger

type Option added in v0.0.2

type Option func(*options)

Option is used to customize the logger.

func With added in v0.0.2

func With(keyvals ...interface{}) Option

With adds key value pairs to the logger.

func WithCaller added in v0.0.3

func WithCaller(showCaller bool) Option

WithCaller sets whether or not to include the source file and line number of where the message originated.

func WithDestination added in v0.0.2

func WithDestination(w io.Writer) Option

WithDestination sets the target for where the output of the logger should be written.

func WithFormat added in v0.0.2

func WithFormat(format string) Option

WithFormat sets the format to log in.

func WithLevel added in v0.0.2

func WithLevel(level string) Option

WithLevel sets the logging level of the logger.

func WithName added in v0.0.2

func WithName(name string) Option

WithName specifies the name of the application. If not specified, the current processes name will be used.

func WithTimeLocation added in v0.0.3

func WithTimeLocation(loc *time.Location) Option

WithTimeLocation specifies the locale to log the time in.

type TimeFormatterFunc added in v0.0.3

type TimeFormatterFunc func(time.Time) string

Jump to

Keyboard shortcuts

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