# @okuryu/grok-js

> Regular expression template library inspired by logstash grok filter module

Latest version **5.0.0** (published 2026-11-28) · ISC license · 0 weekly downloads

## Install

```sh
npm install @okuryu/grok-js
pnpm add @okuryu/grok-js
yarn add @okuryu/grok-js
bun add @okuryu/grok-js
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 5.0.0 |
| Published | 2026-11-28 |
| First published | 2021-07-16 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 40.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 1 |
| Author | Andrey Chausenko |
| Maintainers | okuryu |
| Keywords | regex, grok, logstash |

## Links

- npm: https://www.npmjs.com/package/@okuryu/grok-js
- Repository: https://github.com/okuryu/grok-js
- Homepage: https://github.com/okuryu/grok-js#readme
- Issues: https://github.com/okuryu/grok-js/issues
- npm.io page: https://npm.io/package/@okuryu/grok-js

## Dependencies (3)

- [debug](https://npm.io/package/debug.md) ^4.1.1
- [universalify](https://npm.io/package/universalify.md) ^2.0.1
- [oniguruma-to-es](https://npm.io/package/oniguruma-to-es.md) ^4.3.6

## Alternatives

- [cli-color](https://npm.io/package/cli-color.md) — 3.4M weekly downloads
- [log](https://npm.io/package/log.md) — 1.3M weekly downloads
- [logstash-client](https://npm.io/package/logstash-client.md) — 4.5K weekly downloads
- [@nocobase/plugin-logger](https://npm.io/package/@nocobase/plugin-logger.md) — 2.0K weekly downloads
- [child-process-debug](https://npm.io/package/child-process-debug.md) — 695 weekly downloads

## Recent versions

- 5.0.0 (latest) — 2026-11-28
- 4.0.0-alpha.1 — 2026-04-06
- 4.0.0-alpha.0 — 2022-08-02
- 3.4.0 — 2021-07-16

## README

# grok-js

> Fork of https://github.com/Beh01der/node-grok

This library is inspired by logstash grok filter but it's not a port of it.

More details about usage and implementation here https://memz.co/parsing-log-files-node-js-regex-grok/

This is a templating library that helps reusing existing regular expressions and constructing new, more complex one. The primary goal was to help parsing and transforming plain text logs into JSON objects (one line => one object) based on provided template. 

## Install

```
npm install @okuryu/grok-js
```

## Quick start

Following simple snippet

```javascript
import grok from'@okuryu/grok-js';

const p = '%{IP:client} \\[%{TIMESTAMP_ISO8601:timestamp}\\] "%{WORD:method} %{URIHOST:site}%{URIPATHPARAM:url}" %{INT:code} %{INT:request} %{INT:response} - %{NUMBER:took} \\[%{DATA:cache}\\] "%{DATA:mtag}" "%{DATA:agent}"';
const str = '203.35.135.165 [2016-03-15T12:42:04+11:00] "GET memz.co/cloud/" 304 962 0 - 0.003 [MISS] "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36"';

grok.loadDefault((err, patterns) => {
  if (err) {
    console.error(err);
    return;
  }

  const pattern = patterns.createPattern(p);

  console.log(pattern.parseSync(str));
});
```

will transform string

```
203.35.135.165 [2016-03-15T12:42:04+11:00] "GET memz.co/cloud/" 304 962 0 - 0.003 [MISS] "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36"
```

into object

```json
{ 
  "client": "203.35.135.165",
  "timestamp": "2016-03-15T12:42:04+11:00",
  "method": "GET",
  "site": "memz.co",
  "url": "/cloud/",
  "code": "304",
  "request": "962",
  "response": "0",
  "took": "0.003",
  "cache": "MISS",
  "mtag": "-",
  "agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36" 
}
```

## Synchronous version of code

```javascript
import grok from '@okuryu/grok-js';

const p = '%{IP:client} \\[%{TIMESTAMP_ISO8601:timestamp}\\] "%{WORD:method} %{URIHOST:site}%{URIPATHPARAM:url}" %{INT:code} %{INT:request} %{INT:response} - %{NUMBER:took} \\[%{DATA:cache}\\] "%{DATA:mtag}" "%{DATA:agent}"';
const str = '203.35.135.165 [2016-03-15T12:42:04+11:00] "GET memz.co/cloud/" 304 962 0 - 0.003 [MISS] "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36"';

try {
  const patterns = grok.loadDefaultSync();
  const pattern = patterns.createPattern(p);

  console.log(pattern.parseSync(str));
} catch (err) {
  console.error(err);
}
```

## Promises

Experimental

```javascript
import grok from'@okuryu/grok-js';

const p = '%{IP:client} \\[%{TIMESTAMP_ISO8601:timestamp}\\] "%{WORD:method} %{URIHOST:site}%{URIPATHPARAM:url}" %{INT:code} %{INT:request} %{INT:response} - %{NUMBER:took} \\[%{DATA:cache}\\] "%{DATA:mtag}" "%{DATA:agent}"';
const str = '203.35.135.165 [2016-03-15T12:42:04+11:00] "GET memz.co/cloud/" 304 962 0 - 0.003 [MISS] "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36"';

grok.loadDefault().then(patterns => {
  return patterns.createPattern(p).parseSync(str);
}).then(obj => {
  console.log(obj);
}).catch(err => {
  console.log(err);
})
```

## API

* **loadDefault([loadModules,] callback)** - creates new pattern collection including all built-in patterns from `./patterns` folder. By providing *loadModules* parameter you can limit number of loaded patterns: `loadDefault(['grok-patterns'] ,...);`. Callback receives *patterns* collection filled in with default templates: `function(err, patterns)`.

* **loadDefaultSync([loadModules])** - creates new default pattern collection and returns it `GrokCollection`.

* **new GrokCollection()** - creates a new empty pattern collection.

* **GrokCollection.createPattern(expression, [id])** - creates new pattern and adds it to the collection. Find out more about pattern syntax [here](http://logstash.net/docs/1.4.2/filters/grok) and about regular expression syntax [here](http://www.geocities.jp/kosako3/oniguruma/doc/RE.txt). Patterns are converted to native JavaScript `RegExp` by [oniguruma-to-es](https://github.com/slevithan/oniguruma-to-es), so only the Oniguruma syntax supported by it is available

* **GrokCollection.getPattern(id)** - returns existing pattern `GrokPattern`

* **GrokCollection.load(filePath, callback)** - asynchronously loads patterns from file. Callback is `function(err)`.

* **GrokCollection.loadSync(filePath)** - loads patterns from file and returns number of newly loaded patterns `number`

* **GrokPattern.parseSync(str)** - parses string using corresponding pattern and returns resulting object `object`, or `null` if the string does not match. Throws if the pattern is invalid

Find out more about grok-js https://memz.co/parsing-log-files-node-js-regex-grok/ 

## License 

**ISC License (ISC)**

Copyright (c) 2019, Andrey Chausenko <andrey.chausenko@gmail.com>

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

---
_Source: https://npm.io/package/@okuryu/grok-js · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
