The Logger class facilitates logging to a database and/or file and to console.
A Logger is associated with a specific table and timestamp which must be supplied at initialization.
This information is used to create the log file (if a log_path is given) and the log entry in the database
(if a log_table_id and log_conn is given).
Logging to the database must match the fields in the log table.
Value
A new instance of the Logger R6 class.
Active bindings
- output_to_console
- ( - logical(1))
 Should the Logger output to console? Read only. This can always be overridden by Logger$log_info(..., output_to_console = FALSE).
- log_path
- ( - character(1))
 The location log files are written (if this is not NULL). Defaults to- getOption("SCDB.log_path"). Read only.
- log_tbl
- ( - tbl_dbi(1))
 The database table used for logging. Class is connection-specific, but inherits from- tbl_dbi. Read only.
- start_time
- ( - POSIXct(1))
 The time at which data processing was started. Read only.
- log_filename
- ( - character(1))
 The filename (basename) of the file that the- Loggerinstance will output to. Read only.
- log_realpath
- ( - character(1))
 The full path to the logger's log file. Read only.
Methods
Method new()
Create a new Logger object
Arguments
- db_table
- ( - id-like object(1))
 A table specification (coercible by- id()) specifying the table being updated.
- timestamp
- ( - POSIXct(1),- Date(1), or- character(1))
 A timestamp describing the data being processed (not the current time).
- output_to_console
- ( - logical(1))
 Should the Logger output to console?
- log_table_id
- ( - id-like object(1))
 A table specification (coercible by- id()) specifying the location of the log table.
- log_conn
- ( - DBIConnection(1))
 A database connection where log table should exist.
- log_path
- ( - character(1))
 The path where logs are stored. If- NULL, no file logs are created.
- start_time
- ( - POSIXct(1))
 The time at which data processing was started (defaults to- Sys.time()).
- warn
- ( - logical(1))
 Should a warning be produced if no logging will be done?
Method log_info()
Write a line to log (console / file).
Arguments
- ...
- ( - character())
 Character strings to be concatenated as log message.
- tic
- ( - POSIXct(1))
 The timestamp used by the log entry.
- output_to_console
- ( - logical(1))
 Should the line be written to console?
- log_type
- ( - character(1))
 The severity of the log message.
- timestamp_format
- ( - character(1))
 The format of the timestamp used in the log message (parsable by- strftime()).
Method log_warn()
Write a warning to log file and generate warning.
Arguments
- ...
- ( - character())
 Character strings to be concatenated as log message.
- log_type
- ( - character(1))
 The severity of the log message.
Method log_error()
Write an error to log file and stop execution.
Arguments
- ...
- ( - character())
 Character strings to be concatenated as log message.
- log_type
- ( - character(1))
 The severity of the log message.
Method finalize_db_entry()
Auto-fills "end_time" and "duration" for the log entry and clears the "log_file" field if no file is being written.
Usage
Logger$finalize_db_entry(end_time = Sys.time())Examples
  logger <- Logger$new(
    db_table = "test.table",
    timestamp = "2020-01-01 09:00:00"
  )
#> Warning: `log_path` and `log_tbl` are both `NULL` and therefore NO file or database logging will be done.
#> Consider adding options SCDB.log_table_id and/or SCDB.log_path to your .Rprofile
  logger$log_info("This is an info message")
#> 2025-02-27 13:10:19.014 - runner - INFO - This is an info message
  logger$log_to_db(message = "This is a message")
  try(logger$log_warn("This is a warning!"))
#> Warning: 2025-02-27 13:10:19.016 - runner - WARNING - This is a warning!
  try(logger$log_error("This is an error!"))
#> Error : 2025-02-27 13:10:19.017 - runner - ERROR - This is an error!
