# Avoid double stacktrace when logging error

**URL:** <https://discourse.julialang.org/t/avoid-double-stacktrace-when-logging-error/73374>\
**Category:** General Usage\
**Tags:** question, error, logging\
**Created:** [December 20, 2021, 3:05pm UTC](https://discourse.julialang.org/t/avoid-double-stacktrace-when-logging-error/73374 "2021-12-20T15:05:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![visr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/visr/32/17204_2.png) [@visr](https://discourse.julialang.org/u/visr)\
**Post date:** [December 20, 2021, 3:05pm UTC](https://discourse.julialang.org/t/avoid-double-stacktrace-when-logging-error/73374/1 "2021-12-20T15:05:27Z")

</div>

I have a long running application with custom loggers set up, and when an error is thrown, I want to log that nicely with `@error`. This is done in a `catch` block, and works well. Since `@error` does not throw an error itself, I added `rethrow()`, because I want a non-zero exit code. What I don’t like however is that I then get two stacktraces in the terminal.

```julia
function f()
    try
        error("oh no")
    catch e
        @error "Something went wrong" exception = (e, catch_backtrace())
        rethrow()
    end
end

f()

```

```julia
┌ Error: Something went wrong
│ exception =
│ oh no
│ Stacktrace:
│ [1] error(s::String)
│ @ Base .\error.jl:33
│ [2] f()
│ @ Main C:\tmp\err\err.jl:3
│ [3] top-level scope
│ @ C:\tmp\err\err.jl:10
│ [4] include(mod::Module, _path::String)
│ @ Base .\Base.jl:418
│ [5] exec_options(opts::Base.JLOptions)
│ @ Base .\client.jl:292
│ [6] _start()
│ @ Base .\client.jl:495
└ @ Main C:\tmp\err\err.jl:5
ERROR: LoadError: oh no
Stacktrace:
 [1] error(s::String)
   @ Base .\error.jl:33
 [2] f()
   @ Main C:\tmp\err\err.jl:3
 [3] top-level scope
   @ C:\tmp\err\err.jl:10
in expression starting at C:\tmp\err\err.jl:10

```

Is there a way to avoid swallowing the exception but not have it printed a second time? Of course `exit(1)` instead of `rethrow()` would work but I don’t want it to quit julia.

---

<div class="post-metadata">

**Author:** ![visr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/visr/32/17204_2.png) [@visr](https://discourse.julialang.org/u/visr)\
**Post date:** [December 20, 2021, 4:28pm UTC](https://discourse.julialang.org/t/avoid-double-stacktrace-when-logging-error/73374/2 "2021-12-20T16:28:25Z")

</div>

In my custom logging I use the `TeeLogger` from [LoggingExtras.jl](https://github.com/JuliaLogging/LoggingExtras.jl) to log simultaneously to the terminal and a file.

Now I attach an `_id = :my_id` to this particular `@error`, and use `EarlyFilteredLogger` to filter out that ID for the terminal, but not for the logfile. Combined with [this trick](https://github.com/JuliaLogging/LoggingExtras.jl/issues/46#issuecomment-803716480) I then get a nice stacktrace from `@error` in my logfile, and a single stacktrace from `rethrow()` in the terminal. That seems like an acceptable workaround, though if people have other ideas I’d like to hear them.
