Hi,
I want to be able to temporarily suppress all errors and warnings.
I tried doing something like this using redirect_stderr
:
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 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.
2 Likes
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
?
My guess is that maybe @warn writes to stdin instead of 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> 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"
2 Likes