The logging of informations is important to have useful logs to explicit processings. Info logs are used to show general informations. Warning logs are used to show potential errors, that are not blocking for the processing. Error logs are used to explicit better errors, before stopping the program. Debug logs are usually activated with the command line flag -d
. They are used to show usefull debug informations and should be very widely used. Verbose are like info logs, but devivers optionnal informations. It is usually activated with the -v
or -V
command line flag. INTERNAL logs are for the logger itself, when it detects internal problems, like a closed output log file, or so.
This is a class that helps you logging outputs. The advantages of using a logger is that people write logs in a uniform way. The logger can be named and will provide more informations like:
It can also help you create log files,
## provide a path at logger creation
logfile <- tempfile()
logger <- W4MRUtils::get_logger(
"LoggerTest2",
out_path = logfile
)
or
## messages are printed to the terminal and sent to the log file.
logger$info("Info message")
#> [32m[ info-LoggerTest3-16:31:36] - Info message[m
logger$warning("Warning message")
#> [33m[warning-LoggerTest3-16:31:36] - Warning message[m
logger$error("Error message")
#> [31m[ error-LoggerTest3-16:31:36] - Error message[m
logger$debug("Debug message")
## debug messages are deactivated by default
logger$verbose("Verbose message")
## verbose messages are deactivated by default
print(readLines(logfile))
#> [1] "[ info-LoggerTest3-16:31:36] - Info message"
#> [2] "[warning-LoggerTest3-16:31:36] - Warning message"
#> [3] "[ error-LoggerTest3-16:31:36] - Error message"
file.remove(logfile)
#> [1] TRUE
By default, logs are formated using the following pattern: “[{{ level }}-{{ name }}-{{ time }}] - {{ message }}” You can change the format string when you create the logger with get_logger:
By default, a coloring is used to easily differentiate kinds of log. The coloring is the following:
This coloring can be changed by providing a named list to the get_logger function: