Exemple #1
0
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)
	}
}
Exemple #2
0
func main() {
	if len(os.Args[1:]) < 2 {
		fmt.Println("--------------------------------")
		fmt.Println("grok")
		fmt.Println("--------------------------------")
		fmt.Println("usage: ")
		fmt.Println("\tgrok <filename> \"<pattern>\" [patternsDir] [newLinePattern]\n\r")
		fmt.Println("*error* filename and pattern are required arguments")
		return
	}

	g := grok.New()

	file := os.Args[1]
	pattern := os.Args[2]

	fmt.Println("...scanning", file, "for pattern", pattern)

	if len(os.Args[1:]) >= 3 {
		patternsDir := os.Args[3]
		err := loadPatternsDir(patternsDir, *g) //g.AddPatternsFromPath(patternsDir) // not working!
		if err != nil {
			log.Fatalf("oops", err)
		}
	}

	if len(os.Args[1:]) >= 4 {
		newLinePattern := os.Args[4]
		fmt.Println("newline pattern:", newLinePattern)
		fmt.Println("*warning* multiline pattern matching is not yet implemented")
	}
	// yet to be implemented

	err := readLines(file, func(line string) {
		//    err := readLinesWithRegex(file, "^\\d", func(line string) {
		log.Println("--- newline ---", line)
		values, _ := g.Parse(pattern, line)
		for k, v := range values {
			log.Println(fmt.Sprintf("%+15s: %s", k, v))
		}
	})

	if err != nil {
		log.Fatal(err)
	}
}
Exemple #3
0
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))
	}
}