logs

package
v0.14.5 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2024 License: MIT Imports: 9 Imported by: 12

README

logs

A package that provides enriched logs:

  • Level support
  • JSON format
  • Customizable JSON encoder
  • Customizable output

Install

go get -u github.com/aukilabs/go-tooling/pkg/logs

Usage

Info
logs.New().Info("hi")
logs.WithTag("to", "ted").Info("hi")
Error
logs.Error(errors.New("an error"))

logs.WithTag("type", "simulation").
	Error(errors.New("an error"))

Note that errors created with errors package enriches logs by default.

Debug
logs.New().Warn("hi")
logs.WithTag("to", "ted").Warn("hi")
Warning
logs.New().Warn("hi")
logs.WithTag("to", "ted").Warn("hi")
Enrich With Tags
logs.WithTag("method", "GET").
    WithTag("path", "/cookies").
    Info("http request")
Set A Custom Logger
logs.SetLogger(func(e Entry) {
    fmt.Println(e) // or whatever you want to use.
})
Set A Custom JSON Encoder
logs.Encoder = json.Marshal // or whathever JSON encoder you want to use.

Documentation

Index

Constants

View Source
const (
	DebugLevel Level = iota
	InfoLevel
	WarningLevel
	ErrorLevel

	AppKeyTag        = "app_key"
	SessionIDTag     = "session_id"
	ParticipantIDTag = "participant_id"
	ClientIDTag      = "client_id"
)

Available log levels.

Variables

View Source
var (
	// The function to encode log entries and their tags.
	Encoder func(any) ([]byte, error)
)

Functions

func Debug

func Debug(arg ...any)

Logs with debug severity

func Debugf

func Debugf(format string, arg ...any)

Logs with debug severity

func Error

func Error(err error)

Logs an error.

func Fatal

func Fatal(err error)

Logs with error severity and exit with status code 1

func Info

func Info(arg ...any)

Logs with info severity

func Infof

func Infof(format string, arg ...any)

Logs with info severity

func Panic

func Panic(err error)

Logs with error severity and panic

func SetIndentEncoder

func SetIndentEncoder()

SetIndentEncoder is a helper function that set the error encoder to a function that uses json.MarshalIndent.

func SetInlineEncoder

func SetInlineEncoder()

SetInlineEncoder is a helper function that set the error encoder to json.Marshal.

func SetLevel

func SetLevel(v Level)

Sets what log levels are logged. Levels under the given level are ignored.

func SetLogger

func SetLogger(l func(Entry))

Set the function that logs an entry.

func Warn

func Warn(arg ...any)

Logs with warn severity

func Warnf

func Warnf(format string, arg ...any)

Logs with warn severity

Types

type Entry

type Entry interface {
	// Return the time when the entry was created.
	Time() time.Time

	// Returns the log level.
	Level() Level

	// Sets the tag key with the given value. The value is converted to a
	// string.
	WithTag(k string, v any) Entry

	// Set Client ID tag
	WithClientID(v string) Entry

	// Set opentelemetry context tags
	WithOtelCtx(ctx context.Context) Entry

	// Returns the log tags.
	Tags() map[string]any

	// Logs the given values with debug level.
	Debug(v ...any)

	// Logs the velues with the given format on with debug level.
	Debugf(format string, v ...any)

	// Logs the given values with info level.
	Info(v ...any)

	// Logs the velues with the given format on with info level.
	Infof(format string, v ...any)

	// Logs the given values with warning level.
	Warn(v ...any)

	// Logs the velues with the given format on with warning level.
	Warnf(format string, v ...any)

	// Logs the given values with error level.
	Error(error)

	// Logs error on error level and exit with status 1.
	Fatal(err error)

	// Logs error on error level and panic
	Panic(err error)

	// Returns the error used to create the entry.
	GetError() error

	// Return the entry as a string.
	String() string
}

func New

func New() Entry

Creates a log entry.

func WithClientID

func WithClientID(v string) Entry

Creates a log entry with client id

func WithOtelCtx

func WithOtelCtx(ctx context.Context) Entry

Creates a log entry with opentelemetry context tags

func WithTag

func WithTag(k string, v any) Entry

Creates a log entry and set the given tag.

type Level

type Level int

A log level.

func ParseLevel

func ParseLevel(v string) Level

Parses a level from a string.

func (Level) String

func (l Level) String() string

Converts a level to a string.

Jump to

Keyboard shortcuts

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