slog

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2023 License: MIT Imports: 6 Imported by: 2

README

slog

The slog library is a thin wrapper around Go's slog. Here we add some extra functionality that a more general/standard library won't do. Things like tracing and config loading from env vars.

Documentation

Overview

Package slog provides structured logging for our Go services It is a wrapper for the https://pkg.golang.ir/golang.org/x/exp/slog With some extra functionality to configure levels and formatters.

Index

Examples

Constants

View Source
const (
	FormatText   = "text"
	FormatGcloud = "gcloud"
)

All available log formats

View Source
const (
	DefaultLevel  = slog.LevelInfo
	DefaultFormat = FormatGcloud
)

Default configurations

Variables

This section is empty.

Functions

func Configure

func Configure(cfg Config) error

Configure will change the default logger configuration. It should be called as soon as possible, usually on the main of your program.

func Debug

func Debug(msg string, args ...any)

Debug calls Logger.Debug on the default logger.

func DebugCtx

func DebugCtx(ctx context.Context, msg string, args ...any)

DebugCtx calls Logger.DebugCtx on the default logger.

func Error

func Error(msg string, args ...any)

Error calls Logger.Error on the default logger.

func ErrorCtx

func ErrorCtx(ctx context.Context, msg string, args ...any)

ErrorCtx calls Logger.ErrorCtx on the default logger.

func Fatal added in v0.0.5

func Fatal(msg string, args ...any)

Fatal is equivalent to Error() followed by a call to os.Exit(1).

func FatalCtx added in v0.0.5

func FatalCtx(ctx context.Context, msg string, args ...any)

FatalCtx is equivalent to ErrorCtx() followed by a call to os.Exit(1).

func FromCtx added in v0.0.2

func FromCtx(ctx context.Context) *slog.Logger

FromCtx gets the Logger associated with the given context. A default Logger is returned if the context has no Logger associated with it.

func Info

func Info(msg string, args ...any)

Info calls Logger.Info on the default logger.

func InfoCtx

func InfoCtx(ctx context.Context, msg string, args ...any)

InfoCtx calls Logger.InfoCtx on the default logger.

func NewContext added in v0.0.2

func NewContext(ctx context.Context, log *slog.Logger) context.Context

NewContext creates a new context.Context with the given Logger associated with it. Call FromCtx to retrieve the Logger.

func Warn

func Warn(msg string, args ...any)

Warn calls Logger.Warn on the default logger.

func WarnCtx

func WarnCtx(ctx context.Context, msg string, args ...any)

WarnCtx calls Logger.WarnCtx on the default logger.

Types

type Config

type Config struct {
	Level  Level
	Format Format
}

Config represents log configuration.

func LoadConfig

func LoadConfig(service string) (Config, error)

LoadConfig will load the log Config of the service from environment variables. The service name is used as a prefix for the environment variables. So a service "TEST" will load the log level from "TEST_LOG_LEVEL".

Available log levels are: "debug", "info", "warn", "error" Available log fmts are: "gcloud", "text"

If the environment variables are not found it will use default values.

Example
package main

import (
	"github.com/birdie-ai/golibs/slog"
)

func main() {
	_ = slog.Configure(slog.Config{
		Level:  slog.LevelDebug,
		Format: slog.FormatText,
	})

	slog.Info("info msg", "key", "val", "key2", 666)

	_ = slog.Configure(slog.Config{
		Level:  slog.LevelDebug,
		Format: slog.FormatGcloud,
	})

	slog.Debug("debug msg", "key", "val", "key2", 666)
}
Output:

type Format

type Format string

Format determines the output format of the log records

func ParseFormat added in v0.0.4

func ParseFormat(format string) (Format, error)

ParseFormat parses the string and returns the corresponding Format.

type Level

type Level = slog.Level

Level determines the importance or severity of a log record

const (
	LevelInfo    Level = slog.LevelInfo
	LevelDebug   Level = slog.LevelDebug
	LevelWarn    Level = slog.LevelWarn
	LevelError   Level = slog.LevelError
	LevelDisable Level = math.MaxInt
)

All available log levels

func ParseLevel added in v0.0.4

func ParseLevel(level string) (Level, error)

ParseLevel parses the string and returns the corresponding Level.

type Logger

type Logger = slog.Logger

Logger represents a logger instance with its own context.

func With

func With(args ...any) *Logger

With calls Logger.With on the default logger returning a new Logger instance.

Jump to

Keyboard shortcuts

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