shclog

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2024 License: MIT Imports: 8 Imported by: 0

README ¶

slog: hclog adapter

Allows to use slog as an implementation of hclog. Useful, when using hashicorp libraries, for example raft, or other projects, adopted hclog interface as their default logger.

go get github.com/ValerySidorin/shclog

💡 Usage

Hashicorp raft custom logging
import (
	"log/slog"
	"net"
	"os"
	"time"

	"github.com/ValerySidorin/shclog"
	"github.com/hashicorp/raft"
	"github.com/lmittmann/tint"
)

raftCfg := raft.DefaultConfig()
raftCfg.LocalID = raft.ServerID("node_1")

w := os.Stderr
slogger := slog.New(tint.NewHandler(w, &tint.Options{
	AddSource: true,
	Level:     shclog.SlogLevelTrace,
}))
l := shclog.New(slogger)

// Use shclog here
raftCfg.Logger = l

addr, err := net.ResolveTCPAddr("tcp", "localhost:9876")
if err != nil {
	l.Error(err.Error())
	os.Exit(1)
}

transport, err := raft.NewTCPTransport("localhost:9876", addr, 3, 10*time.Second, os.Stderr)
if err != nil {
	l.Error(err.Error())
	os.Exit(1)
}

_, err = raft.NewRaft(raftCfg, nil, // Pass your fsm implementation here
	raft.NewInmemStore(), raft.NewInmemStore(), raft.NewDiscardSnapshotStore(),
	transport)
if err != nil {
	l.Error(err.Error())
	os.Exit(1)
}
Default logging
import (
	"fmt"
	"log/slog"
	"os"

	"github.com/ValerySidorin/shclog"
	"github.com/hashicorp/go-hclog"
)

level := shclog.SlogLevelTrace
slogger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
	AddSource: true,
	Level:     level,
}))
l := shclog.New(slogger)

l.Log(hclog.Error, "error log", "key", "value")
l.Log(hclog.Off, "no log")
l.Info("info log")
l.Trace("debug log")

fmt.Println("Log level:", l.GetLevel())
fmt.Println("Log is trace:", l.IsTrace())
fmt.Println("Log is debug:", l.IsDebug())
fmt.Println("Log name:", l.Name())

l = l.Named("name_1")
fmt.Println("Log name:", l.Name())

l = l.With("key", "value")
fmt.Println("Implied args:", l.ImpliedArgs())

l = l.Named("name_2")
fmt.Println("Log name:", l.Name())
l = l.ResetNamed("name")
fmt.Println("Log name:", l.Name())

l.SetLevel(hclog.Debug)
fmt.Println("Level can not be set through shclog. Instead set it through slog handler. Log level:", l.GetLevel())

stdLog := l.StandardLogger(&hclog.StandardLoggerOptions{})
stdLog.Println("log from std logger")

w := l.StandardWriter(&hclog.StandardLoggerOptions{})
w.Write([]byte("log from std writer"))

Documentation ¶

Overview ¶

This code was kindly taken from go-hclog src

Index ¶

Constants ¶

View Source
const SlogLevelTrace = slog.LevelDebug - 4

Slog does not have built in trace log level

Variables ¶

This section is empty.

Functions ¶

func New ¶

func New(l *slog.Logger) hclog.Logger

Types ¶

type Shclog ¶

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

func (*Shclog) Debug ¶

func (l *Shclog) Debug(msg string, args ...interface{})

func (*Shclog) Error ¶

func (l *Shclog) Error(msg string, args ...interface{})

func (*Shclog) GetLevel ¶

func (l *Shclog) GetLevel() hclog.Level

func (*Shclog) ImpliedArgs ¶

func (l *Shclog) ImpliedArgs() []interface{}

func (*Shclog) Info ¶

func (l *Shclog) Info(msg string, args ...interface{})

func (*Shclog) IsDebug ¶

func (l *Shclog) IsDebug() bool

func (*Shclog) IsError ¶

func (l *Shclog) IsError() bool

func (*Shclog) IsInfo ¶

func (l *Shclog) IsInfo() bool

func (*Shclog) IsTrace ¶

func (l *Shclog) IsTrace() bool

func (*Shclog) IsWarn ¶

func (l *Shclog) IsWarn() bool

func (*Shclog) Log ¶

func (l *Shclog) Log(level hclog.Level, msg string, args ...interface{})

func (*Shclog) Name ¶

func (l *Shclog) Name() string

func (*Shclog) Named ¶

func (l *Shclog) Named(name string) hclog.Logger

func (*Shclog) ResetNamed ¶

func (l *Shclog) ResetNamed(name string) hclog.Logger

func (*Shclog) SetLevel ¶

func (l *Shclog) SetLevel(level hclog.Level)

func (*Shclog) StandardLogger ¶

func (l *Shclog) StandardLogger(opts *hclog.StandardLoggerOptions) *log.Logger

func (*Shclog) StandardWriter ¶

func (l *Shclog) StandardWriter(opts *hclog.StandardLoggerOptions) io.Writer

func (*Shclog) Trace ¶

func (l *Shclog) Trace(msg string, args ...interface{})

func (*Shclog) Warn ¶

func (l *Shclog) Warn(msg string, args ...interface{})

func (*Shclog) With ¶

func (l *Shclog) With(args ...interface{}) hclog.Logger

Jump to

Keyboard shortcuts

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