sloggokit

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2026 License: Apache-2.0 Imports: 9 Imported by: 4

README

license Go Report Card golangci-lint Latest Release

Go slog-gokit Adapter

This library provides a custom slog.Handler that wraps a go-kit Logger, so that loggers created via slog.New() chain their log calls to the internal go-kit Logger.

Install

go get github.com/tjhop/slog-gokit

Documentation

Documentation can be found here:

https://pkg.go.dev/github.com/tjhop/slog-gokit

Example

package main

import (
	"log/slog"
	"os"

	"github.com/go-kit/log"
	slgk "github.com/tjhop/slog-gokit"
)

func main() {
	// Take an existing go-kit/log Logger:
	gklogger := log.NewLogfmtLogger(os.Stderr)

	// Create an slog Logger that chains log calls to the go-kit/log Logger:
	slogger := slog.New(slgk.NewGoKitHandler(gklogger, nil))
	slogger.WithGroup("example_group").With("foo", "bar").Info("hello world")

	// The slog Logger produces logs at slog.LevelInfo by default.
	// Optionally create an slog.Leveler to dynamically adjust the level of
	// the slog Logger.
	lvl := &slog.LevelVar{}
	lvl.Set(slog.LevelDebug)
	slogger = slog.New(slgk.NewGoKitHandler(gklogger, lvl))
	slogger.WithGroup("example_group").With("foo", "bar").Info("hello world")
}

Development

Contributions are welcome! Commits should follow conventional commits syntax.

Required Software/Tools
  • Working Go environment
  • Docker
  • GNU Make

Documentation

Overview

Example (Basic)
package main

import (
	"log/slog"
	"os"

	"github.com/go-kit/log"
	slgk "github.com/tjhop/slog-gokit"
)

func main() {
	// Take an existing go-kit/log Logger:
	gklogger := log.NewLogfmtLogger(os.Stderr)

	// Create an slog Logger that chains log calls to the go-kit/log Logger:
	slogger := slog.New(slgk.NewGoKitHandler(gklogger, nil))
	slogger.WithGroup("example_group").With("foo", "bar").Info("hello world")
}
Example (ConstantLevel)
package main

import (
	"log/slog"
	"os"

	"github.com/go-kit/log"
	slgk "github.com/tjhop/slog-gokit"
)

func main() {
	// Take an existing go-kit/log Logger:
	gklogger := log.NewLogfmtLogger(os.Stderr)

	// Create an slog Logger that chains log calls to the go-kit/log Logger.
	//
	// The level for the slog Logger can be set explicitly with anything
	// that satisfies the `slog.Leveler` interface, such as slog's level
	// constants:
	slogger := slog.New(slgk.NewGoKitHandler(gklogger, slog.LevelDebug))
	slogger.WithGroup("example_group").With("foo", "bar").Info("hello world")
}
Example (DynamicLevel)
package main

import (
	"log/slog"
	"os"

	"github.com/go-kit/log"
	slgk "github.com/tjhop/slog-gokit"
)

func main() {
	// Take an existing go-kit/log Logger:
	gklogger := log.NewLogfmtLogger(os.Stderr)

	// Create an slog Logger that chains log calls to the go-kit/log Logger.
	//
	// To dynamically change the logger's level, create an `slog.LevelVar`
	// and use it to set the logger's level as needed:
	lvl := &slog.LevelVar{} // Info level by default
	slogger := slog.New(slgk.NewGoKitHandler(gklogger, lvl))
	slogger.WithGroup("example_group").With("foo", "bar").Info("hello world")

	// Change level to debug, etc:
	lvl.Set(slog.LevelDebug)
	slogger.WithGroup("example_group").With("foo", "bar").Debug("helpful debug info")
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewGoKitHandler

func NewGoKitHandler(logger log.Logger, level slog.Leveler) slog.Handler

NewGoKitHandler returns a new slog logger from the provided go-kit logger. Calls to the slog logger are chained to the handler's internal go-kit logger. If provided a level, it will be used to filter log events in the handler's Enabled() method.

The handler adds a `caller` key to each record, resolved from the program counter that slog captured at the log call site. Records handled directly (without going through an slog.Logger) have no PC set, and thus omit the caller.

Types

type GoKitHandler

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

GoKitHandler implements the slog.Handler interface. It holds an internal go-kit logger that is used to perform the true logging.

func (*GoKitHandler) Enabled

func (h *GoKitHandler) Enabled(_ context.Context, level slog.Level) bool

Enabled returns true if the internal slog.Leveler is enabled for the provided log level. It implements slog.Handler.

func (*GoKitHandler) Handle

func (h *GoKitHandler) Handle(_ context.Context, record slog.Record) error

Handler take an slog.Record created by an slog.Logger and dispatches the log call to the internal go-kit logger. Groups and attributes created by slog are formatted and added to the log call as individual key/value pairs. It implements slog.Handler.

func (*GoKitHandler) WithAttrs

func (h *GoKitHandler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs formats the provided attributes and caches them in the handler to attach to all future log calls. It implements slog.Handler.

func (*GoKitHandler) WithGroup

func (h *GoKitHandler) WithGroup(name string) slog.Handler

WithGroup sets the group prefix string and caches it within the handler to use to prefix all future log attribute pairs. It implements slog.Handler.

Jump to

Keyboard shortcuts

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