public class Grok extends Object
Grok parse arbitrary text and structure it.
Grok is simple API that allows you to easily parse logs
and other files (single line). With Grok,
you can turn unstructured log and event data into structured data (JSON).
example:
Grok grok = Grok.create("patterns/patterns");
grok.compile("%{USER}");
Match gm = grok.match("root");
gm.captures();
| Constructor and Description |
|---|
Grok()
Create a new empty
Grok object. |
| Modifier and Type | Method and Description |
|---|---|
void |
addPattern(String name,
String pattern)
Add custom pattern to grok in the runtime.
|
void |
addPatternFromFile(String file)
Add patterns to
Grok from the given file. |
void |
addPatternFromReader(Reader r)
Add patterns to
Grok from a Reader. |
String |
capture(String log)
Match the given log with the named regex.
|
List<String> |
captures(List<String> logs)
Match the given list of log with the named regex
and return the list of json representation of the matched elements.
|
void |
compile(String pattern)
Compile the
Grok pattern to named regex pattern. |
void |
copyPatterns(Map<String,String> cpy)
Copy the given Map of patterns (pattern name, regular expression) to
Grok,
duplicate element will be override. |
static Grok |
create(String grokPatternPath)
Create a
Grok instance with the given grok patterns file. |
static Grok |
create(String grokPatternPath,
String grokExpression)
Create a
Grok instance with the given patterns file and
a Grok pattern. |
String |
discover(String input)
Grok will try to find the best expression that will match your input. |
String |
getNamedRegex()
Get the named regex from the
Grok pattern. |
Map<String,String> |
getNamedRegexCollection()
Get the full collection of the named regex.
|
String |
getNamedRegexCollectionById(String id)
Get the named regex from the given id.
|
String |
getOriginalGrokPattern()
Original grok pattern used to compile to the named regex.
|
Map<String,String> |
getPatterns()
Get the current map of
Grok pattern. |
String |
getSaved_pattern() |
Match |
match(String text)
Match the given text with the named regex
Grok will extract data from the string and get an extence of Match. |
void |
setSaved_pattern(String savedpattern) |
public static final Grok EMPTY
Grok.public String getSaved_pattern()
public void setSaved_pattern(String savedpattern)
public static Grok create(String grokPatternPath, String grokExpression) throws GrokException
Grok instance with the given patterns file and
a Grok pattern.grokPatternPath - Path to the pattern filegrokExpression - - OPTIONAL - Grok pattern to compile ex: %{APACHELOG}Grok instanceGrokExceptionpublic static Grok create(String grokPatternPath) throws GrokException
Grok instance with the given grok patterns file.grokPatternPath - : Path to the pattern fileGrokExceptionpublic void addPattern(String name, String pattern) throws GrokException
name - : Pattern Namepattern - : Regular expression Or Grok patternGrokExceptionpublic void copyPatterns(Map<String,String> cpy) throws GrokException
Grok,
duplicate element will be override.cpy - : Map to copyGrokExceptionpublic Map<String,String> getPatterns()
Grok pattern.public String getNamedRegex()
Grok pattern.
See compile(String) for more detail.public void addPatternFromFile(String file) throws GrokException
Grok from the given file.file - : Path of the grok patternGrokExceptionpublic void addPatternFromReader(Reader r) throws GrokException
Grok from a Reader.r - : Reader with Grok patternsGrokExceptionpublic String capture(String log)
log - : log to matchpublic List<String> captures(List<String> logs)
logs - : list of logpublic Match match(String text)
Grok will extract data from the string and get an extence of Match.text - : Single line of logpublic void compile(String pattern) throws GrokException
Grok pattern to named regex pattern.pattern - : Grok pattern (ex: %{IP})GrokExceptionpublic String discover(String input)
Grok will try to find the best expression that will match your input.
Discoveryinput - : Single line of logpublic String getOriginalGrokPattern()
public String getNamedRegexCollectionById(String id)
id - : named regex idCopyright © 2014. All rights reserved.