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")
}
Output:
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")
}
Output:
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")
}
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewGoKitHandler ¶
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 ¶
Enabled returns true if the internal slog.Leveler is enabled for the provided log level. It implements slog.Handler.
func (*GoKitHandler) Handle ¶
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.