# ZeroLog runtime error

**URL:** <https://forum.golangbridge.org/t/zerolog-runtime-error/31283>\
**Category:** Technical Discussion\
**Created:** [April 11, 2023, 1:21pm UTC](https://forum.golangbridge.org/t/zerolog-runtime-error/31283 "2023-04-11T13:21:02Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![saujanya](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/s/e36b37/32.png) [@saujanya](https://forum.golangbridge.org/u/saujanya)\
**Post date:** [April 11, 2023, 1:21pm UTC](https://forum.golangbridge.org/t/zerolog-runtime-error/31283/1 "2023-04-11T13:21:02Z")

</div>

I have a project that using concurrent goroutines and each process log messages  
I am seeing below random exception, either for  
log.Msgf or log event

panic: runtime error: index out of range [-1]

goroutine 6190 [running]:  
[github.com/rs/zerolog/internal/json.Encoder.AppendKey(...)](http://github.com/rs/zerolog/internal/json.Encoder.AppendKey(...))  
/vendor/github.com/rs/zerolog/internal/json/base.go:15  
[github.com/rs/zerolog.(\*Event).Str](http://github.com/rs/zerolog.(*Event).Str)(0xc003e10ba0?, {0x11a06c2?, 0xc000470ee0?}, {0x0?, 0x1e?})  
/vendor/github.com/rs/zerolog/event.go:247 +0x13d

---

<div class="post-metadata">

**Author:** ![skillian](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/skillian/32/7876_2.png) [@skillian](https://forum.golangbridge.org/u/skillian)\
**Post date:** [April 12, 2023, 12:29am UTC](https://forum.golangbridge.org/t/zerolog-runtime-error/31283/2 "2023-04-12T00:29:22Z")

</div>

Can you show your code? I found a similar error message in an issue here: [panic: runtime error: index out of range [-1] seen when logging with many go coroutines running · Issue #422 · rs/zerolog · GitHub](https://github.com/rs/zerolog/issues/422)

---

<div class="post-metadata">

**Author:** ![saujanya](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/s/e36b37/32.png) [@saujanya](https://forum.golangbridge.org/u/saujanya)\
**Post date:** [April 12, 2023, 3:07pm UTC](https://forum.golangbridge.org/t/zerolog-runtime-error/31283/3 "2023-04-12T15:07:47Z")

</div>

here is short version of the code

main.go.  
log.Info().Msgf(“%v”, clientName) – to write messages about initializing programs/clients  
…  
api request starts a few goroutines – each routine has following

- log.Debug() or log.Msgf()
- defer func(start time.Time) {  
var e \*zerolog.Event  
switch {  
case err != nil:  
e = log.Error()  
default:  
e = log.WithLevel(v.Config.Level())  
}  
e.  
Str(“message”, message).  
e.AnErr(constants.Err, err).Send()  
}(time.Now())

---

<div class="post-metadata">

**Author:** ![saujanya](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/s/e36b37/32.png) [@saujanya](https://forum.golangbridge.org/u/saujanya)\
**Post date:** [April 17, 2023, 3:40am UTC](https://forum.golangbridge.org/t/zerolog-runtime-error/31283/4 "2023-04-17T03:40:37Z")

</div>

sean – any update on this issue ?  
I have reviewed the code couple of times, do not see log event being reused between goroutines.

---

<div class="post-metadata">

**Author:** ![Dean\_Davidson](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/dean_davidson/32/9685_2.png) [@Dean\_Davidson](https://forum.golangbridge.org/u/Dean_Davidson)\
**Post date:** [April 17, 2023, 5:24pm UTC](https://forum.golangbridge.org/t/zerolog-runtime-error/31283/5 "2023-04-17T17:24:40Z")

</div>

Your code formatting above makes it slightly hard to read. If you can create a reproducible example in the go playground, you could try opening an issue in the github repo of the library you’re trying to use.

---

<div class="post-metadata">

**Author:** ![mje](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/mje/32/10284_2.png) [@mje](https://forum.golangbridge.org/u/mje)\
**Post date:** [April 18, 2023, 2:43pm UTC](https://forum.golangbridge.org/t/zerolog-runtime-error/31283/6 "2023-04-18T14:43:45Z")

</div>

I checked the ZeroLog source. It has a single global json formatter and does not synchronize access to the formatter. Multiple goroutines calling ZeroLog functions that require the json formatter can cause the json formatter’s state to become corrupt.

---

<div class="post-metadata">

**Author:** ![saujanya](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/s/e36b37/32.png) [@saujanya](https://forum.golangbridge.org/u/saujanya)\
**Post date:** [April 18, 2023, 4:59pm UTC](https://forum.golangbridge.org/t/zerolog-runtime-error/31283/7 "2023-04-18T16:59:32Z")

</div>

thank Dean and Jeff.  
So the only solution, is to initialize logger within each goroutine

---

<div class="post-metadata">

**Author:** ![mje](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/mje/32/10284_2.png) [@mje](https://forum.golangbridge.org/u/mje)\
**Post date:** [April 18, 2023, 5:17pm UTC](https://forum.golangbridge.org/t/zerolog-runtime-error/31283/8 "2023-04-18T17:17:39Z")

</div>

That won’t help because the ZeroLog json formatter is global. If you must log in json, then you need to protect your logging calls with a mutex.

---

<div class="post-metadata">

**Author:** ![system](https://forum.golangbridge.org/uploads/default/original/2X/5/5ad234f7c3dbc68b6cc72d338ce4f2973d9fd81c.png) [@system](https://forum.golangbridge.org/u/system)\
**Post date:** [July 17, 2023, 5:17pm UTC](https://forum.golangbridge.org/t/zerolog-runtime-error/31283/9 "2023-07-17T17:17:47Z")

</div>

Closed on July 17, 2023, 5:17pm UTC

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