func main() { fmt.Println("# Default Capture :") g := grok.New() values, _ := g.Parse("%{COMMONAPACHELOG}", `127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`) for k, v := range values { fmt.Printf("%+15s: %s\n", k, v) } fmt.Println("\n# Named Capture :") g = grok.NewWithConfig(&grok.Config{NamedCapturesOnly: true}) values, _ = g.Parse("%{COMMONAPACHELOG}", `127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`) for k, v := range values { fmt.Printf("%+15s: %s\n", k, v) } fmt.Println("\n# Add custom patterns :") // We add 3 patterns to our Grok instance, to structure an IRC message g = grok.NewWithConfig(&grok.Config{NamedCapturesOnly: true}) g.AddPattern("IRCUSER", `\A@(\w+)`) g.AddPattern("IRCBODY", `.*`) g.AddPattern("IRCMSG", `%{IRCUSER:user} .* : %{IRCBODY:message}`) values, _ = g.Parse("%{IRCMSG}", `@vjeantet said : Hello !`) for k, v := range values { fmt.Printf("%+15s: %s\n", k, v) } }
func Formatter(input chan []*FileEvent, output chan []*FileEvent, config *Config) { var patterns map[string]string patterns = make(map[string]string) var formats = 0 for _, file := range config.Files { for _, path := range file.Paths { if file.Format != "" { patterns[path] = file.Format formats++ } } } if formats == 0 { emit("Formatting is not enabled") for { for events := range input { output <- events } } } emit("Formatting is enabled (%d grok expressions)", formats) var g *grok.Grok = grok.NewWithConfig(&grok.Config{NamedCapturesOnly: true}) var pattern string for { for events := range input { for _, event := range events { // skip stdin if *event.Source == "-" { continue } // XXX patterns won't work with wildcards in paths because // event.Source is the final path without wildcards. pattern = patterns[*event.Source] if pattern != "" { values, _ := g.ParseTyped(pattern, *event.Text) delete(values, "") encoded, _ := json.Marshal(values) *event.Text = string(encoded) } } output <- events } } }
func main() { pattern := flag.String("pattern", "%{GREEDYDATA:msg}", "a grok expression") namedcapture := flag.Bool("namedcapture", false, "parse only named captures (default is false)") flag.Parse() var g *grok.Grok if *namedcapture { g = grok.NewWithConfig(&grok.Config{NamedCapturesOnly: true}) } else { g = grok.New() } scanner := bufio.NewScanner(os.Stdin) for scanner.Scan() { line := scanner.Text() values, _ := g.Parse(*pattern, line) delete(values, "") encoded, _ := json.Marshal(values) fmt.Println(string(encoded)) } }