grok

package module
v1.1.4 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2024 License: Apache-2.0 Imports: 9 Imported by: 15

README

grok

This library is a fork of github.com/vjeantet/grok that parses grok patterns in Go.

Usage

Denormalize and Compile
denormalized, errs := DenormalizePatternsFromMap(CopyDefalutPatterns())
if len(errs) == 0 {
  g, err := CompilePattern("%{DAY:day}", grok.PatternStorage{denormalized})
  if err == nil {
    ret, _ := g.Run("Tue qds", false)
  }
}

Example

package main

import (
  "fmt"

  "github.com/GuanceCloud/grok"
)

func main() {
  de, errs := grok.DenormalizePatternsFromMap(grok.CopyDefalutPatterns())
  if len(errs) != 0 {
    fmt.Print(errs)
    return
  }
  g, err := grok.CompilePattern("%{COMMONAPACHELOG}", grok.PatternStorage{de})
  if err != nil {
    fmt.Print(err)
  }
  ret, err := g.Run(`127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`, true)
  if err != nil {
    fmt.Print(err)
  }
  for k, name := range g.MatchNames() {
    fmt.Printf("%+15s: %s\n", name, ret[k])
  }
}

output:

     clientip: 127.0.0.1
        ident: -
         auth: -
    timestamp: 23/Apr/2014:22:58:32 +0200
         verb: GET
      request: /index.php
  httpversion: 1.1
   rawrequest:
     response: 404
        bytes: 207

Documentation

Overview

Package grok used to parses grok patterns in Go

Index

Constants

View Source
const (
	GTypeStr    = "str"
	GTypeString = "string"
	GTypeInt    = "int"
	GTypeFloat  = "float"
	GTypeBool   = "bool"
)

Variables

View Source
var ErrMismatch = errors.New("mismatch")
View Source
var ErrNotCompiled = errors.New("not compiled")

Functions

func CopyDefalutPatterns added in v1.1.1

func CopyDefalutPatterns() map[string]string

func CopyDenormalizedDefalutPatterns added in v1.1.1

func CopyDenormalizedDefalutPatterns() map[string]*GrokPattern

func DenormalizePatternsFromMap added in v1.1.1

func DenormalizePatternsFromMap(m map[string]string, denormalized ...map[string]*GrokPattern) (map[string]*GrokPattern, map[string]string)

DenormalizePatternsFromMap denormalize pattern from map, will return a valid pattern:value map and an invalid pattern:error map.

func LoadPatternsFromPath added in v1.1.1

func LoadPatternsFromPath(path string) (map[string]string, error)

Types

type GrokPattern added in v1.1.1

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

func DenormalizePattern added in v1.1.1

func DenormalizePattern(input string, denormalized ...PatternStorageIface) (
	*GrokPattern, error,
)

DenormalizePattern denormalizes the pattern to the regular expression.

func (*GrokPattern) Denormalized added in v1.1.1

func (g *GrokPattern) Denormalized() string

func (*GrokPattern) Pattern added in v1.1.1

func (g *GrokPattern) Pattern() string

func (*GrokPattern) TypedVar added in v1.1.1

func (g *GrokPattern) TypedVar() map[string]string

type GrokRegexp added in v1.1.1

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

func CompilePattern added in v1.1.1

func CompilePattern(input string, denomalized PatternStorageIface) (*GrokRegexp, error)

func (*GrokRegexp) GetValAnyByName added in v1.1.4

func (g *GrokRegexp) GetValAnyByName(k string, val []any) (any, bool)

func (*GrokRegexp) GetValByName added in v1.1.4

func (g *GrokRegexp) GetValByName(k string, val []string) (string, bool)

func (*GrokRegexp) GetValCastByName added in v1.1.4

func (g *GrokRegexp) GetValCastByName(k string, val []string) (any, bool)

func (*GrokRegexp) MatchNames added in v1.1.4

func (g *GrokRegexp) MatchNames() []string

func (*GrokRegexp) Run added in v1.1.1

func (g *GrokRegexp) Run(content string, trimSpace bool) ([]string, error)

func (*GrokRegexp) RunWithTypeInfo added in v1.1.1

func (g *GrokRegexp) RunWithTypeInfo(content string, trimSpace bool) ([]any, error)

func (*GrokRegexp) WithTypeInfo added in v1.1.4

func (g *GrokRegexp) WithTypeInfo() bool

type PatternStorage added in v1.1.1

type PatternStorage []map[string]*GrokPattern

func (PatternStorage) GetPattern added in v1.1.1

func (p PatternStorage) GetPattern(pattern string) (*GrokPattern, bool)

func (PatternStorage) SetPattern added in v1.1.1

func (p PatternStorage) SetPattern(patternAlias string, gp *GrokPattern)

type PatternStorageIface added in v1.1.1

type PatternStorageIface interface {
	GetPattern(string) (*GrokPattern, bool)
	SetPattern(string, *GrokPattern)
}

type SubMatchName added in v1.1.4

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

Jump to

Keyboard shortcuts

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