# Dynamic fields name within grok match

**URL:** https://discuss.elastic.co/t/dynamic-fields-name-within-grok-match/287150
**Category:** Logstash
**Created:** [October 20, 2021, 3:20am UTC](https://discuss.elastic.co/t/dynamic-fields-name-within-grok-match/287150 "2021-10-20T03:20:43Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![paska](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paska/32/96015_2.png) [@paska](https://discuss.elastic.co/u/paska)
#### Post date: [October 20, 2021, 3:20am UTC](https://discuss.elastic.co/t/dynamic-fields-name-within-grok-match/287150/1 "2021-10-20T03:20:43Z")

</div>

Hello. I'm wanted to use dynamic fields name within grok match. But I couldn't find any information about it and about correct syntax.

First of all I'm getting field, which depends on field log.file.path

```auto
	grok {	
		match => { "log.file.path" => "(?<log_prefix>((?<=logs\/)(\w*)(?=\/)|(?<=logs\/)(\w*)(?=.*\.)))" }
	}

```

In the next step, I want to use value of log\_prefix field as a part of new field:

```auto
grok {
			match => { "message" => "%{DATESTAMP:dev.%{log_prefix}.timestamp}] (?<dev.%{log_prefix}.loglevel>production\.[A-Z]{3,9}|local\.[A-Z]{3,9}): (?<dev.%{log_prefix}.message>(.|\r|\n)*)"}
	}

```

So, in this case, I've got error \<Grok::PatternError: pattern %{log\_prefix} not defined\>. Of course I understand why. Is there any suggestion how I can do this correctly? Thanks.

---

<div class="post-metadata">

### Author: ![Cad](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cad/32/86661_2.png) [@Cad](https://discuss.elastic.co/u/Cad)
#### Post date: [October 20, 2021, 12:36pm UTC](https://discuss.elastic.co/t/dynamic-fields-name-within-grok-match/287150/2 "2021-10-20T12:36:37Z")

</div>

Hi,

You should put the values in static fields and create the dynamic fields with the `add_field` option of the grok filter.  
The code need to be like this i think:

```auto
grok {	
    match => { "log.file.path" => "(?<log_prefix>((?<=logs\/)(\w*)(?=\/)|(?<=logs\/)(\w*)(?=.*\.)))" }
}
grok {
    match => { "message" => "%{DATESTAMP:tmp1}] (?<tmp2>production\.[A-Z]{3,9}|local\.[A-Z]{3,9}): (?<tmp3>(.|\r|\n)*)"}
    #add_field executed only if the filter is successful
    add_field => {
        "dev.%{log_prefix}.timestamp" => "%{tmp1}"
        "dev.%{log_prefix}.loglevel" => "%{tmp2}"
        "dev.%{log_prefix}.message" => "%{tmp3}"
        }
	}

```

Cad.

---

<div class="post-metadata">

### Author: ![paska](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paska/32/96015_2.png) [@paska](https://discuss.elastic.co/u/paska)
#### Post date: [October 21, 2021, 2:52am UTC](https://discuss.elastic.co/t/dynamic-fields-name-within-grok-match/287150/3 "2021-10-21T02:52:41Z")

</div>

Hi. Thanks for your solution. I've found another one for me. But it's similar. Instead of using "add\_fields" I decided to use "mutate" filter to rename temporary fields.

```auto
grok {	
		match => { "log.file.path" => "(?<log_prefix>((?<=logs\/)(\w*)(?=\/)|(?<=logs\/)(\w*)(?=.*\.)))" }
}

grok {
		match => { "message" => "%{DATESTAMP:temp.timestamp}](\[(?<temp.thread>[0-9]*)\])? (?<temp.loglevel>production\.[A-Z]{3,9}|local\.[A-Z]{3,9}|Processing|Processed|Failed): (?<temp.message>(.|\r|\n)*[^\n]+)"}
		}

mutate {
			rename => { "temp.timestamp" => "%{server_type}.%{log_prefix}.timestamp" }
			rename => { "temp.loglevel" => "%{server_type}.%{log_prefix}.loglevel" }
			rename => { "temp.message" => "%{server_type}.%{log_prefix}.message" }
			rename => { "temp.thread" => "%{server_type}.%{log_prefix}.thread" }
}

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [November 18, 2021, 2:53am UTC](https://discuss.elastic.co/t/dynamic-fields-name-within-grok-match/287150/4 "2021-11-18T02:53:21Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
