Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // This error is returned when an invalid [LoggerType] is provided to // [NewLogger] via [WithLoggerType] ErrUnhandledLoggerType = errors.New("unhandled logger type") )
Functions ¶
func NewLogger ¶
NewLogger creates a new slog.Logger with the provided slog.Leveler. By default the slog.Logger outputs to os.Stderr and will output using either slog.NewTextHandler or a SystemdHandler.
Types ¶
type LoggerOption ¶
type LoggerOption func(*logger)
LoggerOption allows passing options to NewLogger
func WithLoggerType ¶
func WithLoggerType(lt LoggerType) LoggerOption
WithLoggerType allows overriding the default LoggerType of LoggerTypeAuto
func WithWriter ¶
func WithWriter(w io.Writer) LoggerOption
WithWriter accepts an io.Writer in order to override the default of sending logs to os.Stderr
type LoggerType ¶
type LoggerType string
The type of log handler to use
const ( LoggerTypeAuto LoggerType = "auto" LoggerTypeDiscard LoggerType = "discard" LoggerTypeJson LoggerType = "json" LoggerTypeSystemd LoggerType = "systemd" LoggerTypeText LoggerType = "text" )
String constants for each log handler type
type LoggerTypeVar ¶
type LoggerTypeVar struct {
// contains filtered or unexported fields
}
This type satisfies the github.com/spf13/pflag.Value interface so LoggerType can be set via a command line flag
Example ¶
package main
import (
"log/slog"
"os"
"github.com/andrewheberle/slogger"
"github.com/spf13/pflag"
)
func main() {
lt := new(slogger.LoggerTypeVar)
// set up a flagset
f := pflag.NewFlagSet("example", pflag.ContinueOnError)
f.Var(lt, "type", "Logger type (auto, discard, json, systemd or text)")
// parse as though option provided was systemd
f.Parse([]string{"--type", "systemd"})
// set up our logLevel variable to allow changing levels
logLevel := new(slog.LevelVar)
// set up the logger based on the command line flag
logger, err := slogger.NewLogger(logLevel,
lt.LoggerTypeOption(),
slogger.WithWriter(os.Stdout),
)
if err != nil {
panic(err)
}
logger.Info("output using systemd handler")
}
Output: <6> output using systemd handler
func (LoggerTypeVar) LoggerTypeOption ¶ added in v0.2.0
func (lt LoggerTypeVar) LoggerTypeOption() LoggerOption
Returns a LoggerOption suitable for passing to NewLogger
func (*LoggerTypeVar) Set ¶
func (lt *LoggerTypeVar) Set(s string) error
Set is to satisfy the github.com/spf13/pflag.Value interface
func (*LoggerTypeVar) String ¶
func (lt *LoggerTypeVar) String() string
Strig method for LoggerTypeVar
func (LoggerTypeVar) Type ¶
func (lt LoggerTypeVar) Type() string
Type is to satisfy the github.com/spf13/pflag.Value interface
type SystemdHandler ¶
type SystemdHandler struct {
// contains filtered or unexported fields
}
A slog.Handler implementation to output text in a format suitable for sd_journal_stream_fd.
func NewSystemdHandler ¶
func NewSystemdHandler(w io.Writer, opts *slog.HandlerOptions) *SystemdHandler
Creates a new SystemdHandler that can be used by slog.New