## 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 extension packages such as mlr3tuning define their own logger which inherits from the mlr3 logger. To disable the output from mlr3, but keep the output from mlr3tuning, first reduce the verbosity for the parent logger mlr3, then restore verbosity for the child logger mlr3tuning: lgr::get_logger("mlr3")$set_threshold("warn")
lgr::get_logger("mlr3tuning")$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  [01:24:11.997] this is a warning from mlr3
# print the contents of the file
cat(readLines(tf))
## {"level":300,"timestamp":"2020-05-19 01:24:11","logger":"mlr","caller":"eval","msg":"this is a warning from mlr3"}
# remove the appender again
logger\$remove_appender("json")