Deactivate logging message locally

I currently have a struct whose constructor logs some messages when called, using @info and potentially @warn. This is useful to me when I need to check in the log what parameters I actually used in some of my computations.

However, in some other part, I need to initialize this structure but with default values, for which I do not need to see such logging message. Is there a way to achieve this ?

At the moment my strategy is the following:

let initialized_var
    with_logger(NullLogger()) do 
        initialized_var = init_var()
    end
end

The let is to ensure initialized_var is known outside of the with_logger... block. Is it possible to avoid this kind of structure ?

You could define two constructor methods, eg something like

struct Foo
    x::Int
    function Foo(x::Int)
        @warn "constructing Foo" x
        new(x)
    end
    Foo() = new(42)
end
1 Like

Interesting, in some way, you mean to redefine the default constructor of the structure, right ?

https://docs.julialang.org/en/v1/manual/constructors/#man-inner-constructor-methods

1 Like