diff options
| author | Paul Buetow <paul@buetow.org> | 2023-09-05 14:13:48 +0300 |
|---|---|---|
| committer | Paul Buetow <pbuetow@mimecast.com> | 2023-09-07 15:32:20 +0300 |
| commit | bf39452c1f9a06d9f4e6eb3a06a23068a2451ca5 (patch) | |
| tree | 3e5003050de7440d789821e30e3632e0b6f3338c /doc/logformats.md | |
| parent | 54914d67e98116ec93ee324d28db4a772be02bc2 (diff) | |
Update creating your own logformat docs, to reflect the recent changes.
Diffstat (limited to 'doc/logformats.md')
| -rw-r--r-- | doc/logformats.md | 61 |
1 files changed, 43 insertions, 18 deletions
diff --git a/doc/logformats.md b/doc/logformats.md index c3f0c63..9d4e55d 100644 --- a/doc/logformats.md +++ b/doc/logformats.md @@ -21,15 +21,26 @@ By default, DTail will use the `default` log format. You can override the log fo % dmap --files /var/log/example.log --query 'from EXAMPLE select ....queryhere.... logformat generickv' ``` -Alternatively, you can override the default log format with `MapreduceLogFormat` in the Server section of `dtail.json`. +You can override the default log format with `MapreduceLogFormat` in the Server section of `dtail.json`. ## Under the hood: generickv As an example, let's have a look at the `generickv` log format's implementation. It's located at `internal/mapr/logformat/generickv.go`: -```shell -// MakeFieldsGENERIGKV is the generic key-value logfile parser. -func (p *Parser) MakeFieldsGENERIGKV(maprLine string) (map[string]string, error) { +```go +type genericKVParser struct { + defaultParser +} + +func newGenericKVParser(hostname, timeZoneName string, timeZoneOffset int) (*genericKVParser, error) { + defaultParser, err := newDefaultParser(hostname, timeZoneName, timeZoneOffset) + if err != nil { + return &genericKVParser{}, err + } + return &genericKVParser{defaultParser: *defaultParser}, nil +} + +func (p *genericKVParser) MakeFields(maprLine string) (map[string]string, error) { splitted := strings.Split(maprLine, protocol.FieldDelimiter) fields := make(map[string]string, len(splitted)) @@ -44,7 +55,7 @@ func (p *Parser) MakeFieldsGENERIGKV(maprLine string) (map[string]string, error) for _, kv := range splitted[0:] { keyAndValue := strings.SplitN(kv, "=", 2) if len(keyAndValue) != 2 { - // dlog.Common.Debug("Unable to parse key-value token, ignoring it", kv) + //dlog.Common.Debug("Unable to parse key-value token, ignoring it", kv) continue } fields[keyAndValue[0]] = keyAndValue[1] @@ -101,26 +112,40 @@ These variables may only exist in the DTail default log format (see `internal/ma * `$pid` - DTail server process ID * `$uptime` - DTail server uptime -## Implementing your own log format +## Implementing your own log format `Foo` -All what needs to be done is to place your own implementation into the `logformat` source directory. As a template, you can copy an existing format ... +What needs to be done is to place your own implementation into the `logformat` source directory. As a template, you can copy an existing format ... ```shell -% cp internal/mapr/logformat/generic.go internal/mapr/logformat/yourcustomformat.go +% cp internal/mapr/logformat/generic.go internal/mapr/logformat/foo.go ``` -... and replace `GENERIGKV` with your format's name in capital letters (the method name string is used by DTail to reflect the log format parser method, so it is important to name it correctly): +... and replace `generic` ` with your format's name `foo`: + +```go +package logformat + +type fooParser struct { + defaultParser +} + +func newFooParser(hostname, timeZoneName string, timeZoneOffset int) (*fooParser, error) { + defaultParser, err := newDefaultParser(hostname, timeZoneName, timeZoneOffset) + if err != nil { + return &fooParser{}, err + } + return &fooParser{defaultParser: *defaultParser}, nil +} + +func (p *fooParser) MakeFields(maprLine string) (map[string]string, error) { + fields := make(map[string]string, 3) + + .. + <YOUR CUSTOM CODE HERE> + .. -```shell -// MakeFieldsCUSTOMLOGFORMAT is your own custom log format. -func (p *Parser) MakeFieldsCUSTOMLOGFORMAT(maprLine string) (map[string]string, error) { - // .. Your own format implementation goes here - // .. you can parse maprLine and store values into the fields map. -.. -. -. return fields, nil } ``` -Once done, recompile DTail. DTail now understands `... logformat customlogformat` (see "Seleting a log format" above). +Once done, recompile DTail. DTail now understands `... logformat foo` (see "Seleting a log format" above). |
