# Capturing warnings inside generated functions

**URL:** https://discourse.julialang.org/t/capturing-warnings-inside-generated-functions/132535
**Category:** General Usage
**Tags:** logging, generated
**Created:** [September 21, 2025, 10:21am UTC](https://discourse.julialang.org/t/capturing-warnings-inside-generated-functions/132535 "2025-09-21T10:21:00Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [September 21, 2025, 10:21am UTC](https://discourse.julialang.org/t/capturing-warnings-inside-generated-functions/132535/1 "2025-09-21T10:21:00Z")

</div>

Is there a way to capture logging messages (e.g. warnings) issued during compilation, for example from inside a generated function? For example

```julia-auto
julia> using Logging

julia> @generated foo() = @warn "this warning is emitted during compilation"
foo (generic function with 1 method)

julia> with_logger(NullLogger()) do; foo(); end
┌ Warning: this warning is emitted during compilation
└ @ Main REPL[18]:1

```

this doesn’t capture the warning, because it’s emitted before running the body of the `do`-block, during compilation.

Bonus points for a solution which works with `Test.@test_warn`.

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [September 21, 2025, 11:41am UTC](https://discourse.julialang.org/t/capturing-warnings-inside-generated-functions/132535/2 "2025-09-21T11:41:40Z")

</div>

Very hacky workaround, but one approach would be to use `eval` or to store the function in an untyped container:

```julia-auto
julia> using Logging

julia> @generated foo() = @warn "this warning is emitted during compilation";

julia> with_logger(NullLogger()) do
           @eval foo()
       end

```

```julia-repl
julia> using Logging

julia> @generated foo() = @warn "this warning is emitted during compilation";

julia> let fv = Ref{Any}(foo)
           with_logger(NullLogger()) do 
               fv[]()
           end
       end

```

Both solutions help ensure that `foo` is not compiled at the same time as the `do` block.

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [September 21, 2025, 11:56am UTC](https://discourse.julialang.org/t/capturing-warnings-inside-generated-functions/132535/3 "2025-09-21T11:56:56Z")

</div>

> [@Mason](#):
>
> one approach would be to use `eval`

Oh, right, `@eval` works also with `@test_warn`:

```julia-auto
julia> @generated foo() = @warn "this warning is emitted during compilation"
foo (generic function with 1 method)

julia> @test_warn "this warning is emitted during compilation" @eval foo()

```

Of course this would fail if the function is called once more:

```julia-auto
julia> @test_warn "this warning is emitted during compilation" @eval foo()
Test Failed at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-XG3Q6T6R70.0/build/default-honeycrisp-XG3Q6T6R70-0/julialang/julia-release-1-dot-11/usr/share/julia/stdlib/v1.11/Test/src/Test.jl:896
  Expression: contains_warn(read(fname, String), $(Expr(:escape, "this warning is emitted during compilation")))

ERROR: There was an error during testing

```

but inside tests execution is generally deterministic, so that shouldn’t be a problem. Need only to see if using `@eval` in these tests has drawbacks, hopefully not. Thanks!

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [September 21, 2025, 12:50pm UTC](https://discourse.julialang.org/t/capturing-warnings-inside-generated-functions/132535/4 "2025-09-21T12:50:53Z")

</div>

> [@giordano](#):
>
> hopefully not

And of course my hope was ill posed. But wrapping the function in `Ref{Any}` seems to work well, so I think I’ll go with that solution, thanks!

```julia-auto
julia> @testset begin
           @generated foo() = @warn "this warning is emitted during compilation"
           f = Ref{Any}(foo)
           @test_warn "this warning is emitted during compilation" f[]()
       end;
Test Summary: | Pass Total Time
test set | 1 1 0.0s

```
