# New user - Simple problem with adding a field with mutate

**URL:** <https://discuss.elastic.co/t/new-user-simple-problem-with-adding-a-field-with-mutate/59605>\
**Category:** Logstash\
**Created:** [September 1, 2016, 9:37pm UTC](https://discuss.elastic.co/t/new-user-simple-problem-with-adding-a-field-with-mutate/59605 "2016-09-01T21:37:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jazzed](https://avatars.discourse-cdn.com/v4/letter/j/258eb7/32.png) [@jazzed](https://discuss.elastic.co/u/jazzed)\
**Post date:** [September 1, 2016, 9:37pm UTC](https://discuss.elastic.co/t/new-user-simple-problem-with-adding-a-field-with-mutate/59605/1 "2016-09-01T21:37:53Z")

</div>

I want to add a new field based on the content of the source field from filebeat. The filebeat-\* index shows SOME logs get the new field, and other logs are empty. However, the logstash-\* index has file defined for every log file. Don't understand.

Tnx

```
input {

```

beats {  
port =\> 5044  
}  
}

filter {  
mutate {  
add\_field =\> {  
"file" =\> "%{source}"  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [September 2, 2016, 5:49am UTC](https://discuss.elastic.co/t/new-user-simple-problem-with-adding-a-field-with-mutate/59605/2 "2016-09-02T05:49:01Z")

</div>

If the `source` field doesn't always exist you shouldn't unconditionally include the mutate filter. Use a conditional:

```nohighlight
if [source] {
  mutate {
    add_field => {
      "file" => "%{source}"
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![jazzed](https://avatars.discourse-cdn.com/v4/letter/j/258eb7/32.png) [@jazzed](https://discuss.elastic.co/u/jazzed)\
**Post date:** [September 2, 2016, 4:44pm UTC](https://discuss.elastic.co/t/new-user-simple-problem-with-adding-a-field-with-mutate/59605/3 "2016-09-02T16:44:31Z")

</div>

Thank you, magnus. Could I get you to look at my latest config? I made my jboss filebeat send the type as "jboss" so I could do some processing on it. I want to extract the debug level. Again, some logs I see the level field added and some I don't, yet the message exists and my pattern matches as confirmed in the grok debugger.

```
filter {
    mutate {
        add_field => {
            "file" => "%{source}"
        }
    }
    if [message] {
    if [type] == "jboss" {
        grok {
            match => { "message" => "%{TIME:time} %{WORD:level} .*" }
        }
        grok {
            add_field => { "level" => "%{level}" }
        }
    }
    }
}

```

Example line that isn't generating the level field.

`10:23:09,766 INFO [stdout] (Curator-TreeCache-0) Listen: Update path: /realtime/subscriptions/network/160072/160072/TLOC, timestamp is: 1472836989577`

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [September 3, 2016, 3:36pm UTC](https://discuss.elastic.co/t/new-user-simple-problem-with-adding-a-field-with-mutate/59605/4 "2016-09-03T15:36:51Z")

</div>

> ```
> if [message] {
> 
> ```

```
if [type] == "jboss" {

```

Equivalent:

```
if [message] and [type] == "jboss" {

```

> ```
> grok {
> 
> ```

```
        add_field => { "level" => "%{level}" }
    }

```

I've seen people make this exact mistake a few times before. What made you add this?

> Example line that isn't generating the level field.

Works just fine:

```plaintext
$ cat test.config
input { stdin {} }
output { stdout { codec => rubydebug } }
filter {
  grok {
    match => ["message", "%{TIME:time} %{WORD:level} .*"]
  }
}
$ echo '10:23:09,766 INFO [stdout] (Curator-TreeCache-0) Listen: Update path: /realtime/subscriptions/network/160072/160072/TLOC, timestamp is: 1472836989577' | logstash -f test.config
Settings: Default pipeline workers: 8
Pipeline main started
{
       "message" => "10:23:09,766 INFO [stdout] (Curator-TreeCache-0) Listen: Update path: /realtime/subscriptions/network/160072/160072/TLOC, timestamp is: 1472836989577",
      "@version" => "1",
    "@timestamp" => "2016-09-03T15:35:47.428Z",
          "host" => "bertie",
          "time" => "10:23:09,766",
         "level" => "INFO"
}
Pipeline main has been shutdown
stopping pipeline {:id=>"main"}

```

---

<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:** [July 6, 2017, 4:40am UTC](https://discuss.elastic.co/t/new-user-simple-problem-with-adding-a-field-with-mutate/59605/5 "2017-07-06T04:40:04Z")

</div>


