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 togetOption("SCDB.log_path")
. Read only.log_tbl
(
tbl_dbi(1)
)
The database table used for logging. Class is connection-specific, but inherits fromtbl_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 theLogger
instance 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 byid()
) specifying the table being updated.timestamp
(
POSIXct(1)
,Date(1)
, orcharacter(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 byid()
) 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. IfNULL
, no file logs are created.start_time
(
POSIXct(1)
)
The time at which data processing was started (defaults toSys.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 bystrftime()
).
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")
#> 2024-10-04 07:59:11.911 - 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: 2024-10-04 07:59:11.912 - runner - WARNING - This is a warning!
try(logger$log_error("This is an error!"))
#> Error in logger$log_error("This is an error!") :
#> 2024-10-04 07:59:11.913 - runner - ERROR - This is an error!