# How to extract timestamp and log level from message and add it as a filed

**URL:** https://discuss.elastic.co/t/how-to-extract-timestamp-and-log-level-from-message-and-add-it-as-a-filed/316639
**Category:** Logstash
**Created:** [October 14, 2022, 1:00pm UTC](https://discuss.elastic.co/t/how-to-extract-timestamp-and-log-level-from-message-and-add-it-as-a-filed/316639 "2022-10-14T13:00:56Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Akumar22](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@Akumar22](https://discuss.elastic.co/u/Akumar22)
#### Post date: [October 14, 2022, 1:00pm UTC](https://discuss.elastic.co/t/how-to-extract-timestamp-and-log-level-from-message-and-add-it-as-a-filed/316639/1 "2022-10-14T13:00:56Z")

</div>

Hi

I want to extract timestamp and log level from message and want to add it as a field can you please suggest some filter or grok pattern to do this  
sample log :  
`2022-10-12 12:02:32,611 [DEBUG][pool-5-thread-14][com.hybris.service.workflow.worker.order.InitiateFulfilmentWorker][][] after obtaining lock `

---

<div class="post-metadata">

### Author: ![Rios](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rios/32/95745_2.png) [@Rios](https://discuss.elastic.co/u/Rios)
#### Post date: [October 15, 2022, 2:58am UTC](https://discuss.elastic.co/t/how-to-extract-timestamp-and-log-level-from-message-and-add-it-as-a-filed/316639/2 "2022-10-15T02:58:10Z")

</div>

Dissect, faster

```auto
	 dissect {
      mapping => {
        "message" => "%{time} [%{level}][%{thread}][%{method}][%{field1}][%{field2}] %{logmsg}"
      }

```

Grok, should prefer for this case

```auto
    grok { 
       match => { "message" => "%{TIMESTAMP_ISO8601:time}%{SPACE}\[%{LOGLEVEL:loglevel}\]%{SPACE}\[%{DATA:tread}\]%{SPACE}\[%{DATA:method}\]%{SPACE}\[%{DATA:field1}\]%{SPACE}\[%{DATA:field2}\]%{SPACE}%{GREEDYDATA:logmsg}" }

    }

```

Convert to the date format

```auto
    date {
       match => ["timestamp", "yyyy/MM/dd HH:mm:ss,SSS"]
       target => "@timestamp"
       remove_field => ["timestamp"]
    }

```

---

<div class="post-metadata">

### Author: ![Akumar22](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@Akumar22](https://discuss.elastic.co/u/Akumar22)
#### Post date: [October 17, 2022, 8:03am UTC](https://discuss.elastic.co/t/how-to-extract-timestamp-and-log-level-from-message-and-add-it-as-a-filed/316639/3 "2022-10-17T08:03:25Z")

</div>

I am getting this error after using this dissect and grok filter

```auto
Failed to execute action {:action=>LogStash::PipelineAction::Create/pipeline_id:oms-server-logs, :exception=>"LogStash::ConfigurationError", :message=>"Expected one of [\\t\\r\\n], \"#\", \"=>\" at line 16, column 9 (byte 286) after filter {\n dissect {\n mapping => {\n \"message\" => \"%{time} [%{level}][%{thread}][%{method}][%{field1}][%{field2}] %{logmsg}\"\n }\n \n grok ", :backtrace=>["/usr/share/logstash/logstash-core/lib/logstash/compiler.rb:32:in `compile_imperative'", "org/logstash/execution/AbstractPipelineExt.java:199:in `initialize'", "org/logstash/execution/JavaBasePipelineExt.java:72:in `initialize'", "/usr/share/logstash/logstash-core/lib/logstash/java_pipeline.rb:48:in `initialize'", "/usr/share/logstash/logstash-core/lib/logstash/pipeline_action/create.rb:50:in `execute'", "/usr/share/logstash/logstash-core/lib/logstash/agent.rb:381:in `block in converge_state'"]}

```

---

<div class="post-metadata">

### Author: ![Rios](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rios/32/95745_2.png) [@Rios](https://discuss.elastic.co/u/Rios)
#### Post date: [October 17, 2022, 10:12am UTC](https://discuss.elastic.co/t/how-to-extract-timestamp-and-log-level-from-message-and-add-it-as-a-filed/316639/4 "2022-10-17T10:12:55Z")

</div>

Add 1 more }

```auto
dissect {
      mapping => {
        "message" => "%{time} [%{level}][%{thread}][%{method}][%{field1}][%{field2}] %{logmsg}"
      }
}

```

---

<div class="post-metadata">

### Author: ![Akumar22](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@Akumar22](https://discuss.elastic.co/u/Akumar22)
#### Post date: [October 18, 2022, 9:30am UTC](https://discuss.elastic.co/t/how-to-extract-timestamp-and-log-level-from-message-and-add-it-as-a-filed/316639/5 "2022-10-18T09:30:51Z")

</div>

> [@Rios](#):
>
> ```auto
> grok { 
> match => { "message" => "%{TIMESTAMP_ISO8601:time}%{SPACE}\[%{LOGLEVEL:loglevel}\]%{SPACE}\[%{DATA:tread}\]%{SPACE}\[%{DATA:method}\]%{SPACE}\[%{DATA:field1}\]%{SPACE}\[%{DATA:field2}\]%{SPACE}%{GREEDYDATA:logmsg}" }
> 
> }
> 
> ```

Thank you this dissect is working

---

<div class="post-metadata">

### Author: ![Akumar22](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@Akumar22](https://discuss.elastic.co/u/Akumar22)
#### Post date: [October 18, 2022, 9:31am UTC](https://discuss.elastic.co/t/how-to-extract-timestamp-and-log-level-from-message-and-add-it-as-a-filed/316639/6 "2022-10-18T09:31:27Z")

</div>

match =\> ["timestamp", "yyyy-MM-dd HH:mm:ss,SSS"]

It should be like this

---

<div class="post-metadata">

### Author: ![Rios](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rios/32/95745_2.png) [@Rios](https://discuss.elastic.co/u/Rios)
#### Post date: [October 18, 2022, 9:47am UTC](https://discuss.elastic.co/t/how-to-extract-timestamp-and-log-level-from-message-and-add-it-as-a-filed/316639/7 "2022-10-18T09:47:42Z")

</div>

Yes, sorry my mistake, copy+paste.

---

<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 15, 2022, 9:48am UTC](https://discuss.elastic.co/t/how-to-extract-timestamp-and-log-level-from-message-and-add-it-as-a-filed/316639/8 "2022-11-15T09:48:31Z")

</div>

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