|
123 | 123 | end |
124 | 124 | end |
125 | 125 | end |
| 126 | + |
| 127 | +@testset "Global logging toggle disables all logging" begin |
| 128 | + problem = LogDummyProblem() |
| 129 | + algorithm = LogDummyAlgorithm(StopAfterIteration(3)) |
| 130 | + |
| 131 | + # Action that logs the current iteration number |
| 132 | + iter_logger = CallbackAction() do problem, algorithm, state |
| 133 | + @info "Iter $(state.iteration)" |
| 134 | + end |
| 135 | + |
| 136 | + # Save the current global logging state |
| 137 | + previous_state = AlgorithmsInterface.get_global_logging_state() |
| 138 | + @test previous_state == true # logging should be enabled by default |
| 139 | + |
| 140 | + # Disable logging globally |
| 141 | + AlgorithmsInterface.set_global_logging_state!(false) |
| 142 | + @test AlgorithmsInterface.get_global_logging_state() == false |
| 143 | + |
| 144 | + # Even with a logger configured, no logs should be emitted |
| 145 | + @test_logs begin |
| 146 | + with_algorithmlogger(:PostStep => iter_logger) do |
| 147 | + solve(problem, algorithm) |
| 148 | + end |
| 149 | + end |
| 150 | + |
| 151 | + # Re-enable logging |
| 152 | + AlgorithmsInterface.set_global_logging_state!(true) |
| 153 | + @test AlgorithmsInterface.get_global_logging_state() == true |
| 154 | + |
| 155 | + # Now logging should work again |
| 156 | + @test_logs (:info, "Iter 1") (:info, "Iter 2") (:info, "Iter 3") begin |
| 157 | + with_algorithmlogger(:PostStep => iter_logger) do |
| 158 | + solve(problem, algorithm) |
| 159 | + end |
| 160 | + end |
| 161 | + |
| 162 | + # Restore the original state (in case it was different) |
| 163 | + AlgorithmsInterface.set_global_logging_state!(previous_state) |
| 164 | +end |
0 commit comments