raplog provides logging for registries and underlying reports at Rapporteket. The purpose of raplog is NOT logging of how your code works (e.g. for code debugging) but rather the use of reports (statistics and auditing). See also the raplog site
You can install the released version of raplog from Rapporteket at GitHub with:
raplog collects information from three sources:
A typical usecase will call one out of two functions in raplog; appLogger()
for logging at the shiny application level and repLogger()
for logging at the report level.
To make a log entry when the shiny application “smerteregistert” starts, use raplog::appLogger()
in the shiny server function:
library(shiny)
library(raplog)
server <- function(input, output, session) {
raplog::appLogger(session, msg = "Smerteregisteret: starting shiny app")
...
}
This will add a record to the application log that may look like:
"time","user","name","group","role","resh_id","message"
"2019-08-12 10:55:38","ttester","Tore Tester","smerte","LU",999999,"Smerteregisteret: starting shiny app"
To log the use of single reports (e.g. figures, tables, documents) use raplog::repLogger()
within a reactive context in the server function:
library(shiny)
library(raplog)
server <- function(input, output, session) {
...
output$hist <- renderPlot({
raplog::repLogger(session, msg = "Smerteregisteret: providing histogram")
makeHist(df = regData, var = input$var, bins = input$bins)
...
})
...
}
This will add a record to the report log that may look like:
"time","user","name","group","role","resh_id","environment","call","message"
"2019-08-12 10:55:41","ttester","Tore Tester","smerteregisteret","LU",999999,"R_GlobalEnv","renderPlot(...)","Smerteregisteret: providing histogram"
From the last example above the log states that it was called from renderPlot(...)
residing in the R global environment (R_GlobalEnv
). The log fields environment and call would be much more useful if they provided more details on the actual function call that produces the report and the package (environment) that the registry belongs to. The latter would provide the oportunity to check wether the login credentials (as provided in the log fields user, name, group, role, and resh_id) match the actual call for the report or not (in case we have a possible breach of confidentiality). This can be obtained by moving repLogger()
from the shiny reactive function into the function that actually produces the report (makeHist()
in the above example). This can be obtained by altering makeHist()
allowing it to take the session object from shiny as an argument, e.g.:
makeHist <- function(regData, var, bins, ...) {
if ("session" %in% names(list(...))) {
raplog::repLogger(session = list(...)[["session"]], msg = "Providing histogram")
}
...
}
This will add a record to the report log that looks like:
"2019-08-12 13:04:51","ttester","Tore Tester","smerteregisteret","LU",999999,"smerteregisteret","makeHist(df = regData, var = input$var, bins = input$bins, session = session)","Providing histogram"
Changes to the function as suggested above will not alter the way it works in other context since the function only reacts to logging if session
is found among its arguments.