slogs

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2026 License: Apache-2.0 Imports: 8 Imported by: 8

README

slogs

This is a wrapper around the stdlib Golang slog package that just creates a slog logger in some standard way, with a configurable log level.

Slog was introduced in Go 1.21, so that is the minimum required Go version to use this module.

Usage

package main

import "github.com/chia-network/go-modules/pkg/slogs"

func main() {
	// Init the logger with a log-level string (debug, info, warn, error)
	// defaults to "info" if empty or unsupported string 
	// not passing any logger options
	slogs.Init("info")

	// Logs a hello world message at the info level
	slogs.Logr.Info("hello world")

	// Logs an error message at the error level 
	slogs.Logr.Error("we received an error")
}

In a Cobra/Viper CLI app this might look more like:

package cmd

import (
	"log"
	
	"github.com/spf13/cobra"
	"github.com/spf13/viper"
	"github.com/chia-network/go-modules/pkg/slogs"
)

var rootCmd = &cobra.Command{
	Use:   "cmd",
	Short: "Short help message for cmd",

	Run: func(cmd *cobra.Command, args []string) {
		// Init logger
		slogs.Init(viper.GetString("log-level"))

		// Application logic below
	},
}

func Execute() {
	cobra.CheckErr(rootCmd.Execute())
}

func init() {
	rootCmd.PersistentFlags().String("log-level", "info", "The log-level for the application, can be one of info, warn, error, debug.")
	err := viper.BindPFlag("log-level", rootCmd.PersistentFlags().Lookup("log-level"))
	if err != nil {
		log.Fatalln(err.Error()) // Have to log with standard logger until the slog logger is initialized
	}
}
Logger Options
Source contexts

Source context tells you the position in your code that created a log line. This is disabled by default, but can be enabled when initializing this package as follows:

slogs.Init("info", slogs.WithSourceContext(true))
Writer

The default logger's writer sends logs to stdout. you can change this when initializing this package as follows:

slogs.Init("info", slogs.WithWriter(io.Discard))

This example uses io.Discard which sends logs to /dev/null but any other interface that implements the standard Write method will work, such as os.Stderr.

JSON output

The default logger outputs logs in text format. To output logs in JSON format instead, enable it when initializing:

slogs.Init("info", slogs.WithJSONOutput())

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Init

func Init(level string, options ...ClientOptionFunc)

Init custom init function that accepts the log level for the application and initializes a stdout slog logger

Types

type ClientOptionFunc added in v1.0.0

type ClientOptionFunc func(*loggerOptions)

ClientOptionFunc can be used to customize a new slogs client

func WithJSONOutput added in v1.0.0

func WithJSONOutput() ClientOptionFunc

WithJSONOutput enables JSON formatted log output instead of the default text format

func WithSourceContext added in v1.0.0

func WithSourceContext(set bool) ClientOptionFunc

WithSourceContext sets the AddSource option for the slogs logger, which adds the location in the code that called the logger, to each logline

func WithWriter added in v1.0.0

func WithWriter(w io.Writer) ClientOptionFunc

WithWriter sets a given io.Writer to be the receiver for slogs logs

type Logger

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

Logger is a wrapper around a slog log handler

var Logr Logger

Logr is a custom text logger from the stdlib slog package

func (Logger) Debug added in v1.0.0

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

Debug uses the initialized logger at Debug level

func (Logger) DebugContext added in v1.0.0

func (l Logger) DebugContext(ctx context.Context, msg string, args ...any)

DebugContext uses the initialized logger at Debug level with a given context

func (Logger) Error added in v1.0.0

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

Error uses the initialized logger at Error level

func (Logger) ErrorContext added in v1.0.0

func (l Logger) ErrorContext(ctx context.Context, msg string, args ...any)

ErrorContext uses the initialized logger at Error level with a given context

func (Logger) Fatal

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

Fatal uses the initialized logger at Error level, and exits 1

func (Logger) FatalContext added in v1.0.0

func (l Logger) FatalContext(ctx context.Context, msg string, args ...any)

FatalContext uses the initialized logger at Error level with a given context, and exits 1

func (Logger) Info added in v1.0.0

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

Info uses the initialized logger at Info level

func (Logger) InfoContext added in v1.0.0

func (l Logger) InfoContext(ctx context.Context, msg string, args ...any)

InfoContext uses the initialized logger at Info level with a given context

func (Logger) Warn added in v1.0.0

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

Warn uses the initialized logger at Warn level

func (Logger) WarnContext added in v1.0.0

func (l Logger) WarnContext(ctx context.Context, msg string, args ...any)

WarnContext uses the initialized logger at Warn level with a given context

Jump to

Keyboard shortcuts

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