liitu

package module
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2023 License: MIT Imports: 13 Imported by: 0

README

liitu

Go Reference Go Report Card

liitu is a pretty logging library for go with focus on readability. It currently implements formatters for github.com/rs/zerolog and log/slog.

liitu == crayon

From a old finnish proverb:

"Ne piirtää kel on liituu"
Translation: "Those who have cryons/chalk will draw"

liitu/slog

Getting started

go get -u github.com/lime008/liitu

Using with slog
package main

import (
	"os"

	"github.com/lime008/liitu"
	"log/slog"
)

func main() {
	logger = slog.New(liitu.NewSlogHandler(os.Stderr, nil))
	log.Debug().Msg("hi")
	log.Debug().Msg("hello")
}

Using with zerolog
package main

import (
	"os"

	"github.com/lime008/liitu"
	"github.com/rs/zerolog/log"
)

func main() {
	log.Logger = log.Output(liitu.ZerologWriter{Out: os.Stderr})
	log.Debug().Msg("hi")
	log.Debug().Msg("hello")
}

References

Documentation

Index

Examples

Constants

View Source
const (
	LevelTrace   = -8
	LevelDebug   = -4
	LevelInfo    = 0
	LevelWarning = 4
	LevelError   = 8
	LevelFatal   = 12
	LevelPanic   = 15
	LevelUnknown = 16
)

Variables

View Source
var (
	IndentSize = 2
	IndentChar = " "
	Indent     = strings.Repeat(IndentChar, IndentSize)
)
View Source
var (
	DefaultLevel      = slog.LevelInfo
	DefaultTimeFormat = "2006-01-02 15:04:05"
)

Functions

func NewSlogHandler

func NewSlogHandler(w io.Writer, opts *SlogOptions) slog.Handler

NewHandler creates a slog.Handler that writes tinted logs to Writer w, using the default options. If opts is nil, the default options are used.

Example
fmt.Fprintf(os.Stderr, "\n\n\n")
ctx := context.Background()

programLevel := new(slog.LevelVar)

logger := slog.New(
	liitu.NewSlogHandler(
		os.Stderr,
		&liitu.SlogOptions{Level: programLevel},
	),
)

programLevel.Set(liitu.LevelTrace)

logger.Log(ctx, liitu.LevelTrace, "hello")
logger.Debug("world")
logger.Info("foo")
logger.Warn("bar")
logger.Error("baz")
logger.Log(ctx, liitu.LevelFatal, "quz")
logger.Log(ctx, liitu.LevelPanic, "do not panic")
logger.Log(ctx, slog.LevelWarn+2, "custom levels")
logger.Info("printing log message fields",
	slog.String("string", "bar"),
	slog.Int("integer", 14),
	slog.Float64("float", 18.35),
	slog.Bool("boolean", false),
	slog.Any("binary", []byte("Test")),
	slog.Any("raw json message value", json.RawMessage(`{"some": "json"}`)),
	slog.Any("structs", myStruct),
	slog.Any("slices", testSlice),
)
g := logger.WithGroup("group")
g.Info("hello from a group")
g.WithGroup("child").Warn("with nested log group")

logger.Error("field groups", slog.Group("group",
	slog.String("Foo", "bar"),
	slog.Group("child-group",
		slog.String("hello", "world"),
	),
))

loggerWithSource := slog.New(
	liitu.NewSlogHandler(
		os.Stderr,
		&liitu.SlogOptions{Level: programLevel, AddSource: true},
	),
)
loggerWithSource.Info(
	"you can automatically include source file in the logs by setting SlogOptions.AddSource=true",
	slog.String("foo", "bar"),
	slog.String("baz", "quz"),
)

loggerWithCaller := slog.New(
	liitu.NewSlogHandler(
		os.Stderr,
		&liitu.SlogOptions{Level: programLevel, AddSource: true, AddCaller: true},
	),
)
loggerWithCaller.Info(
	"you can also include the caller function with SlogOptions.AddCaller=true",
	slog.String("foo", "bar"),
	slog.String("baz", "quz"),
)
fmt.Fprintf(os.Stderr, "\n\n\n")
Output:

Types

type Level

type Level int

Level is a extended log level definition compatible with slog.Level. It adds TRACE, FATAL and PANIC levels to the level definitions.

type SlogOptions

type SlogOptions struct {
	// Minimum level to log (Default: slog.LevelInfo)
	Level slog.Leveler
	// Disable color (Default: false)
	NoColor bool
	// Enable source code location (Default: false)
	AddSource bool
	// Enable caller function (Default: false)
	AddCaller bool
	// Time format (Default: time.StampMilli)
	TimeFormat string
	// ReplaceAttr is called to rewrite each non-group attribute before it is logged.
	// See https://pkg.go.dev/log/slog#HandlerOptions for details.
	ReplaceAttr func(groups []string, attr slog.Attr) slog.Attr
}

Options for a slog.Handler that writes tinted logs. A zero Options consists entirely of default values.

Options can be used as a drop-in replacement for slog.HandlerOptions.

type ZerologWriter

type ZerologWriter struct {
	Out     io.Writer
	NoColor bool
	// contains filtered or unexported fields
}
Example
package main

import (
	"os"

	"github.com/lime008/liitu"
	"github.com/rs/zerolog"
	"github.com/rs/zerolog/log"
)

type testStruct struct {
	Name   string
	Age    int
	Member bool
	Nested *testStruct
}

var myStruct = &testStruct{
	Name:   "John",
	Age:    32,
	Member: true,
	Nested: &testStruct{
		Name:   "Johnny",
		Age:    3,
		Member: false,
	},
}

func main() {
	log.Logger = log.Output(liitu.ZerologWriter{Out: os.Stderr})
	log.Logger.Level(zerolog.TraceLevel)

	log.Trace().Msg("hello")
	log.Debug().Msg("world")
	log.Info().Msg("foo")
	log.Warn().Msg("bar")
	log.Error().Msg("baz")
	log.WithLevel(zerolog.FatalLevel).Msg("quz")
	log.WithLevel(zerolog.PanicLevel).Msg("do not panic")
	log.Debug().Caller().
		Int("number", 14).
		Bytes("binary", []byte("Test")).
		Bool("boolean", false).
		Str("foo", "bar").
		RawJSON("value", []byte(`{"some": "json"}`)).Msg("printing log fields")
	log.Debug().Msg("hi")
	log.Warn().Msg("WARNING")
	log.Info().Interface("testStruct", myStruct).Msg("hello")

}
Output:

func (ZerologWriter) Write

func (w ZerologWriter) Write(p []byte) (n int, err error)

Jump to

Keyboard shortcuts

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