I want to be able to temporarily suppress all errors and warnings.
I tried doing something like this using
foo() = @warn "oh oh"
io = open("tmp.err","w")
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 but they don’t seem to work for this case with Julia 1.4.1.
Nevermind, I guess I should be just using
with_logger(NullLogger()) do; foo(); end here.
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
My guess is that maybe @warn writes to stdin instead of stderr?
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> using Logging
julia> io = open("stderr.txt", "w");
julia> redirect_stderr(io) do
logger = ConsoleLogger(stderr)
@info "Hello, world."
julia> read("stderr.txt", String)
"[ Info: Hello, world.\n"