Documentation
¶
Index ¶
- type ParserConfig
- type Record
- func (d Record) ContainsKey(key string) bool
- func (d Record) ContainsPair(key, value string) bool
- func (d Record) ForEach(fn func(key, value string))
- func (d Record) Get(key string) string
- func (d Record) GetDuration(key string, defaultVal time.Duration) time.Duration
- func (d Record) GetInt(key string, defaultVal int) int
- func (d Record) GetTime(key string, layout string) time.Time
- func (d Record) LogTime() time.Time
- type TextParser
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ParserConfig ¶
type ParserConfig struct {
// ReuseRecord controls whether calls to Read may return a slice sharing
// the backing array of the previous call's returned slice for performance.
// By default, each call to Read returns newly allocated memory owned by the caller.
ReuseRecord bool
}
type Record ¶
type Record struct {
// contains filtered or unexported fields
}
Record is a single parsed log line, equivalent to [slog.Record].
func (Record) ContainsKey ¶
ContainsKey returns true if a Record pair contains an exact key match.
func (Record) ContainsPair ¶
ContainsPair returns true if a Record pair contains an exact key and value match.
func (Record) ForEach ¶
ForEach calls the given function for each key-value pair in the Record. These are guaranteed to be returned in the same order they appeared in the log line.
func (Record) Get ¶
Get returns the value for the given key. If the key is not found, returns an empty string.
func (Record) GetDuration ¶
GetDuration returns the value for the given key as a time.Duration. If the key is not found or the value is not a valid duration, returns the argument default value.
func (Record) GetInt ¶
GetInt returns the value for the given key as an integer. If the key is not found or the value is not an integer, returns the argument default value.
type TextParser ¶
type TextParser struct {
// contains filtered or unexported fields
}
TextParser implements parsing of a [slog.TextHandler] generated structured log file.
Example ¶
package main
import (
"fmt"
"log/slog"
"os"
"github.com/soypat/slogparse"
)
func main() {
const filename = "slog.log"
fp, err := os.Create(filename)
if err != nil {
panic(err.Error())
}
log := slog.New(slog.NewTextHandler(fp, nil))
log.Info("Hello", slog.String("name", "World"))
log.Info("Bye", slog.String("name", "Welt"))
if err != nil {
panic(err.Error())
}
fp.Seek(0, 0)
p := slogparse.NewTextParser(fp, slogparse.ParserConfig{})
for {
record, err := p.Next()
if err != nil {
break
}
message := record.Get("msg")
level := record.Get("level")
name := record.Get("name")
fmt.Println(level, message, "name:", name)
}
}
Output: INFO Hello name: World INFO Bye name: Welt
func NewTextParser ¶
func NewTextParser(r io.Reader, cfg ParserConfig) *TextParser
NewTextParser returns a new parser ready to parse TextHandler slog-formatted logs.
func (*TextParser) Next ¶
func (p *TextParser) Next() (Record, error)
Next reads the next log line from the input and returns it as a Record. Next returns io.EOF when the input ends.
func (*TextParser) Reset ¶
func (p *TextParser) Reset(r io.Reader)
Reset discards any buffered data and resets the parser to read from r.