Set printing debug messages programmatically

Is there a way to turn on @debug-printing programmatically instead of using env variable JULIA_DEBUG? My attempt was

using Base.CoreLogging: disable_logging, BelowMinLevel
disable_logging(BelowMinLevel)
@debug("hi")

but with no success.

If you take a look at the Logging docs, you’ll see that there is a whole sequence of tests that occur before a log record is generated.

Your code has updated the first of the three checks, but then the default logger (a ConsoleLogger) is rejecting the debug record. Presumably, when you start julia with an env variable present, the ConsoleLogger adjusts its minimum log level.

To make it work, you should replace the default logger:

using Logging: disable_logging, global_logger, ConsoleLogger, BelowMinLevel

disable_logging(BelowMinLevel)
global_logger(ConsoleLogger(stderr, BelowMinLevel))

@debug "hi"
3 Likes