slogparse

package module
v0.0.0-...-aaba849 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2024 License: BSD-3-Clause Imports: 6 Imported by: 0

README

slogparse

go.dev reference Go Report Card codecov Go sourcegraph

Parse logs generated with Go's slog package.

How to install package with newer versions of Go (+1.16):

go mod download github.com/soypat/slogparse@latest

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) // Reset file pointer to start to re-read with parser.
	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
}

Documentation

Index

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

func (d Record) ContainsKey(key string) bool

ContainsKey returns true if a Record pair contains an exact key match.

func (Record) ContainsPair

func (d Record) ContainsPair(key, value string) bool

ContainsPair returns true if a Record pair contains an exact key and value match.

func (Record) ForEach

func (d Record) ForEach(fn func(key, value string))

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

func (d Record) Get(key string) string

Get returns the value for the given key. If the key is not found, returns an empty string.

func (Record) GetDuration

func (d Record) GetDuration(key string, defaultVal time.Duration) time.Duration

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

func (d Record) GetInt(key string, defaultVal int) int

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.

func (Record) GetTime

func (d Record) GetTime(key string, layout string) time.Time

GetTime returns the value for the given key as a time.Time. If the key is not found or the value is not a valid time, returns time.Time{}.

func (Record) LogTime

func (d Record) LogTime() time.Time

LogTime returns time.Time from "time" key in the dictionary. If not found, returns time.Time{}.

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.

Jump to

Keyboard shortcuts

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