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:


logger = lgr::get_logger("mlr3")

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

## 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:


To reduce the verbosity, reduce the log level to 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.


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  [00:18:02.833] this is a warning from mlr3
# print the contents of the file
## {"level":300,"timestamp":"2021-03-04 00:18:02","logger":"mlr","caller":"eval","msg":"this is a warning from mlr3"}
# remove the appender again