main.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/vjeantet/grok"
  5. )
  6. func main() {
  7. fmt.Println("# Default Capture :")
  8. g, _ := grok.New()
  9. values, _ := g.Parse("%{COMMONAPACHELOG}", `127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`)
  10. for k, v := range values {
  11. fmt.Printf("%+15s: %s\n", k, v)
  12. }
  13. fmt.Println("\n# Named Capture :")
  14. g, _ = grok.NewWithConfig(&grok.Config{NamedCapturesOnly: true})
  15. values, _ = g.Parse("%{COMMONAPACHELOG}", `127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`)
  16. for k, v := range values {
  17. fmt.Printf("%+15s: %s\n", k, v)
  18. }
  19. fmt.Println("\n# Add custom patterns :")
  20. // We add 3 patterns to our Grok instance, to structure an IRC message
  21. g, _ = grok.NewWithConfig(&grok.Config{NamedCapturesOnly: true})
  22. g.AddPattern("IRCUSER", `\A@(\w+)`)
  23. g.AddPattern("IRCBODY", `.*`)
  24. g.AddPattern("IRCMSG", `%{IRCUSER:user} .* : %{IRCBODY:message}`)
  25. values, _ = g.Parse("%{IRCMSG}", `@vjeantet said : Hello !`)
  26. for k, v := range values {
  27. fmt.Printf("%+15s: %s\n", k, v)
  28. }
  29. }