Get Started¶
This documents the core functionalities of the log package.
Setup¶
A logger is created from the Logger
class. Setting write
to TRUE
will write every message logged to the file
. The prefix
is a string that will precede every message. The sep
argument is the separator between the prefix
and the rest of the message to the right, it defaults to a tab (t
).
1 2 3 4 5 6 7 8 9 |
|
Different loggers can write to the same file.
One can leave write
as the default FALSE
and later use the dump
method to save it to a file.
Basic¶
After the logger has been instantiated one can use the log
method to log a message to the console (and the file depending on whether that was set).
1 2 3 4 5 6 7 8 9 10 11 |
|
Flags¶
When setting up the logger one can customise it with “flags” so it includes a bit more information that might be useful to debug, e.g.: the time and date at which the message was logged. These will be written after the prefix
and are placed in the order they are used (e.g.: date then time as shown below).
The date and time format can be customised with the format
argument.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Flags available:
date
- current datetime
- current timeunix
- unix timestampwd
- working directory
Tip
You can also customise the look of the prefix with hook
, pass it a function that will take the prefix and return a modified version of it.
While the package comes with basic flags you can add your own with the flag
method. This method accepts either a function that will be run every time a message is logged or a string that will simply be included in the message.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
Hook¶
There is also the possibility to pass a “hook;” a function that will preprocess the prefix and return a modified version of it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Tip
The package comes with default loggers, see templates
Dump¶
Finally you can dump the log to a file with dump
.
1 |
|
Printer¶
By default log uses cli::cat_cli
to print the messages, this can be changed via the printer
field. It accepts a function that will be used to print messages, this function must accept a single argument: the message.
1 2 3 4 5 |
|
Predicate¶
The predicate
field allows you to pass a predicate function that defines whether the logger actually runs. This very useful when debugging for instance.
If the predicate function returns TRUE
then the logger runs, if it returns FALSE
the logger does not print, write to file, or dump the log.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|