sloginit

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

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

Go to latest
Published: May 16, 2024 License: MIT Imports: 7 Imported by: 0

README

sloginit

An opinionated initialisation for the slog package.

Currently works as follows:

  • If output is a TTY will use github.com/lmittmann/tint for logging
  • If output is not it will use JSON logging

Settings can be overridden by environment variables:

  • LOG_LEVEL - the level to log at
  • LOG_JSON - force JSON logger if set to 1/ON/YES/TRUE, otherwise use console logger

Supports auto initialisation of the slog default logger:

package main

import (
	"log/slog"

	_ "github.com/witchard/sloginit/auto"
)

func main() {
	slog.Debug("debug")
	slog.Info("info")
	slog.Warn("warn")
	slog.Error("error")
}

Or can be initialised directly so you can provide setup options:

package main

import (
	"log/slog"

	"github.com/witchard/sloginit"
)

func main() {
	sloginit.SetDefault(sloginit.JSON)
	slog.Info("hi", "format", "json")
}

Documentation

Overview

sloginit provides an opinionated simple initialisation for the slog package.

If the output is a TTY then you'll get colourful logs, otherwise JSON. This can be overridden with the LOG_JSON=on environment variable. Log level can be overridden with the LOG_LEVEL environment variable. You can also force these options by providing relevant SlogOpt when calling Logger().

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Console

func Console(o *opts)

Console is a SlogOpt that forces output to the terminal.

func JSON

func JSON(o *opts)

JSON is a SlogOpt that forces output as JSON.

func Logger

func Logger(options ...SlogOpt) *slog.Logger

Logger will create an slog.Logger using the environment and options provided.

func SetDefault

func SetDefault(options ...SlogOpt)

Default will set the slog default logger to the result of passing options to Logger.

Types

type SlogOpt

type SlogOpt func(*opts)

SlogOpt provides functional options to configure the logger.

func EnvPrefix

func EnvPrefix(prefix string) SlogOpt

EnvPrefix is a SlogOpt that sets the env prefix for reading environment options.

func Level

func Level(l slog.Level) SlogOpt

Level is a SlogOpt to set the log level.

Directories

Path Synopsis
auto initialises the default log/slog using sloginit defaults.
auto initialises the default log/slog using sloginit defaults.

Jump to

Keyboard shortcuts

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