- Notifications
You must be signed in to change notification settings - Fork3
opinionated, lightweight logging for R
License
Unknown, MIT licenses found
Licenses found
sellorm/rlog
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
rlog is an opinionated, lightweight logging package for R.
It relies on long standing Unix traditions, to write simple log messages in a flexible way.
Log message output looks like this:
2021-02-21 17:19:06 [INFO] This is my log messageInstall rlog from GitHub with the following:
devtools::install_github("sellorm/rlog")
We think so, yes!
There are already loads of logging packages for R. Many are no longer maintained and of the remaining selection some are overly complex, making them less approachable for beginners.
Since logging should be an essential part of any production application or pipeline I think it's important that as many people as possible are introduced to good logging practice.
If rlog doesn't have enough features for you, I'd recommend the excellent "logger" instead.
The rlog philosophy is theUnix philosophy and as such, we expect applications using rlog to be able to integrate with other enterprise software quickly and efficiently.
rlog is simple by design and it relies on existing Unix/Linux norms to get the job done.
- Log message format is intentionally not configurable
- the log level can only be controlled by an environment variable
- log messages are written to standard output and standard error
The best way to use rlog is to call the functions directly from within your applications.
There is one function per log message level:
rlog::log_fatal("my fatal message")rlog::log_error("my error message")rlog::log_warn("my warn message")rlog::log_info("my info message")rlog::log_debug("my debug message")rlog::log_trace("my trace message")
If you run this code as-is without setting a customLOG_LEVEL with the environment variable, you'll get output like this:
2021-02-21 17:19:06 [FATAL] fatal2021-02-21 17:19:06 [ERROR] error2021-02-21 17:19:06 [WARN] warn2021-02-21 17:19:06 [INFO] infoThe "DEBUG" and "TRACE" message won't be printed, since the defaultLOG_LEVEL is "INFO" and those message have lower priority.
Play around with setting theLOG_LEVEL directly in R and seeing what messages are emitted:
Sys.setenv("LOG_LEVEL"="ERROR")rlog::log_fatal("my fatal message")rlog::log_error("my error message")rlog::log_warn("my warn message")rlog::log_info("my info message")rlog::log_debug("my debug message")rlog::log_trace("my trace message")
Gives us this output:
2021-02-21 17:19:06 [FATAL] fatal2021-02-21 17:19:06 [ERROR] errorRemember though, the best way to set the environment variable is outside of the app. That way it's easier to change at run-time.
So in your terminal you can do something like this before you run your script:
$export LOG_LEVEL=TRACE$ ./my_script.ROr you can even call your script with the appropriate value at run-time:
$ LOG_LEVEL=ERROR ./my_script.R
If you're using RStudio Connect users can set an environment variable calledLOG_LEVEL in the "Vars" tab of their apps control panel.
rlog relies on an environment variable calledLOG_LEVEL to control which log messages are emitted.
If it's not set, it will default to "INFO".
The available levels -- in decreasing order of severity -- are as follows:
- fatal
- error
- warn
- info
- debug
- trace
When theLOG_LEVEL environment variable is set to "INFO", only messages at that level and above will be emitted.
Another example:
- fatal
- error
- warn
- info
- debug <= If we set the log level here only messages of debug and above will be emitted.
- trace
This is a really powerful way of only including the messages you want to see in the log files at any given time. For example, you may choose to set theLOG_LEVEL to "TRACE" while developing your application or if it runs into a problem in production, but you might choose to run the application with aLOG_LEVEL of "ERROR" under normal circumstances.
rlog outputs it log messages into the console in the same way that most Unix/Linux tools do. Ordinary messages go to "standard output" (stdout) and errors go to "standard error" (stderr).
This means they can be captured in the standard Unix/Linux way:
./my_script.R > /path/to/file.log 2>&1This command redirects stderr to stdout and drops it all into the/path/to/file.log file.
You can also keep the "normal" and error outputs separate:
./my_script.R > /path/to/file.log 2> /path/to/errors.logMany enterprise software products, such as RStudio Connect will capture this output automatically and save it for you.
About
opinionated, lightweight logging for R
Topics
Resources
License
Unknown, MIT licenses found
Licenses found
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Uh oh!
There was an error while loading.Please reload this page.
Contributors3
Uh oh!
There was an error while loading.Please reload this page.