Go (RE2)

Log Level in GO

Matches standard log level keywords in log lines.

Try it in the GO tester →

Pattern

regexGO
\b(TRACE|DEBUG|INFO|WARN|ERROR|FATAL)\b   (flags: g)

Go (RE2) code

goGo
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`\b(TRACE|DEBUG|INFO|WARN|ERROR|FATAL)\b`)
	input := `[INFO] Server started. [ERROR] Connection failed.`
	for _, match := range re.FindAllString(input, -1) {
		fmt.Println(match)
	}
}

Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.

How the pattern works

`\b(TRACE|DEBUG|INFO|WARN|ERROR|FATAL)\b` matches any of the six standard log levels as whole words.

Examples

Input

[INFO] Server started. [ERROR] Connection failed.

Matches

  • INFO
  • ERROR

Input

DEBUG: cache miss

Matches

  • DEBUG

Same pattern, other engines

← Back to Log Level overview (all engines)