# How to temporarily suppress all error and warning messages?

**URL:** <https://discourse.julialang.org/t/how-to-temporarily-suppress-all-error-and-warning-messages/38725>\
**Category:** General Usage\
**Created:** [May 4, 2020, 8:27am UTC](https://discourse.julialang.org/t/how-to-temporarily-suppress-all-error-and-warning-messages/38725 "2020-05-04T08:27:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![orialb](https://avatars.discourse-cdn.com/v4/letter/o/65b543/32.png) [@orialb](https://discourse.julialang.org/u/orialb)\
**Post date:** [May 4, 2020, 8:27am UTC](https://discourse.julialang.org/t/how-to-temporarily-suppress-all-error-and-warning-messages/38725/1 "2020-05-04T08:27:11Z")

</div>

Hi,  
I want to be able to temporarily suppress all errors and warnings.  
I tried doing something like this using `redirect_stderr` :

```julia
foo() = @warn "oh oh"

io = open("tmp.err","w")
redirect_stderr(io) do
    foo()
end
close(io)

```

but this didn’t seem to work, the warning message was still displayed (and also nothing was written to the `tmp.err` file). Am I using it wrong or misunderstanding something about stderr here?

I also tried the macros from [Suppressor.jl](https://github.com/JuliaIO/Suppressor.jl) but they don’t seem to work for this case with Julia 1.4.1.

---

<div class="post-metadata">

**Author:** ![orialb](https://avatars.discourse-cdn.com/v4/letter/o/65b543/32.png) [@orialb](https://discourse.julialang.org/u/orialb)\
**Post date:** [May 4, 2020, 8:48am UTC](https://discourse.julialang.org/t/how-to-temporarily-suppress-all-error-and-warning-messages/38725/2 "2020-05-04T08:48:53Z")

</div>

Nevermind, I guess I should be just using `with_logger(NullLogger()) do; foo(); end` here.

---

<div class="post-metadata">

**Author:** ![orialb](https://avatars.discourse-cdn.com/v4/letter/o/65b543/32.png) [@orialb](https://discourse.julialang.org/u/orialb)\
**Post date:** [May 4, 2020, 11:24am UTC](https://discourse.julialang.org/t/how-to-temporarily-suppress-all-error-and-warning-messages/38725/3 "2020-05-04T11:24:29Z")

</div>

As a follow up question- why is the code in the OP not capturing the warning message to the file? What kind of errors/warnings would be redirected when using `redirect_stderr`?

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [May 4, 2020, 1:06pm UTC](https://discourse.julialang.org/t/how-to-temporarily-suppress-all-error-and-warning-messages/38725/4 "2020-05-04T13:06:29Z")

</div>

My guess is that maybe @warn writes to stdin instead of stderr?

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [May 4, 2020, 1:13pm UTC](https://discourse.julialang.org/t/how-to-temporarily-suppress-all-error-and-warning-messages/38725/5 "2020-05-04T13:13:41Z")

</div>

> [@orialb](#):
>
> As a follow up question- why is the code in the OP not capturing the warning message to the file? What kind of errors/warnings would be redirected when using `redirect_stderr` ?

The old `stderr` has already been captured by the global logger, so it will not be redirected. If you reverse the order (create the logger after redirecting) it works:

```julia
julia> using Logging

julia> io = open("stderr.txt", "w");

julia> redirect_stderr(io) do
           logger = ConsoleLogger(stderr)
           with_logger(logger) do
               @info "Hello, world."
           end
       end

julia> close(io)

julia> read("stderr.txt", String)
"[ Info: Hello, world.\n"

```

---

<div class="post-metadata">

**Author:** ![orialb](https://avatars.discourse-cdn.com/v4/letter/o/65b543/32.png) [@orialb](https://discourse.julialang.org/u/orialb)\
**Post date:** [May 4, 2020, 4:01pm UTC](https://discourse.julialang.org/t/how-to-temporarily-suppress-all-error-and-warning-messages/38725/6 "2020-05-04T16:01:12Z")

</div>

Thanks!
