When trying to test for logging inside of a custom logger, the @test_logs macro seems to be unable to capture the logs.
Here is an MWE
@test_logs (:error, "test") match_mode = :any Logging.with_logger(Logging.SimpleLogger()) do
Which returns empty captured logs
┌ Error: test
└ @ Main REPL:2
Log Test Failed at REPL:1
Expression: Logging.with_logger(Logging.SimpleLogger()) do
#= REPL:2 =# @error "test"
Log Pattern: :error match_mode = :any
I’ve just run into this as well. Did you manage to find a solution?
Ah, having just looked through the source code, I understand the problem now:
I thought @test_logs intercepted and analysed stdout/stderr, but really it just wraps a call to with_logger(f, TestLogger). This means of course that it will never see any log messages sent to a different local logger.
That’s a bother. Does anybody have any ideas how one could circumvent this, or whether its worth submitting this as an issue?
In case it helps anybody, I have now thought of a way to circumvent the issue in my case:
# copied together from https://github.com/JuliaLang/julia/blob/master/base/logging.jl
logstate = current_task().logstate
logstate == nothing ? model.logger = global_logger() : model.logger = logstate.logger
@info "Updating model"
@test_logs((:info, "Updating model"),