# Add message\_length field

**URL:** <https://discuss.elastic.co/t/add-message-length-field/272737>\
**Category:** Logstash\
**Created:** [May 11, 2021, 6:18pm UTC](https://discuss.elastic.co/t/add-message-length-field/272737 "2021-05-11T18:18:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lag](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lag/32/37896_2.png) [@lag](https://discuss.elastic.co/u/lag)\
**Post date:** [May 11, 2021, 6:18pm UTC](https://discuss.elastic.co/t/add-message-length-field/272737/1 "2021-05-11T18:18:42Z")

</div>

I am trying to add calculated "message\_length" field that would be a representation on character count in "message" field. Info is coming in from winlogbeat/filebeat/syslog if that makes any difference.

This is the code I currently have:

```auto
filter {
  ruby {
	code => "event['message_length'] = event['message'].length"
        }
}

```

But it's producing 0 results. No value is added into message\_length field.

What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [May 11, 2021, 6:31pm UTC](https://discuss.elastic.co/t/add-message-length-field/272737/2 "2021-05-11T18:31:30Z")

</div>

> [@lag](#):
>
> `code => "event['message_length'] = event['message'].length"`

Unless you are running an old version (4.x perhaps) that would have to be

```
code => "event.set('message_length', event.get('message').length)"

```

---

<div class="post-metadata">

**Author:** ![lag](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lag/32/37896_2.png) [@lag](https://discuss.elastic.co/u/lag)\
**Post date:** [May 11, 2021, 6:39pm UTC](https://discuss.elastic.co/t/add-message-length-field/272737/3 "2021-05-11T18:39:40Z")

</div>

> [@Badger](#):
>
> `code => "event.set`

Thank you! That works, running 7.x.

---

<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:** [June 8, 2021, 6:39pm UTC](https://discuss.elastic.co/t/add-message-length-field/272737/4 "2021-06-08T18:39:50Z")

</div>

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