logging

General informations

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.

What is a logger?

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,

How to create a logger?

logger <- W4MRUtils::get_logger("LoggerTest1")

How to create a log file?

## provide a path at logger creation
logfile <- tempfile()
logger <- W4MRUtils::get_logger(
  "LoggerTest2",
  out_path = logfile
)

or

## add it after the logger's creation
logfile <- tempfile()
logger <- W4MRUtils::get_logger("LoggerTest3")
logger$set_out_paths(logfile)

How to send messages?

## messages are printed to the terminal and sent to the log file.
logger$info("Info message")
#> [   info-LoggerTest3-16:31:36] - Info message
logger$warning("Warning message")
#> [warning-LoggerTest3-16:31:36] - Warning message
logger$error("Error message")
#> [  error-LoggerTest3-16:31:36] - Error message
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

formating

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:

W4MRUtils::get_logger(
  "Processing",
  format = "[{{ time }}-{{ name }}] - {{ message }}"
)

coloring

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:

W4MRUtils::get_logger(
  "Processing",
  coloring = list(
    debug = "red",
    warning = "green",
    error = "purple",
    verbose = "blue",
    info = "orange",
    INTERNAL = "white"
  ),
  show_debug = TRUE
)$info("Infos are orange")$debug("Debug is red")
#> [16:31:36-Processing] - Infos are orange