@test_logs behavior when using with_logger

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

using Test
using Logging
@test_logs (:error, "test") match_mode = :any Logging.with_logger(Logging.SimpleLogger()) do 
       @error "test"
       end

Which returns empty captured logs

┌ Error: test
└ @ Main REPL[58]:2
Log Test Failed at REPL[58]:1
  Expression: Logging.with_logger(Logging.SimpleLogger()) do 
    #= REPL[58]:2 =# @error "test"
end
  Log Pattern: :error match_mode = :any
  Captured Logs: 

Anyone has experienced this and knows how to fix?

I’ve just run into this as well. Did you manage to find a solution?

Edit:

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?

Submitted an issue here: @test_logs doesn't work when using local loggers · Issue #48456 · JuliaLang/julia · GitHub

In case it helps anybody, I have now thought of a way to circumvent the issue in my case:

function withtestlogger(model)
    # 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
    model
end

function stepsimulation!(model)
    with_logger(model.logger) do
        @info "Updating model"
    end
end

@test_logs((:info, "Updating model"),
           min_level=Logging.Debug, match_mode=:any,
           stepsimulation!(withtestlogger(model)))