Here is what I want to do:
using Logging using Statistics mutable struct MyLogger <: AbstractLogger log min_level end MyLogLevel= LogLevel(-1) MyLogger() = MyLogger(, MyLogLevel) function Logging.handle_message(logger::MyLogger, level, message, _module, group, id, file, line; kargs...) push!(logger.log, kargs.data) end Logging.shouldlog(logger::MyLogger, level, _module, group, id) = level == MyLogLevel Logging.min_enabled_level(logger::MyLogger) = MyLogLevel logger = MyLogger() function compute() data = rand(10000) @logmsg MyLogLevel "Compute" mean=mean(data) end function no_log_compute() data = rand(10000) end with_logger(logger) do println("With Logger") @btime compute() end println("No logger") @btime compute() println("No log compute") @btime no_log_compute()
Here I have a custom logger, which log something which require computation, and push to my logger.
I wish that if I don’t have
with_logger(logger), it should perform the same as I didn’t write the
@logmsg in the first place, meaning don’t run anything in the key value pairs.
But in fact, I get:
With Logger 8.000 μs (8 allocations: 78.45 KiB) No logger 9.000 μs (11 allocations: 78.62 KiB) No log compute 6.340 μs (2 allocations: 78.20 KiB)
Which means, even the loglevel is above the target, the log is not logged into anything, the key value pairs still get executed anyway.
And the second version is even slower than the with logger version…