5.5 Logging

We use the lgr package for logging and progress output.

5.5.1 Changing mlr3 logging levels

To change the setting for mlr3 for the current session, you need to retrieve the logger (which is a R6 object) from lgr, and then change the threshold of the like this:

requireNamespace("lgr")

logger = lgr::get_logger("mlr3")
logger$set_threshold("<level>")

The default log level is "info". All available levels can be listed as follows:

getOption("lgr.log_levels")
## fatal error  warn  info debug trace 
##   100   200   300   400   500   600

To increase verbosity, set the log level to a higher value, e.g. to "debug" with:

lgr::get_logger("mlr3")$set_threshold("debug")

To reduce the verbosity, reduce the log level to warn:

lgr::get_logger("mlr3")$set_threshold("warn")

lgr comes with a global option called "lgr.default_threshold" which can be set via options() to make your choice permanent across sessions.

Also note that the optimization packages such as mlr3tuning mlr3fselect use the logger of their base package bbotk. To disable the output from mlr3, but keep the output from mlr3tuning, reduce the verbosity for the logger mlr3 and optionally change the logger bbotk to the desired level.

lgr::get_logger("mlr3")$set_threshold("warn")
lgr::get_logger("bbotk")$set_threshold("info")

5.5.2 Redirecting output

Redirecting output is already extensively covered in the documentation and vignette of lgr. Here is just a short example which adds an additional appender to log events into a temporary file in JSON format:

tf = tempfile("mlr3log_", fileext = ".json")

# get the logger as R6 object
logger = lgr::get_logger("mlr")

# add Json appender
logger$add_appender(lgr::AppenderJson$new(tf), name = "json")

# signal a warning
logger$warn("this is a warning from mlr3")
## WARN  [10:46:27.906] this is a warning from mlr3
# print the contents of the file
cat(readLines(tf))
## {"level":300,"timestamp":"2020-12-01 10:46:27","logger":"mlr","caller":"eval","msg":"this is a warning from mlr3"}
# remove the appender again
logger$remove_appender("json")