# @test\_logs behavior when using with\_logger

**URL:** https://discourse.julialang.org/t/test-logs-behavior-when-using-with-logger/80239
**Category:** General Usage
**Tags:** question
**Created:** [April 29, 2022, 2:26am UTC](https://discourse.julialang.org/t/test-logs-behavior-when-using-with-logger/80239 "2022-04-29T02:26:51Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![jdlara-berkeley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jdlara-berkeley/32/4096_2.png) [@jdlara-berkeley](https://discourse.julialang.org/u/jdlara-berkeley)
#### Post date: [April 29, 2022, 2:26am UTC](https://discourse.julialang.org/t/test-logs-behavior-when-using-with-logger/80239/1 "2022-04-29T02:26:51Z")

</div>

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

```julia
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

```julia
┌ 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?

---

<div class="post-metadata">

### Author: ![veddox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/veddox/32/45227_2.png) [@veddox](https://discourse.julialang.org/u/veddox)
#### Post date: [January 24, 2023, 4:45pm UTC](https://discourse.julialang.org/t/test-logs-behavior-when-using-with-logger/80239/2 "2023-01-24T16:45:49Z")

</div>

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](https://github.com/JuliaLang/julia/blob/b8adb2ede505e2d9eb2cfcd03caa64da643c9f22/stdlib/Test/src/logging.jl#L241) a [call](https://github.com/JuliaLang/julia/blob/b8adb2ede505e2d9eb2cfcd03caa64da643c9f22/stdlib/Test/src/logging.jl#L260) to [`with_logger(f, TestLogger)`](https://github.com/JuliaLang/julia/blob/b8adb2ede505e2d9eb2cfcd03caa64da643c9f22/stdlib/Test/src/logging.jl#L112). 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?

---

<div class="post-metadata">

### Author: ![veddox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/veddox/32/45227_2.png) [@veddox](https://discourse.julialang.org/u/veddox)
#### Post date: [January 30, 2023, 12:00pm UTC](https://discourse.julialang.org/t/test-logs-behavior-when-using-with-logger/80239/3 "2023-01-30T12:00:24Z")

</div>

Submitted an issue here: [@test\_logs doesn't work when using local loggers · Issue #48456 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/48456)

---

<div class="post-metadata">

### Author: ![veddox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/veddox/32/45227_2.png) [@veddox](https://discourse.julialang.org/u/veddox)
#### Post date: [January 31, 2023, 10:09am UTC](https://discourse.julialang.org/t/test-logs-behavior-when-using-with-logger/80239/4 "2023-01-31T10:09:17Z")

</div>

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

```julia
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)))

```
