goslog

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2024 License: MIT Imports: 14 Imported by: 0

README

go-slog

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewLogger

func NewLogger(config *LogConfig) (*slog.Logger, error)

Types

type Entry

type Entry struct {
	Time    time.Time  `json:"time"`
	Level   slog.Level `json:"level"`
	Message string     `json:"msg"`
	Error   string     `json:"error"`
	Raw     json.RawMessage
}

Entry is a convenience struct containing basic information TODO replace the fields with methods to save ram

func (Entry) Get

func (e Entry) Get(path string) gjson.Result

Use Get if you want to get a value from a LogEntry that is not contained within the message. Use the dot notation for that. For more information about dot notation see https://github.com/tidwall/gjson

func (Entry) GetMany

func (e Entry) GetMany(paths ...string) []gjson.Result

GetMany searches json for the multiple paths. The return value is a Result array where the number of items will be equal to the number of input paths. Use the dot notation for that. For more information about dot notation see https://github.com/tidwall/gjson

type Hook

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

Inspired by Logrus Testing Hook, this is my version for slog.

func NewHookLogger

func NewHookLogger(level slog.Level) (Hook, *slog.Logger)

func NewHookLoggerWithOptions

func NewHookLoggerWithOptions(options *slog.HandlerOptions) (Hook, *slog.Logger)

func (Hook) Logs

func (h Hook) Logs() []Entry

func (Hook) Reset

func (h Hook) Reset()

Clear the Hooks buffer, aka delete all Log Entries.

type LogConfig

type LogConfig struct {
	Verbose bool      `short:"v" long:"verbose" env:"VERBOSE" description:"Short hand for log level debug. Will override any setting for level"`
	Level   LogLevel  `` /* 131-byte string literal not displayed */
	Format  LogFormat `short:"f" long:"format" env:"FORMAT" default:"text" choice:"text" choice:"json" choice:"pretty" description:"Log format"`
}

You can use LogConfig like this:

Log goslog.LogConfig `group:"Log Options" namespace:"log" env-namespace:"LOG"`

type LogFormat

type LogFormat int
const (
	FORMAT_PRETTY LogFormat = iota
	FORMAT_TEXT
	FORMAT_JSON
)

func (*LogFormat) MarshalFlag

func (l *LogFormat) MarshalFlag() (string, error)

MarshalFlag converts a CookieDomain to a string

func (*LogFormat) Set

func (l *LogFormat) Set(value string) error

implements flag.Value

func (LogFormat) String

func (l LogFormat) String() string

implements flag.Value

func (*LogFormat) UnmarshalFlag

func (l *LogFormat) UnmarshalFlag(value string) error

UnmarshalFlag converts a string to a CookieDomain

type LogLevel

type LogLevel slog.Level
const (
	LEVEL_DEBUG LogLevel = LogLevel(slog.LevelDebug)
	LEVEL_INFO  LogLevel = LogLevel(slog.LevelInfo)
	LEVEL_WARN  LogLevel = LogLevel(slog.LevelWarn)
	LEVEL_ERROR LogLevel = LogLevel(slog.LevelError)
)

func (*LogLevel) MarshalFlag

func (l *LogLevel) MarshalFlag() (string, error)

MarshalFlag converts a CookieDomain to a string

func (*LogLevel) Set

func (l *LogLevel) Set(value string) error

implements flag.Value

func (LogLevel) String

func (l LogLevel) String() string

implements flag.Value

func (*LogLevel) UnmarshalFlag

func (l *LogLevel) UnmarshalFlag(value string) error

UnmarshalFlag converts a string to a CookieDomain

Jump to

Keyboard shortcuts

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